Module: Clitopic::Parser::OptParser
- Included in:
- Command::Base
- Defined in:
- lib/clitopic/parser/option_parser.rb
Instance Method Summary collapse
- #check_all_required ⇒ Object
- #check_required(opts) ⇒ Object
- #help ⇒ Object
- #parse(args) ⇒ Object
- #parser ⇒ Object
- #process_options(parser, opts) ⇒ Object
Instance Method Details
#check_all_required ⇒ Object
78 79 80 81 82 |
# File 'lib/clitopic/parser/option_parser.rb', line 78 def check_all_required check_required(self.) check_required(self.topic.) if !self.topic.nil? check_required(Clitopic::Commands.) end |
#check_required(opts) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/clitopic/parser/option_parser.rb', line 66 def check_required (opts) opts.each do |opt| if opt[:required] == true if [opt[:name]].nil? = "Missing required option: #{opt[:args][0]}" $stderr.puts(Clitopic::Helpers.format_with_bang() + "\n\n") Clitopic::Commands.run(self.fullname, ["--help"]) end end end end |
#help ⇒ Object
62 63 64 |
# File 'lib/clitopic/parser/option_parser.rb', line 62 def help parser.to_s end |
#parse(args) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/clitopic/parser/option_parser.rb', line 84 def parse(args) @invalid_options ||= [] parser.parse!(args) check_all_required @arguments = args Clitopic::Commands.validate_arguments!(@invalid_options) return @options, @arguments rescue OptionParser::InvalidOption => ex @invalid_options << ex.args.first retry end |
#parser ⇒ Object
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 |
# File 'lib/clitopic/parser/option_parser.rb', line 25 def parser @opt_parser = OptionParser.new do |parser| # remove OptionParsers Officious['version'] to avoid conflicts # see: https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L814 parser. = self. unless self..nil? parser.base.long.delete('version') (parser, self.) if !self.topic.nil? self..each do |cmd| cmd = self.topic.commands[cmd.to_s] parser.separator "" parser.separator "'#{cmd.fullname}' inherited options" (parser, cmd.) end if self.topic..size > 0 parser.separator "" parser.separator "Topic options" (parser, self.topic.) end end parser.separator "" parser.separator "Common options" (parser, Clitopic::Commands.) # No argument, shows at tail. This will print an options summary. # Try it and see! parser.on_tail("-h", "--help", "Show this message") do puts parser exit 0 end end return @opt_parser end |
#process_options(parser, opts) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/clitopic/parser/option_parser.rb', line 7 def (parser, opts) opts.each do |option| parser.on(*option[:args]) do |value| if option[:proc] option[:proc].call(value) end name = option[:name] if .has_key?(name) && [name].is_a?(Array) [name] += value else puts "Warning: already defined option: --#{option[:name]} #{[name]}" if .has_key?(name) && option[:default] == nil [name] = value end end end end |