Class: Mothership::Parser
- Inherits:
-
Object
- Object
- Mothership::Parser
- Defined in:
- lib/mothership/parser.rb
Instance Attribute Summary collapse
-
#given ⇒ Object
readonly
Returns the value of attribute given.
Instance Method Summary collapse
-
#initialize(command, given = {}) ⇒ Parser
constructor
A new instance of Parser.
-
#parse_arguments(args) ⇒ Object
- FOO
- BAR
-
FIZZ BUZZ: 1 2 => :fizz => 1, :buzz => 2 1 2 3 => :foo => 1, :fizz => 2, :buzz => 3 1 2 3 4 => :foo => 1, :bar => 2, :fizz => 3, :buzz => 4.
- #parse_argv(argv) ⇒ Object
- #parse_flags(argv, find_in = nil) ⇒ Object
Constructor Details
#initialize(command, given = {}) ⇒ Parser
Returns a new instance of Parser.
5 6 7 8 |
# File 'lib/mothership/parser.rb', line 5 def initialize(command, given = {}) @command = command @given = given end |
Instance Attribute Details
#given ⇒ Object (readonly)
Returns the value of attribute given.
3 4 5 |
# File 'lib/mothership/parser.rb', line 3 def given @given end |
Instance Method Details
#parse_arguments(args) ⇒ Object
- FOO
- BAR
-
FIZZ BUZZ:
1 2 => :fizz => 1, :buzz => 2
1 2 3 => :foo => 1, :fizz => 2, :buzz => 3
1 2 3 4 => :foo => 1, :bar => 2, :fizz => 3, :buzz => 4
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mothership/parser.rb', line 93 def parse_arguments(args) total = @command.arguments.size required = 0 optional = 0 @command.arguments.each do |arg| case arg[:type] when :optional optional += 1 when :splat break else required += 1 end end parse_optionals = args.size - required @command.arguments.each do |arg| name = arg[:name] next if @given.key? name input = @command.inputs[name] case arg[:type] when :splat @given[name] = [] until args.empty? @given[name] << args.shift end when :optional if parse_optionals > 0 && val = args.shift @given[name] = val parse_optionals -= 1 end else if val = args.shift @given[name] = val elsif !input[:default] && !input[:interact] raise MissingArgument.new(@command, name) end end end raise ExtraArguments.new(@command, args) unless args.empty? end |
#parse_argv(argv) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/mothership/parser.rb', line 10 def parse_argv(argv) args = parse_flags(argv.dup) parse_arguments(args) @given end |
#parse_flags(argv, find_in = nil) ⇒ 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mothership/parser.rb', line 18 def parse_flags(argv, find_in = nil) local = nil args = [] until argv.empty? flag = normalize_flag(argv.shift, argv) name = local && local.flags[flag] || @command.flags[flag] unless name # assume first argument is subcommand if args.empty? && find_in && !local local = find_in[flag.gsub("-", "_").to_sym] end args << flag next end # skip flags defined by the local cmd if local && local.inputs[name] args << flag next end input = @command.inputs[name] if argv.first == "--ask" @given[name] = :interact argv.shift next end case input[:type] when :bool, :boolean if argv.first == "false" || argv.first == "true" @given[name] = argv.shift else @given[name] = "true" end when :float, :floating if !argv.empty? && argv.first =~ /^[0-9]+(\.[0-9]*)?$/ @given[name] = argv.shift else raise TypeMismatch.new(@command, name, "floating") end when :integer, :number, :numeric if !argv.empty? && argv.first =~ /^[0-9]+$/ @given[name] = argv.shift else raise TypeMismatch.new(@command, name, "numeric") end else arg = if argv.empty? || argv.first.start_with?("-") "" else argv.shift end @given[name] = if input[:argument] == :splat arg.split(",") else arg end end end args end |