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
if args.empty? && find_in && !local
local = find_in[flag.gsub("-", "_").to_sym]
end
args << flag
next
end
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
|