Module: Climate::ParsingMethods
Instance Method Summary collapse
- #arg(*args) ⇒ Object
- #cli_arguments ⇒ Object
- #cli_options ⇒ Object
- #conflicting_options ⇒ Object
- #conflicts(*args) ⇒ Object
- #dependent_options ⇒ Object
- #depends(*args) ⇒ Object
- #has_argument?(name) ⇒ Boolean
- #has_arguments? ⇒ Boolean
- #has_multi_argument?(name) ⇒ Boolean
- #has_options? ⇒ Boolean
- #has_required_argument?(name) ⇒ Boolean
- #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
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/climate/parser.rb', line 97 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
214 |
# File 'lib/climate/parser.rb', line 214 def cli_arguments ; @cli_arguments ||= [] ; end |
#cli_options ⇒ Object
213 |
# File 'lib/climate/parser.rb', line 213 def ; @cli_options ||= [] ; end |
#conflicting_options ⇒ Object
215 |
# File 'lib/climate/parser.rb', line 215 def ; @conflicting_options ||= [] ; end |
#conflicts(*args) ⇒ Object
117 118 119 |
# File 'lib/climate/parser.rb', line 117 def conflicts(*args) << args end |
#dependent_options ⇒ Object
216 |
# File 'lib/climate/parser.rb', line 216 def ; @dependent_options ||= [] ; end |
#depends(*args) ⇒ Object
121 122 123 |
# File 'lib/climate/parser.rb', line 121 def depends(*args) << args end |
#has_argument?(name) ⇒ Boolean
221 222 223 |
# File 'lib/climate/parser.rb', line 221 def has_argument?(name) cli_arguments.map(&:name).include?(name) end |
#has_arguments? ⇒ Boolean
219 |
# File 'lib/climate/parser.rb', line 219 def has_arguments? ; not cli_arguments.empty? ; end |
#has_multi_argument?(name) ⇒ Boolean
229 230 231 |
# File 'lib/climate/parser.rb', line 229 def has_multi_argument?(name) cli_arguments.select(&:multi?).map(&:name).include?(name) end |
#has_options? ⇒ Boolean
218 |
# File 'lib/climate/parser.rb', line 218 def ; not .empty? ; end |
#has_required_argument?(name) ⇒ Boolean
225 226 227 |
# File 'lib/climate/parser.rb', line 225 def has_required_argument?(name) cli_arguments.select(&:required?).map(&:name).include?(name) end |
#opt(*args) ⇒ Object
109 110 111 |
# File 'lib/climate/parser.rb', line 109 def opt(*args) << Option.new(*args) end |
#parse(arguments, command = self) ⇒ Object
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 |
# File 'lib/climate/parser.rb', line 182 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) elsif /.+ conflicts with .+/.match(e.) raise ConflictingOptionError.new(e., command) elsif /.+ requires .+/.match(e.) raise MissingArgumentError.new(e., command) else raise CommandError.new(e., command) 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
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 |
# File 'lib/climate/parser.rb', line 143 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| arg_value ||= argument.default 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
113 114 115 |
# File 'lib/climate/parser.rb', line 113 def stop_on(args) @stop_on = args end |
#trollop_parser ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/climate/parser.rb', line 125 def trollop_parser Trollop::Parser.new.tap do |parser| parser.stop_on @stop_on .each do |option| option.add_to(parser) end .each do |conflicting| parser.conflicts(*conflicting) end .each do |dependent| parser.depends(*dependent) end end end |