Class: Opt::Command
- Inherits:
-
Object
- Object
- Opt::Command
- Defined in:
- lib/opt/command.rb
Direct Known Subclasses
Defined Under Namespace
Instance Attribute Summary collapse
-
#commands ⇒ Array<Command>
readonly
List of registered subcommands.
-
#name ⇒ Object
readonly
The command name.
-
#options ⇒ Array<Option>
readonly
List of registered options for this command.
Instance Method Summary collapse
-
#command(name, opts = {}) {|command| ... } ⇒ Command
Add a subcommand.
-
#defaults ⇒ Hash<String, Object>
private
Return hash with default values for all options.
-
#initialize(name, opts = {}) ⇒ Command
constructor
private
A new instance of Command.
-
#option(definition = nil, opts = {}, &block) ⇒ Object
Register a new option.
-
#parse(argv = ARGV) ⇒ Result
Parses given list of command line tokens.
- #parse_argv!(argv, result, options = []) ⇒ Object private
Constructor Details
#initialize(name, opts = {}) ⇒ Command
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Command.
31 32 33 34 35 36 |
# File 'lib/opt/command.rb', line 31 def initialize(name, opts = {}) @name = name.to_s.freeze @opts = opts @options = [] @commands = [] end |
Instance Attribute Details
#commands ⇒ Array<Command> (readonly)
List of registered subcommands.
Can be used to add manually created Opt::Commands but use with care as no collision or sanity checks are done.
23 24 25 |
# File 'lib/opt/command.rb', line 23 def commands @commands end |
#name ⇒ Object (readonly)
The command name.
27 28 29 |
# File 'lib/opt/command.rb', line 27 def name @name end |
Instance Method Details
#command(name, opts = {}) {|command| ... } ⇒ Command
Add a subcommand.
A command can either have subcommands or free-text options.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/opt/command.rb', line 118 def command(name, opts = {}) if .any?{|o| o.text? } raise ArgumentError.new \ 'Can only have subcommands OR free-text arguments.' end command = Command.new(name, opts) if commands.any?{|c| c.name == command.name } raise ArgumentError.new "Command `#{command.name}' already registered." end yield command if block_given? commands << command command end |
#defaults ⇒ Hash<String, Object>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return hash with default values for all options.
142 143 144 |
# File 'lib/opt/command.rb', line 142 def defaults Hash[.map{|o| [o.name, o.default] }] end |
#option(definition = nil, opts = {}, &block) ⇒ Object
Register a new option.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/opt/command.rb', line 77 def option(definition = nil, opts = {}, &block) option = Option.new(definition, opts, &block) if commands.any? raise ArgumentError.new \ 'Can only have subcommands OR free-text arguments.' end if (opt = .find{|o| o.collide?(option) }) raise "Option `#{definition}' collides with already " \ "registered option: #{opt}" else << option end end |
#parse(argv = ARGV) ⇒ Result
Parses given list of command line tokens.
159 160 161 162 163 164 165 166 |
# File 'lib/opt/command.rb', line 159 def parse(argv = ARGV) result = Result.new result.merge! defaults parse_argv! parse_tokens(argv.dup), result result end |
#parse_argv!(argv, result, options = []) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/opt/command.rb', line 170 def parse_argv!(argv, result, = []) += self. while argv.any? next if .any?{|o| o.parse!(argv, result) } if argv.first.text? if (cmd = commands.find{|c| c.name == argv.first.value }) result.command << argv.shift.value cmd.parse_argv!(argv, result, ) next end end raise "Unknown option (#{argv.first.type}): #{argv.first}" end end |