Module: Clitopic::Parser::OptParser

Included in:
Command::Base
Defined in:
lib/clitopic/parser/option_parser.rb

Instance Method Summary collapse

Instance Method Details

#check_all_requiredObject



78
79
80
81
82
# File 'lib/clitopic/parser/option_parser.rb', line 78

def check_all_required
  check_required(self.cmd_options)
  check_required(self.topic.topic_options) if !self.topic.nil?
  check_required(Clitopic::Commands.global_options)
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 options[opt[:name]].nil?
        message = "Missing required option: #{opt[:args][0]}"
        $stderr.puts(Clitopic::Helpers.format_with_bang(message) + "\n\n")
        Clitopic::Commands.run(self.fullname, ["--help"])
      end
    end
  end
end

#helpObject



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

#parserObject



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.banner = self.banner unless self.banner.nil?
    parser.base.long.delete('version')
    process_options(parser, self.cmd_options)

    if !self.topic.nil?
      self.inherited_options.each do |cmd|
        cmd = self.topic.commands[cmd.to_s]
        parser.separator ""
        parser.separator "'#{cmd.fullname}' inherited options"
        process_options(parser, cmd.cmd_options)
      end

      if self.topic.topic_options.size > 0
        parser.separator ""
        parser.separator "Topic options"
        process_options(parser, self.topic.topic_options)
      end
    end

    parser.separator ""
    parser.separator "Common options"
    process_options(parser, Clitopic::Commands.global_options)

    # 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 process_options(parser, opts)
  opts.each do |option|
    parser.on(*option[:args]) do |value|
      if option[:proc]
        option[:proc].call(value)
      end
      name = option[:name]
      if options.has_key?(name) && options[name].is_a?(Array)
        options[name] += value
      else
        puts "Warning: already defined option: --#{option[:name]} #{options[name]}" if options.has_key?(name) && option[:default] == nil
        options[name] = value
      end
    end
  end
  options
end