Class: OptParseValidator::OptParser
- Inherits:
-
OptionParser
- Object
- OptionParser
- OptParseValidator::OptParser
- Defined in:
- lib/opt_parse_validator.rb,
lib/opt_parse_validator/options_file.rb
Overview
TODO
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#symbols_used ⇒ Object
readonly
Returns the value of attribute symbols_used.
Instance Method Summary collapse
- #add(*options) ⇒ void
- #add_option(opt) ⇒ void
-
#check_option(opt) ⇒ void
Ensures the opt given is valid.
-
#initialize(banner = nil, width = 32, indent = ' ' * 4) ⇒ OptParser
constructor
A new instance of OptParser.
- #load_options_files ⇒ Object
- #options_files ⇒ Object
-
#post_processing ⇒ Void
Ensure that all required options are supplied Should be overriden to modify the behavior.
- #results(argv = default_argv) ⇒ Hash
Constructor Details
#initialize(banner = nil, width = 32, indent = ' ' * 4) ⇒ OptParser
Returns a new instance of OptParser.
20 21 22 23 24 25 26 |
# File 'lib/opt_parse_validator.rb', line 20 def initialize( = nil, width = 32, indent = ' ' * 4) @results = {} @symbols_used = [] @opts = [] super(, width, indent) end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
18 19 20 |
# File 'lib/opt_parse_validator.rb', line 18 def opts @opts end |
#symbols_used ⇒ Object (readonly)
Returns the value of attribute symbols_used.
18 19 20 |
# File 'lib/opt_parse_validator.rb', line 18 def symbols_used @symbols_used end |
Instance Method Details
#add(*options) ⇒ void
This method returns an undefined value.
31 32 33 |
# File 'lib/opt_parse_validator.rb', line 31 def add(*) .each { |option| add_option(option) } end |
#add_option(opt) ⇒ void
This method returns an undefined value.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/opt_parse_validator.rb', line 38 def add_option(opt) check_option(opt) @opts << opt @symbols_used << opt.to_sym # Set the default option value if it exists @results[opt.to_sym] = opt.default unless opt.default.nil? on(*opt.option) do |arg| begin @results[opt.to_sym] = opt.normalize(opt.validate(arg)) rescue => e # Adds the long option name to the message # And raises it as an OptParseValidator::Error if not already one # e.g --proxy Invalid Scheme format. raise e.is_a?(Error) ? e.class : Error, "#{opt.to_long} #{e}" end end end |
#check_option(opt) ⇒ void
This method returns an undefined value.
Ensures the opt given is valid
63 64 65 66 |
# File 'lib/opt_parse_validator.rb', line 63 def check_option(opt) fail Error, "The option is not an OptBase, #{opt.class} supplied" unless opt.is_a?(OptBase) fail Error, "The option #{opt.to_sym} is already used !" if @symbols_used.include?(opt.to_sym) end |
#load_options_files ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/opt_parse_validator/options_file.rb', line 11 def files_data = {} .each do |file| data = parse_file(file) files_data.merge!(data) if data end @opts.each do |opt| # Annoying thing: the hash returned from parse_file is a string-full {"key"=>"value"} # and not a ruby hash {key: value} :/ As a result, symbol.to_s has to be used next unless files_data.key?(opt.to_sym.to_s) @results[opt.to_sym] = opt.normalize(opt.validate(files_data[opt.to_sym.to_s])) end end |
#options_files ⇒ Object
7 8 9 |
# File 'lib/opt_parse_validator/options_file.rb', line 7 def @options_files ||= [] end |
#post_processing ⇒ Void
Ensure that all required options are supplied Should be overriden to modify the behavior
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/opt_parse_validator.rb', line 81 def post_processing @opts.each do |opt| if opt.required? fail NoRequiredOption, "The option #{opt} is required" unless @results.key?(opt.to_sym) end next if opt.required_unless.empty? next if @results.key?(opt.to_sym) fail_msg = "One of the following options is required: #{opt}, #{opt.required_unless.join(', ')}" fail NoRequiredOption, fail_msg unless opt.required_unless.any? do |sym| @results.key?(sym) end end end |
#results(argv = default_argv) ⇒ Hash
69 70 71 72 73 74 75 |
# File 'lib/opt_parse_validator.rb', line 69 def results(argv = default_argv) self.parse!(argv) post_processing @results end |