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.
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 = {} = OptionParser.new . = "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 |
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
67 68 69 |
# File 'lib/bricolage/commandlineapplication.rb', line 67 def context @context ||= create_context end |
#create_context ⇒ Object
71 72 73 |
# File 'lib/bricolage/commandlineapplication.rb', line 71 def create_context Bricolage::Context.for_application(@home, environment: @env) end |
#data_source(long_option) ⇒ Object
54 55 56 57 |
# File 'lib/bricolage/commandlineapplication.rb', line 54 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
47 48 49 50 51 52 |
# File 'lib/bricolage/commandlineapplication.rb', line 47 def data_source_option(long_option, description, kind:, short: nil, default: nil) @ds[long_option] = DataSourceOpt.new(kind, default || kind) .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
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bricolage/commandlineapplication.rb', line 28 def .on_tail('-e', '--environment=ENV', "Bricolage execution environment. (default: #{Context.environment})") {|env| @env = env } default_home = Context.home_path .on_tail('-C', '--home=PATH', "Bricolage home directory. (default: #{default_home == Dir.getwd ? '.' : default_home})") {|path| @home = path } .on_tail('--help', 'Prints this message and quit.') { puts .help exit 0 } end |
#main ⇒ Object
75 76 77 78 79 80 |
# File 'lib/bricolage/commandlineapplication.rb', line 75 def main yield rescue => ex $stderr.puts "#{@name}: error: #{ex.message}" exit 1 end |
#parse! ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/bricolage/commandlineapplication.rb', line 59 def parse! return .parse! rescue OptionParser::ParseError => err $stderr.puts "#{$0}: error: #{err.message}" $stderr.puts .help exit 1 end |