Class: Miniparse::OptionBroker
- Inherits:
-
Object
- Object
- Miniparse::OptionBroker
- Defined in:
- lib/miniparse/option_broker.rb
Instance Attribute Summary collapse
-
#parsed_values ⇒ Object
readonly
Returns the value of attribute parsed_values.
Instance Method Summary collapse
- #add_option(args, &block) ⇒ Object
- #help_desc ⇒ Object
- #help_usage ⇒ Object
-
#initialize(&help_block) ⇒ OptionBroker
constructor
A new instance of OptionBroker.
-
#parse_argv(argv) ⇒ Object
Unprocessed arguments.
Constructor Details
#initialize(&help_block) ⇒ OptionBroker
9 10 11 12 13 14 15 |
# File 'lib/miniparse/option_broker.rb', line 9 def initialize(&help_block) @parsed_values = {} = {} if help_block add_option(spec: "--help", negatable: false, &help_block) end end |
Instance Attribute Details
#parsed_values ⇒ Object (readonly)
Returns the value of attribute parsed_values.
7 8 9 |
# File 'lib/miniparse/option_broker.rb', line 7 def parsed_values @parsed_values end |
Instance Method Details
#add_option(args, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/miniparse/option_broker.rb', line 17 def add_option(args, &block) opt = new_option(args, &block) # FEATURE duplicate option overwrite the old one # NOTE defining a switch option and a flag option with the same name doesn't work (the first one gets overwritten) if opt.shortable duplicate = .values.collect { |o| (o.shortable && o.name.to_s[0] == opt.name.to_s[0])? o.name : nil } duplicate.compact! unless duplicate.empty? || duplicate.include?(opt.name) raise SyntaxError, "shortable option '#{opt.name}' conflicts with previously defined '" + duplicate.join(', ') + "'" end end [opt.name] = opt end |
#help_desc ⇒ Object
67 68 69 70 |
# File 'lib/miniparse/option_broker.rb', line 67 def help_desc helps = .values.collect { |opt| opt.help_desc } helps.compact.join("\n") end |
#help_usage ⇒ Object
60 61 62 63 64 65 |
# File 'lib/miniparse/option_broker.rb', line 60 def help_usage helps = .values.collect do |opt| opt.help_usage end helps.compact.join(" ") end |
#parse_argv(argv) ⇒ Object
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 |
# File 'lib/miniparse/option_broker.rb', line 34 def parse_argv(argv) av = argv.dup rest_argv = [] while av.size > 0 arg = av.shift opt = check_arg(arg) if opt val = opt.parse_value(arg) if val.nil? && (av.size > 0) && (av[0][0] != '-') new_arg = arg + "=" + av.shift val = opt.parse_value(new_arg) end if val.nil? raise ArgumentError, "#{opt.class} invalid invocation format '#{arg}'" end else if Miniparse.control(:raise_on_unrecognized) && (arg[0] == '-') raise ArgumentError, "unrecognized option '#{arg}'" end rest_argv << arg end end update_parsed_values rest_argv end |