Class: Bricolage::CommandLineApplication
- Inherits:
-
Object
- Object
- Bricolage::CommandLineApplication
- Extended by:
- Forwardable
- Defined in:
- lib/bricolage/commandlineapplication.rb
Defined Under Namespace
Classes: DataSourceOpt
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #context ⇒ Object
- #create_context ⇒ Object
- #data_source(long_option) ⇒ Object
- #data_source_option(long_option, description, kind:, short: nil, default: nil) ⇒ Object
- #define_default_options ⇒ Object
-
#initialize ⇒ CommandLineApplication
constructor
A new instance of CommandLineApplication.
- #main ⇒ Object
- #parse! ⇒ Object
Constructor Details
#initialize ⇒ CommandLineApplication
Returns a new instance of CommandLineApplication.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/bricolage/commandlineapplication.rb', line 15 def initialize @name = File.basename($0) @home = nil @env = nil @subsys = nil @ds = {} @options = OptionParser.new @options. = "Usage: #{@name} [options]" end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/bricolage/commandlineapplication.rb', line 26 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
27 28 29 |
# File 'lib/bricolage/commandlineapplication.rb', line 27 def @options end |
Class Method Details
.define {|opts| ... } ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/bricolage/commandlineapplication.rb', line 7 def CommandLineApplication.define Application.install_signal_handlers opts = new yield opts opts.parse! opts end |
Instance Method Details
#context ⇒ Object
69 70 71 |
# File 'lib/bricolage/commandlineapplication.rb', line 69 def context @context ||= create_context end |
#create_context ⇒ Object
73 74 75 |
# File 'lib/bricolage/commandlineapplication.rb', line 73 def create_context Bricolage::Context.for_application(@home, environment: @env) end |
#data_source(long_option) ⇒ Object
56 57 58 59 |
# File 'lib/bricolage/commandlineapplication.rb', line 56 def data_source(long_option) ent = @ds[long_option] or raise ArgumentError, "no such data source entry: #{long_option}" ent.ds ||= context.get_data_source(ent.kind, ent.name) end |
#data_source_option(long_option, description, kind:, short: nil, default: nil) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/bricolage/commandlineapplication.rb', line 49 def data_source_option(long_option, description, kind:, short: nil, default: nil) @ds[long_option] = DataSourceOpt.new(kind, default || kind) @options.on(* [short, long_option + "=NAME", description + " (default: #{default || kind})"].compact) {|ds_name| @ds[long_option] = DataSourceOpt.new(kind, ds_name) } end |
#define_default_options ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bricolage/commandlineapplication.rb', line 29 def @options.on_tail('-e', '--environment=ENV', "Bricolage execution environment. (default: #{Context.environment})") {|env| @env = env } default_home = Context.home_path @options.on_tail('-C', '--home=PATH', "Bricolage home directory. (default: #{default_home == Dir.getwd ? '.' : default_home})") {|path| @home = path } @options.on_tail('--help', 'Prints this message and quit.') { puts @options.help exit 0 } end |
#main ⇒ Object
77 78 79 80 81 82 |
# File 'lib/bricolage/commandlineapplication.rb', line 77 def main yield rescue => ex $stderr.puts "#{@name}: error: #{ex.}" exit 1 end |
#parse! ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/bricolage/commandlineapplication.rb', line 61 def parse! return @options.parse! rescue OptionParser::ParseError => err $stderr.puts "#{$0}: error: #{err.}" $stderr.puts @options.help exit 1 end |