class OptParseValidator::OptParser
Validator
Attributes
Public Class Methods
Source
# File lib/opt_parse_validator.rb, line 23 def initialize(banner = nil, width = 32, indent = ' ' * 4) @results = {} @symbols_used = [] @opts = [] super end
Calls superclass method
Public Instance Methods
Source
# File lib/opt_parse_validator.rb, line 39 def add(*options) options.each do |option| check_option(option) @opts << option @symbols_used << option.to_sym # Set the default option value if it exists # The default value is not validated as it is provided by devs # and should be set to the correct format/value directly @results[option.to_sym] = option.default unless option.default.nil? register_callback(option) end self end
@param [ Array<OptBase> ] options
@return [ Self ] For chaining new.add.results
Source
# File lib/opt_parse_validator.rb, line 32 def config_files @config_files ||= ConfigFilesLoaderMerger.new end
@return [ OptParseValidator::ConfigFilesLoaderMerger
]
Source
# File lib/opt_parse_validator.rb, line 90 def full_help to_s end
@return [ String ] The full help, with the advanced option/s listed
Source
# File lib/opt_parse_validator.rb, line 58 def results(argv = default_argv) load_config_files 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.message end
@return [ Hash ]
Source
# File lib/opt_parse_validator.rb, line 70 def simple_help help = to_s # Removes all advanced help messages @opts.select(&:advanced?).each do |opt| messages_pattern = // opt.help_messages.each do |msg| messages_pattern = /#{messages_pattern}\s*#{Regexp.escape(msg)}/ end pattern = /\s*#{Regexp.escape(opt.option[0..1].select { |o| o[0] == '-' }.join(', '))}#{messages_pattern}/ help.gsub!(pattern, '') end help end
@return [ String ] The simplified help (without any of the advanced option/s listed)
Protected Instance Methods
Source
# File lib/opt_parse_validator.rb, line 101 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
Ensures the opt given is valid
@param [ OptBase
] opt
@return [ void ]
Source
# File lib/opt_parse_validator.rb, line 125 def load_config_files files_data = config_files.parse @opts.each do |opt| next unless files_data.key?(opt.to_sym) begin @results[opt.to_sym] = opt.normalize(opt.validate(files_data[opt.to_sym].to_s)) 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
@return [ Void ]
Source
# File lib/opt_parse_validator.rb, line 146 def post_processing @opts.each do |opt| raise NoRequiredOption, "The option #{opt.to_long} is required" if opt.required? && !@results.key?(opt.to_sym) next if opt.required_unless.empty? || @results.key?(opt.to_sym) fail_msg = 'One of the following options is required: ' \ "#{opt.to_long}, --#{opt.required_unless.join(', --').tr('_', '-')}" raise NoRequiredOption, fail_msg unless opt.required_unless.any? do |sym| @results.key?(sym) end end end
Ensure that all required options are supplied Should be overriden to modify the behavior
@return [ Void ]
Source
# File lib/opt_parse_validator.rb, line 109 def register_callback(opt) on(*opt.option) do |arg| if opt.alias? parse!(opt.alias_for.split) else @results[opt.to_sym] = opt.normalize(opt.validate(arg)) end 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
@param [ OptBase
] opt
@return [ void ]