Class: Swamp::Command
- Inherits:
-
Object
- Object
- Swamp::Command
- Defined in:
- lib/swamp/command.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #add_option(command, *args, &block) ⇒ Object
-
#initialize(cli, name) ⇒ Command
constructor
A new instance of Command.
- #parse_options(argv) ⇒ Object
- #set_action(&block) ⇒ Object
Constructor Details
#initialize(cli, name) ⇒ Command
Returns a new instance of Command.
11 12 13 14 15 16 |
# File 'lib/swamp/command.rb', line 11 def initialize(cli, name) @cli = cli @name = name @options = {} @args = [] end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
8 9 10 |
# File 'lib/swamp/command.rb', line 8 def action @action end |
#description ⇒ Object
Returns the value of attribute description.
9 10 11 |
# File 'lib/swamp/command.rb', line 9 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/swamp/command.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/swamp/command.rb', line 7 def @options end |
Instance Method Details
#add_option(command, *args, &block) ⇒ Object
22 23 24 |
# File 'lib/swamp/command.rb', line 22 def add_option(command, *args, &block) @options[command] = {spec: [command] + args, block: block} end |
#parse_options(argv) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/swamp/command.rb', line 26 def (argv) = {} OptionParser.new do |opts| opts.version = @cli.version opts. = "Usage: #{@cli.app_name} #{@name} [options]" @options.values.each do |option| opts_block = proc { |value| option[:block].call(value, ) } opts.on(*option[:spec], &opts_block) end end.parse!(argv) rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e raise Swamp::Error, e. end |
#set_action(&block) ⇒ Object
18 19 20 |
# File 'lib/swamp/command.rb', line 18 def set_action(&block) @action = block end |