Class: Optitron::Parser
- Inherits:
-
Object
- Object
- Optitron::Parser
- Defined in:
- lib/optitron/parser.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#short_opts ⇒ Object
readonly
Returns the value of attribute short_opts.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
- #help ⇒ Object
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #parse(argv = ARGV) ⇒ Object
- #parse_args(tokens, args, response) ⇒ Object
- #parse_options(tokens, options, response) ⇒ Object
Constructor Details
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
4 5 6 |
# File 'lib/optitron/parser.rb', line 4 def args @args end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
4 5 6 |
# File 'lib/optitron/parser.rb', line 4 def commands @commands end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/optitron/parser.rb', line 4 def @options end |
#short_opts ⇒ Object (readonly)
Returns the value of attribute short_opts.
4 5 6 |
# File 'lib/optitron/parser.rb', line 4 def short_opts @short_opts end |
#target ⇒ Object
Returns the value of attribute target.
3 4 5 |
# File 'lib/optitron/parser.rb', line 3 def target @target end |
Instance Method Details
#help ⇒ Object
14 15 16 |
# File 'lib/optitron/parser.rb', line 14 def help @help.generate end |
#parse(argv = ARGV) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/optitron/parser.rb', line 18 def parse(argv = ARGV) tokens = Tokenizer.new(self, argv).tokens response = Response.new(self, tokens) = @options args = @args unless @commands.empty? potential_cmd_toks = tokens.select { |t| t.respond_to?(:lit) } if cmd_tok = potential_cmd_toks.find { |t| @commands.assoc(t.lit) } tokens.delete(cmd_tok) response.command = cmd_tok.lit += @commands.assoc(cmd_tok.lit).last. args = @commands.assoc(cmd_tok.lit).last.args elsif !potential_cmd_toks.empty? && @target.respond_to?(:command_missing) command = potential_cmd_toks.first.lit response.command = 'command_missing' @commands << [response.command, Option::Cmd.new(response.command)] @commands.assoc(response.command).last..insert(-1, *tokens.select { |t| !t.respond_to?(:lit) }.map { |t| t.is_a?(Tokenizer::Named) ? Option::Opt.new(t.name, nil, :short_name => t.name) : Option::Opt.new(t.name, nil, :type => (t.value ? :string : :boolean)) }) @commands.assoc(response.command).last.args << Option::Arg.new('command', 'Command name', :type => :string) << Option::Arg.new('args', 'Command arguments', :type => :greedy) += @commands.assoc(response.command).last. args = @commands.assoc(response.command).last.args else potential_cmd_toks.first ? response.add_error('an unknown command', potential_cmd_toks.first.lit) : response.add_error('unknown command') end end (tokens, , response) parse_args(tokens, args, response) response.validate response end |
#parse_args(tokens, args, response) ⇒ Object
68 69 70 |
# File 'lib/optitron/parser.rb', line 68 def parse_args(tokens, args, response) args.each { |arg| arg.consume(response, tokens) } end |
#parse_options(tokens, options, response) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/optitron/parser.rb', line 56 def (tokens, , response) .each do |opt| response.params[opt.name] = opt.default if opt.has_default? if opt_tok = tokens.find { |tok| opt.match?(tok) } opt_tok_index = tokens.index(opt_tok) opt.consume(response, tokens) elsif opt.required? response.add_error("required", opt.name) end end end |