Class: Decline::Command
- Inherits:
-
Object
- Object
- Decline::Command
- Defined in:
- lib/decline/command.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#subcommands ⇒ Object
readonly
Returns the value of attribute subcommands.
Instance Method Summary collapse
- #add_command(name, command) ⇒ Object
- #add_options(&block) ⇒ Object
-
#initialize ⇒ Command
constructor
A new instance of Command.
- #run(argv, parent_config = {}) ⇒ Object
Constructor Details
#initialize ⇒ Command
Returns a new instance of Command.
8 9 10 11 12 13 |
# File 'lib/decline/command.rb', line 8 def initialize @config = OpenStruct.new @parser = OptionParser.new @subcommands ||= Hash.new self.(@parser) # Callback to the child class end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/decline/command.rb', line 6 def config @config end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
6 7 8 |
# File 'lib/decline/command.rb', line 6 def parser @parser end |
#subcommands ⇒ Object (readonly)
Returns the value of attribute subcommands.
6 7 8 |
# File 'lib/decline/command.rb', line 6 def subcommands @subcommands end |
Instance Method Details
#add_command(name, command) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/decline/command.rb', line 15 def add_command(name, command) command. do |cli| cli.set_program_name "#{parser.program_name} #{name}" end subcommands[name.to_s] = command self end |
#add_options(&block) ⇒ Object
25 26 27 28 |
# File 'lib/decline/command.rb', line 25 def (&block) block.call(parser) self end |
#run(argv, parent_config = {}) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/decline/command.rb', line 30 def run(argv, parent_config={}) merge_config(parent_config) # I didn't want to use `order!` here because it will mutate argv, but it parsed_args = Array(parser.order!(argv)) call_command(parsed_args) end |