Class: Avalanche::CLI::Command
- Inherits:
-
Object
- Object
- Avalanche::CLI::Command
- Defined in:
- lib/avalanche/cli/command.rb
Instance Method Summary collapse
-
#initialize(usage, options, handler) ⇒ Command
constructor
A new instance of Command.
- #run(argv) ⇒ Object
- #to_proc ⇒ Object
Constructor Details
#initialize(usage, options, handler) ⇒ Command
Returns a new instance of Command.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/avalanche/cli/command.rb', line 4 def initialize(usage, , handler) parts = usage.split(/\s+/) @name = parts.shift usage = parts.join(" ") @handler = handler.to_proc @options = {} @opts = OptionParser.new @opts.program_name = @name @opts. = "Usage: #{@name} #{usage}" .each do |name, opts| @opts.on(*opts) do |v| @options[name] = v end end @opts.base.append("", nil, nil) @opts.base.append("Additional options:", nil, nil) @opts.on_tail("-h", "--help", "Show this message") { print_help } @opts.on_tail("-d", "--debug", "Enable debug output") { enable_debug } end |
Instance Method Details
#run(argv) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/avalanche/cli/command.rb', line 29 def run(argv) $0 = "#{@name} #{argv.join(" ")}" @handler.call(@options, @opts.permute!(argv)) rescue ArgumentError, OptionParser::InvalidOption => e @opts.warn(e.) if @debug puts "" puts e.backtrace puts "" end print_help rescue Interrupt exit 0 rescue => e if @debug puts "" puts e.backtrace puts "" end @opts.abort(e.) end |
#to_proc ⇒ Object
54 55 56 |
# File 'lib/avalanche/cli/command.rb', line 54 def to_proc proc { |opts, args| run(args) } end |