Class: Bricolage::CommandLineApplication

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bricolage/commandlineapplication.rb

Defined Under Namespace

Classes: DataSourceOpt

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandLineApplication

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.banner = "Usage: #{@name} [options]"
  define_default_options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/bricolage/commandlineapplication.rb', line 26

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



27
28
29
# File 'lib/bricolage/commandlineapplication.rb', line 27

def options
  @options
end

Class Method Details

.define {|opts| ... } ⇒ Object

Yields:

  • (opts)


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

#contextObject



69
70
71
# File 'lib/bricolage/commandlineapplication.rb', line 69

def context
  @context ||= create_context
end

#create_contextObject



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_optionsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bricolage/commandlineapplication.rb', line 29

def define_default_options
  @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

#mainObject



77
78
79
80
81
82
# File 'lib/bricolage/commandlineapplication.rb', line 77

def main
  yield
rescue => ex
  $stderr.puts "#{@name}: error: #{ex.message}"
  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.message}"
  $stderr.puts @options.help
  exit 1
end