Class: OptParseValidator::OptParser
- Inherits:
-
OptionParser
- Object
- OptionParser
- OptParseValidator::OptParser
- Defined in:
- lib/opt_parse_validator.rb
Overview
Validator
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 ⇒ Void
- #options_files ⇒ OptParseValidator::OptionsFiles
-
#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 57 58 |
# 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 # The default value is not validated as provided by devs # and should be set to the correct format/value directly @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 StandardError => 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
65 66 67 68 |
# File 'lib/opt_parse_validator.rb', line 65 def check_option(opt) raise Error, "The option is not an OptBase, #{opt.class} supplied" unless opt.is_a?(OptBase) raise Error, "The option #{opt.to_sym} is already used !" if @symbols_used.include?(opt.to_sym) end |
#load_options_files ⇒ Void
83 84 85 86 87 88 89 90 91 |
# File 'lib/opt_parse_validator.rb', line 83 def files_data = .parse @opts.each do |opt| next unless files_data.key?(opt.to_sym) @results[opt.to_sym] = opt.normalize(opt.validate(files_data[opt.to_sym].to_s)) end end |
#options_files ⇒ OptParseValidator::OptionsFiles
94 95 96 |
# File 'lib/opt_parse_validator.rb', line 94 def ||= OptionsFiles.new end |
#post_processing ⇒ Void
Ensure that all required options are supplied Should be overriden to modify the behavior
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/opt_parse_validator.rb', line 102 def post_processing @opts.each do |opt| if opt.required? raise 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(', ')}" raise NoRequiredOption, fail_msg unless opt.required_unless.any? do |sym| @results.key?(sym) end end end |
#results(argv = default_argv) ⇒ Hash
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/opt_parse_validator.rb', line 71 def results(argv = default_argv) parse!(argv) post_processing @results rescue StandardError => e # Raise it as an OptParseValidator::Error if not already one raise e.is_a?(Error) ? e.class : Error, e. end |