Module: Climate::ParsingMethods
Instance Method Summary collapse
- #arg(*args) ⇒ Object
- #cli_arguments ⇒ Object
- #cli_options ⇒ Object
- #has_arguments? ⇒ Boolean
- #has_options? ⇒ Boolean
- #help_banner(out = $stdout) ⇒ Object
- #opt(*args) ⇒ Object
- #parse(arguments, command = self) ⇒ Object
- #parse_arguments(args, command = self) ⇒ Object
- #stop_on(args) ⇒ Object
- #trollop_parser ⇒ Object
Instance Method Details
#arg(*args) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/climate/parser.rb', line 5 def arg(*args) arg = Argument.new(*args) raise DefinitionError, "can not define more arguments after a multi " + " argument" if cli_arguments.any?(&:multi?) raise DefinitionError, "can not define a required argument after an " + "optional one" if cli_arguments.any?(&:optional?) && arg.required? cli_arguments << arg end |
#cli_arguments ⇒ Object
116 |
# File 'lib/climate/parser.rb', line 116 def cli_arguments ; @cli_arguments ||= [] ; end |
#cli_options ⇒ Object
115 |
# File 'lib/climate/parser.rb', line 115 def ; @cli_options ||= [] ; end |
#has_arguments? ⇒ Boolean
119 |
# File 'lib/climate/parser.rb', line 119 def has_arguments? ; not cli_arguments.empty? ; end |
#has_options? ⇒ Boolean
118 |
# File 'lib/climate/parser.rb', line 118 def ; not .empty? ; end |
#help_banner(out = $stdout) ⇒ Object
46 47 48 |
# File 'lib/climate/parser.rb', line 46 def (out=$stdout) trollop_parser.educate(out) end |
#opt(*args) ⇒ Object
18 19 20 |
# File 'lib/climate/parser.rb', line 18 def opt(*args) << Option.new(*args) end |
#parse(arguments, command = self) ⇒ Object
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 |
# File 'lib/climate/parser.rb', line 89 def parse(arguments, command=self) parser = self.trollop_parser begin = parser.parse(arguments) rescue Trollop::CommandlineError => e if (m = /unknown argument '(.+)'/.match(e.)) raise UnexpectedArgumentError.new(m[1], command) elsif (m = /option (.+) must be specified/.match(e.)) raise MissingArgumentError.new(m[1], command) else raise end end # it would get weird if we allowed arguments alongside options, so # lets keep it one or t'other arguments, leftovers = if @stop_on [{}, parser.leftovers] else [self.parse_arguments(parser.leftovers), []] end [arguments, , leftovers] end |
#parse_arguments(args, command = self) ⇒ Object
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/climate/parser.rb', line 50 def parse_arguments(args, command=self) arg_list = cli_arguments if arg_list.none?(&:multi?) && args.size > arg_list.size raise UnexpectedArgumentError.new("#{args.size} for #{arg_list.size}", command) end # mung the last arguments to appear as one for multi args, this is fairly # ugly - thank heavens for unit tests if arg_list.last && arg_list.last.multi? multi_arg = arg_list.last last_args = args[(arg_list.size - 1)..-1] || [] # depending on the number of args that were supplied, you may get nil # or an empty array because of how slicing works, either way we want nil # if no args were supplied so `required?` detection works below args = args[0...(arg_list.size - 1)] + [last_args.empty?? nil : last_args].compact end arg_list.zip(args).map do |argument, arg_value| if argument.required? && arg_value.nil? raise MissingArgumentError.new(argument.name, command) end # empty list is nil for multi arg arg_value = [] if argument.multi? && arg_value.nil? # no arg given is different to an empty arg if arg_value.nil? {} else {argument.name => arg_value} end end.inject {|a,b| a.merge(b) } || {} end |
#stop_on(args) ⇒ Object
22 23 24 |
# File 'lib/climate/parser.rb', line 22 def stop_on(args) @stop_on = args end |
#trollop_parser ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/climate/parser.rb', line 26 def trollop_parser parser = Trollop::Parser.new parser.stop_on @stop_on if cli_arguments.size > 0 parser. "" max_length = cli_arguments.map { |h| h.name.to_s.length }.max cli_arguments.each do |argument| parser.(" " + argument.name.to_s.rjust(max_length) + " - #{argument.description}") end end parser. "" .each do |option| option.add_to(parser) end parser end |