-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathoption_parser.cr
501 lines (453 loc) · 14.8 KB
/
option_parser.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
88
89
90
91
92
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# `OptionParser` is a class for command-line options processing. It supports:
#
# * Short and long modifier style options (example: `-h`, `--help`)
# * Passing arguments to the flags (example: `-f filename.txt`)
# * Subcommands
# * Automatic help message generation
#
# Run `crystal` for an example of a CLI built with `OptionParser`.
#
# NOTE: To use `OptionParser`, you must explicitly import it with `require "option_parser"`
#
# Short example:
#
# ```
# require "option_parser"
#
# upcase = false
# destination = "World"
#
# OptionParser.parse do |parser|
# parser.banner = "Usage: salute [arguments]"
# parser.on("-u", "--upcase", "Upcases the salute") { upcase = true }
# parser.on("-t NAME", "--to=NAME", "Specifies the name to salute") { |name| destination = name }
# parser.on("-h", "--help", "Show this help") do
# puts parser
# exit
# end
# parser.invalid_option do |flag|
# STDERR.puts "ERROR: #{flag} is not a valid option."
# STDERR.puts parser
# exit(1)
# end
# end
#
# destination = destination.upcase if upcase
# puts "Hello #{destination}!"
# ```
#
# # Subcommands
#
# `OptionParser` also supports subcommands.
#
# Short example:
#
# ```
# require "option_parser"
#
# verbose = false
# salute = false
# welcome = false
# name = "World"
# parser = OptionParser.new do |parser|
# parser.banner = "Usage: example [subcommand] [arguments]"
# parser.on("salute", "Salute a name") do
# salute = true
# parser.banner = "Usage: example salute [arguments]"
# parser.on("-t NAME", "--to=NAME", "Specify the name to salute") { |_name| name = _name }
# end
# parser.on("welcome", "Print a greeting message") do
# welcome = true
# parser.banner = "Usage: example welcome"
# end
# parser.on("-v", "--verbose", "Enabled verbose output") { verbose = true }
# parser.on("-h", "--help", "Show this help") do
# puts parser
# exit
# end
# end
#
# parser.parse
#
# if salute
# STDERR.puts "Saluting #{name}" if verbose
# puts "Hello #{name}"
# elsif welcome
# STDERR.puts "Welcoming #{name}" if verbose
# puts "Welcome!"
# else
# puts parser
# exit(1)
# end
# ```
class OptionParser
class Exception < ::Exception
end
class InvalidOption < Exception
def initialize(option)
super("Invalid option: #{option}")
end
end
class MissingOption < Exception
def initialize(option)
super("Missing option: #{option}")
end
end
# :nodoc:
enum FlagValue
Required
Optional
None
end
# :nodoc:
record Handler,
value_type : FlagValue,
block : String ->
# Creates a new parser, with its configuration specified in the block,
# and uses it to parse the passed *args* (defaults to `ARGV`).
#
# Refer to `#gnu_optional_args?` for the behaviour of the named parameter.
def self.parse(args = ARGV, *, gnu_optional_args : Bool = false, &) : self
parser = OptionParser.new(gnu_optional_args: gnu_optional_args)
yield parser
parser.parse(args)
parser
end
# Creates a new parser.
#
# Refer to `#gnu_optional_args?` for the behaviour of the named parameter.
def initialize(*, @gnu_optional_args : Bool = false)
@flags = [] of String
@handlers = Hash(String, Handler).new
@stop = false
@missing_option = ->(option : String) { raise MissingOption.new(option) }
@invalid_option = ->(option : String) { raise InvalidOption.new(option) }
end
# Creates a new parser, with its configuration specified in the block.
#
# Refer to `#gnu_optional_args?` for the behaviour of the named parameter.
def self.new(*, gnu_optional_args : Bool = false, &)
new(gnu_optional_args: gnu_optional_args).tap { |parser| yield parser }
end
# Returns whether the GNU convention is followed for optional arguments.
#
# If true, any optional argument must follow the preceding flag in the same
# token immediately, without any space inbetween:
#
# ```
# require "option_parser"
#
# OptionParser.parse(%w(-a1 -a 2 -a --b=3 --b 4), gnu_optional_args: true) do |parser|
# parser.on("-a", "--b [x]", "optional") { |x| p x }
# parser.unknown_args { |args, _| puts "Remaining: #{args}" }
# end
# ```
#
# Prints:
#
# ```text
# "1"
# ""
# ""
# "3"
# ""
# Remaining: ["2", "4"]
# ```
#
# Without `gnu_optional_args: true`, prints the following instead:
#
# ```text
# "1"
# "2"
# "--b=3"
# "4"
# Remaining: []
# ```
property? gnu_optional_args : Bool
# Establishes the initial message for the help printout.
# Typically, you want to write here the name of your program,
# and a one-line template of its invocation.
#
# Example:
#
# ```
# require "option_parser"
#
# parser = OptionParser.new
# parser.banner = "Usage: crystal [command] [switches] [program file] [--] [arguments]"
# ```
setter banner : String?
# Establishes a handler for a *flag* or subcommand.
#
# Flags must start with a dash or double dash. They can also have
# an optional argument, which will get passed to the block.
# Each flag has a description, which will be used for the help message.
#
# Subcommands are any *flag* passed which does not start with a dash. They
# cannot take arguments. When a subcommand is parsed, all subcommands are
# removed from the OptionParser, simulating a "tree" of subcommands. All flags
# remain valid. For a longer example, see the examples at the top of the page.
#
# Examples of valid flags:
#
# * `-a`, `-B`
# * `--something-longer`
# * `-f FILE`, `--file FILE`, `--file=FILE` (these will yield the passed value to the block as a string)
#
# Examples of valid subcommands:
#
# * `foo`, `run`
def on(flag : String, description : String, &block : String ->)
append_flag flag, description
flag, value_type = parse_flag_definition(flag)
@handlers[flag] = Handler.new(value_type, block)
end
# Establishes a handler for a pair of short and long flags.
#
# See the other definition of `on` for examples. This method does not support
# subcommands.
def on(short_flag : String, long_flag : String, description : String, &block : String ->)
check_starts_with_dash short_flag, "short_flag", allow_empty: true
check_starts_with_dash long_flag, "long_flag"
append_flag "#{short_flag}, #{long_flag}", description
short_flag, short_value_type = parse_flag_definition(short_flag)
long_flag, long_value_type = parse_flag_definition(long_flag)
# Pick the "most required" argument type between both flags
if short_value_type.required? || long_value_type.required?
value_type = FlagValue::Required
elsif short_value_type.optional? || long_value_type.optional?
value_type = FlagValue::Optional
else
value_type = FlagValue::None
end
handler = Handler.new(value_type, block)
@handlers[short_flag] = @handlers[long_flag] = handler
end
private def parse_flag_definition(flag : String)
case flag
when /\A--(\S+)\s+\[\S+\]\z/
{"--#{$1}", FlagValue::Optional}
when /\A--(\S+)(\s+|\=)(\S+)?\z/
{"--#{$1}", FlagValue::Required}
when /\A--\S+\z/
# This can't be merged with `else` otherwise /-(.)/ matches
{flag, FlagValue::None}
when /\A-(.)\s*\[\S+\]\z/
{flag[0..1], FlagValue::Optional}
when /\A-(.)\s+\S+\z/, /\A-(.)\s+\z/, /\A-(.)\S+\z/
{flag[0..1], FlagValue::Required}
else
# This happens for -f without argument
{flag, FlagValue::None}
end
end
# Adds a separator, with an optional header message, that will be used to
# print the help. The separator is placed between the flags registered (`#on`)
# before, and the flags registered after the call.
#
# This way, you can group the different options in an easier to read way.
def separator(message = "") : Nil
@flags << message.to_s
end
# Sets a handler for regular arguments that didn't match any of the setup options.
#
# You typically use this to get the main arguments (not modifiers)
# that your program expects (for example, filenames). The default behaviour
# is to do nothing. The arguments can also be extracted from the *args* array
# passed to `#parse` after parsing.
def unknown_args(&@unknown_args : Array(String), Array(String) ->)
end
# Sets a handler for when a option that expects an argument wasn't given any.
#
# You typically use this to display a help message.
# The default behaviour is to raise `MissingOption`.
def missing_option(&@missing_option : String ->)
end
# Sets a handler for option arguments that didn't match any of the setup options.
#
# You typically use this to display a help message.
# The default behaviour is to raise `InvalidOption`.
def invalid_option(&@invalid_option : String ->)
end
# Sets a handler which runs before each argument is parsed. This callback is
# not passed flag arguments. For example, `--foo=foo_arg --bar bar_arg` would
# pass `--foo=foo_arg` and `--bar` to the callback only.
#
# You typically use this to implement advanced option parsing behaviour such
# as treating all options after a filename differently (along with `#stop`).
def before_each(&@before_each : String ->)
end
# Stops the current parse and returns immediately, leaving the remaining flags
# unparsed. This is treated identically to `--` being inserted *behind* the
# current parsed flag.
def stop : Nil
@stop = true
end
# Returns all the setup options, formatted in a help message.
def to_s(io : IO) : Nil
if banner = @banner
io << banner
io << '\n'
end
@flags.join io, '\n'
end
private def append_flag(flag, description)
indent = " " * 37
description = description.gsub("\n", "\n#{indent}")
if flag.size >= 33
@flags << " #{flag}\n#{indent}#{description}"
else
@flags << " #{flag}#{" " * (33 - flag.size)}#{description}"
end
end
private def check_starts_with_dash(arg, name, allow_empty = false)
return if allow_empty && arg.empty?
unless arg.starts_with?('-')
raise ArgumentError.new("Argument '#{name}' (#{arg.inspect}) must start with a dash (-)")
end
end
private def with_preserved_state(&)
old_flags = @flags.clone
old_handlers = @handlers.clone
old_banner = @banner
old_unknown_args = @unknown_args
old_missing_option = @missing_option
old_invalid_option = @invalid_option
old_before_each = @before_each
begin
yield
ensure
@flags = old_flags
@handlers = old_handlers
@stop = false
@banner = old_banner
@unknown_args = old_unknown_args
@missing_option = old_missing_option
@invalid_option = old_invalid_option
@before_each = old_before_each
end
end
# Parses the passed *args* (defaults to `ARGV`), running the handlers associated to each option.
def parse(args = ARGV) : Nil
with_preserved_state do
# List of indexes in `args` which have been handled and must be deleted
handled_args = [] of Int32
double_dash_index = nil
arg_index = 0
while arg_index < args.size
arg = args[arg_index]
if @stop
double_dash_index = arg_index - 1
@stop = false
break
end
if before_each = @before_each
before_each.call(arg)
end
# -- means to stop parsing arguments
if arg == "--"
double_dash_index = arg_index
handled_args << arg_index
break
end
if arg.starts_with?("--")
value_index = arg.index('=')
if value_index
flag = arg[0...value_index]
value = arg[value_index + 1..-1]
else
flag = arg
value = nil
end
elsif arg.starts_with?('-')
if arg.size > 2
flag = arg[0..1]
value = arg[2..-1]
else
flag = arg
value = nil
end
else
flag = arg
value = nil
end
# Fetch handler of the flag.
# If value is given even though handler does not take value, it is invalid, then it is skipped.
if (handler = @handlers[flag]?) && !(handler.value_type.none? && value)
handled_args << arg_index
if !value
case handler.value_type
in FlagValue::Required
value = args[arg_index + 1]?
if value
handled_args << arg_index + 1
arg_index += 1
else
@missing_option.call(flag)
end
in FlagValue::Optional
unless gnu_optional_args?
value = args[arg_index + 1]?
if value && !@handlers.has_key?(value)
handled_args << arg_index + 1
arg_index += 1
else
value = nil
end
end
in FlagValue::None
# do nothing
end
end
# If this is a subcommand (flag not starting with -), delete all
# subcommands since they are no longer valid.
unless flag.starts_with?('-')
@handlers.select! { |k, _| k.starts_with?('-') }
@flags.select!(&.starts_with?(" -"))
end
handler.block.call(value || "")
end
arg_index += 1
end
# We're about to delete all the unhandled arguments in args so double_dash_index
# is about to change. Arguments are only handled before "--", so we're deleting
# nothing after "--", which means it's index is decremented by handled_args.size.
# But actually we also added "--" itself to handled_args so we change it's index
# by one less.
if double_dash_index
double_dash_index -= handled_args.size - 1
end
# After argument parsing, delete handled arguments from args.
# We reverse so that we delete args from
handled_args.reverse!
i = 0
args.reject! do
# handled_args is sorted in reverse so we know that i <= handled_args.last
handled = i == handled_args.last?
# Maintain the i <= handled_args.last invariant
handled_args.pop if handled
i += 1
handled
end
# Since we've deleted all handled arguments, `args` is all unknown arguments
# which we split by the index of any double dash argument
if unknown_args = @unknown_args
if double_dash_index
before_dash = args[0...double_dash_index]
after_dash = args[double_dash_index..-1]
else
before_dash = args
after_dash = [] of String
end
unknown_args.call(before_dash, after_dash)
end
# We consider any remaining arguments which start with '-' to be invalid
args.each_with_index do |arg, index|
break if double_dash_index && index >= double_dash_index
if arg.starts_with?('-') && arg != "-"
@invalid_option.call(arg)
end
end
end
end
end