Module: Spud::Args
- Defined in:
- lib/args.rb
Class Method Summary collapse
- .default_options ⇒ Object
- .parse_args(args) ⇒ Object
- .parse_args! ⇒ Object
- .parse_option(options, index, args) ⇒ Object
Class Method Details
.default_options ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/args.rb', line 10 def { debug: false, list: false, version: false } end |
.parse_args(args) ⇒ Object
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 |
# File 'lib/args.rb', line 29 def parse_args(args) = .dup rule_name = nil index = 0 while index < args.length arg = args[index] if arg[0] != '-' rule_name = arg index += 1 break end , index = parse_option(, index, args) end positional = [] keyword = {} while index < args.length arg = args[index] if arg[0] == '-' value = args[index + 1] raise Error, "missing value for arg #{arg}" unless value keyword[arg.sub(/^-+/, '').to_sym] = value index += 2 else positional << arg index += 1 end end { options: , rule: rule_name, positional: positional, keyword: keyword } end |
.parse_args! ⇒ Object
6 7 8 |
# File 'lib/args.rb', line 6 def parse_args! parse_args(ARGV) end |
.parse_option(options, index, args) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/args.rb', line 18 def parse_option(, index, args) arg = args[index] case arg when '-v', '--version' then [.merge(version: true), index + 1] when '-l', '--list' then [.merge(list: true), index + 1] when '--debug' then [.merge(debug: true), index + 1] else raise Error, "invalid option '#{arg}'" end end |