Module: Climate::ParsingMethods
Instance Method Summary collapse
- #arg(*args) ⇒ Object
- #check_arguments(args, command = self) ⇒ Object
- #cli_arguments ⇒ Object
- #cli_options ⇒ Object
- #has_arguments? ⇒ Boolean
- #has_options? ⇒ Boolean
- #help_banner(out = $stdout) ⇒ Object
- #opt(*args) ⇒ Object
- #parse(arguments) ⇒ Object
- #stop_on(args) ⇒ Object
- #trollop_parser ⇒ Object
Instance Method Details
#arg(*args) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/climate/parser.rb', line 5 def arg(*args) raise DefinitionError, "can not define a required argument after an " + "optional one" if cli_arguments.any?(&:optional?) cli_arguments << Argument.new(*args) end |
#check_arguments(args, command = self) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/climate/parser.rb', line 44 def check_arguments(args, command=self) if args.size > cli_arguments.size raise UnexpectedArgumentError.new(command, "#{args.size} for #{cli_arguments.size}") end cli_arguments.zip(args).map do |argument, arg_value| if argument.required? && (arg_value.nil? || arg_value.empty?) raise MissingArgumentError.new(command, argument.name) end # 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 |
#cli_arguments ⇒ Object
81 |
# File 'lib/climate/parser.rb', line 81 def cli_arguments ; @cli_arguments ||= [] ; end |
#cli_options ⇒ Object
80 |
# File 'lib/climate/parser.rb', line 80 def ; ||= [] ; end |
#has_arguments? ⇒ Boolean
84 |
# File 'lib/climate/parser.rb', line 84 def has_arguments? ; not cli_arguments.empty? ; end |
#has_options? ⇒ Boolean
83 |
# File 'lib/climate/parser.rb', line 83 def ; not .empty? ; end |
#help_banner(out = $stdout) ⇒ Object
40 41 42 |
# File 'lib/climate/parser.rb', line 40 def (out=$stdout) trollop_parser.educate(out) end |
#opt(*args) ⇒ Object
12 13 14 |
# File 'lib/climate/parser.rb', line 12 def opt(*args) << Option.new(*args) end |
#parse(arguments) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/climate/parser.rb', line 64 def parse(arguments) parser = self.trollop_parser = parser.parse(arguments) # 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.check_arguments(parser.leftovers), []] end [arguments, , leftovers] end |
#stop_on(args) ⇒ Object
16 17 18 |
# File 'lib/climate/parser.rb', line 16 def stop_on(args) @stop_on = args end |
#trollop_parser ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/climate/parser.rb', line 20 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 |