| Class | Command::Parser |
| In: |
lib/command/parser.rb
|
| Parent: | Object |
| argv | [R] | |
| command | [R] | |
| definition | [R] | |
| options | [R] |
# File lib/command/parser.rb, line 16
16: def initialize(definition, argv)
17: @definition = definition
18: @argv = argv
19: @affix = [] # arguments after '--'
20: if i = argv.index('--')
21: @affix = argv[(i+1)..-1]
22: @arguments = argv.first(i)
23: else
24: @arguments = @argv.dup
25: end
26: @options = {}
27: end
# File lib/command/parser.rb, line 29
29: def argument(name)
30: position = @definition.argument_position[name]
31: raise ArgumentError, "No argument #{name.inspect} available" unless position
32: arguments[position]
33: end
# File lib/command/parser.rb, line 43
43: def command!
44: if @definition.commands_by_name.include?(@arguments.first)
45: @command = @arguments.shift
46: else
47: @command = @definition.default_command
48: end
49:
50: @command
51: end
# File lib/command/parser.rb, line 53
53: def normalize_arguments!
54: options = @definition[@command].options_by_flag
55: parse_argv = @arguments
56: @arguments = []
57: while arg = parse_argv.shift
58: if arg =~ /\A-([^-]{2,})/ then
59: flags = $1
60: until flags.empty?
61: flag = flags.slice!(0,1)
62: if opt = options["-#{flag}"] then
63: case opt.necessity
64: when :required
65: @arguments << "-#{flag}"
66: @arguments << flags unless flags.empty?
67: flags = ""
68: when :optional
69: raise "Invalid option - can't merge short options with optional arguments"
70: when :none
71: @arguments << "-#{flag}"
72: else
73: raise "Unknown necessity #{opt.necessity.inspect} for option #{opt}"
74: end
75: else
76: @arguments << "-#{flag}#{flags}"
77: end
78: end
79: else
80: @arguments << arg
81: end
82: end
83: end
# File lib/command/parser.rb, line 85
85: def options!(*flags)
86: ignore_invalid_options = flags.delete(:ignore_invalid_options)
87: options = @definition[@command].options_by_flag # options available to this command
88: env = @definition[@command].env_by_variable # options available to this command
89: defaults = @definition[@command].default_options # options available to this command
90:
91: normalize_arguments!
92:
93: parse_argv = @arguments
94: @arguments = []
95:
96: defaults.each do |key, default|
97: @options[key] = default unless @options.has_key?(key)
98: end
99:
100: env.each do |key, definition|
101: if ENV.has_key?(key) && !@options.has_key?(key) then
102: mapped = options[definition.name]
103: value = mapped.process!(ENV[key])
104: @options[key] = value
105: end
106: end
107:
108: while arg = parse_argv.shift
109: if option = options[arg] then
110: case option.necessity
111: when :required
112: value = option.process!(parse_argv.shift)
113: when :optional
114: if parse_argv.first && parse_argv.first !~ /\A-/ then
115: value = option.process!(parse_argv.shift)
116: else
117: value = true
118: end
119: when :none
120: value = true
121: end
122: @options[option.name] = (arg == option.negated) ? !value : value
123: elsif arg =~ /\A-/ then
124: raise "Invalid option #{arg}" unless ignore_invalid_options
125: @arguments << arg
126: else
127: @arguments << arg
128: end
129: end
130:
131: @options
132: end