OptParseValidator

Gem Version Build Status Coverage Status Code Climate Dependency Status

Installation

gem install opt_parse_validator

Usage Example

# test.rb

require 'opt_parse_validator'

# For contructor options, such as setting a banner, the summary width and indent,
# see http://ruby-doc.org/stdlib-2.4.2/libdoc/optparse/rdoc/OptionParser.html#method-c-new
parser = OptParseValidator::OptParser.new

parser.add(
  OptParseValidator::OptString.new(['-m', '--mandatory PARAM', 'A Mandatory CLI option'], required: true),
  OptParseValidator::OptBoolean.new(['--test', '-t', 'Option Helper Message']),
  OptParseValidator::OptFilePath.new(['-o', '--output FILE', 'Output to FILE'], writable: true, exists: false)
)

begin
  p parser.results
rescue OptParseValidator::Error => e
  puts 'Parsing Error: ' + e.message
end

Then have a play with ruby test.rb -h ruby test.rb -m hh -t ruby test.rb -t

For more option examples, see

Please Feel free to send Pull Requests to improve this Readme

Global Attributes

Some attributes are available for all Validators:

  • :required (whether or not the associated cli option is required/mandatory - example).
  • :required_unless (like the above, except if the option/s given in this parameter are called in the CLI - example, can be a single symbol or array of symbols)
  • :default (Default value to use if the option is not supplied, the correct format has to be used as it won't go through the validation and normalization processes)
  • :value_if_empty (Value to use if no argument has been supplied for the related option)

Available Validators & Associated Attributes:

  • Array
    • :separator (default: ',')
  • Boolean
  • Choice
    • :choices (mandatory)
    • :case_sensitive
  • Credentials
  • Directory Path
    • :create
    • :exists
    • :executable
    • :readable
    • :writable
  • File Path
    • :create
    • :exists
    • :executable
    • :readable
    • :writable
  • Headers
  • Integer
  • IntegerRange
    • separator (default: '-')
  • MultiChoices
    • choices (mandatory)
    • separator (default: ',')
    • incompatible
  • Positive Integer
  • Path
    • :create
    • :file
    • :directory
    • :exists
    • :executable
    • :readable
    • :writable
  • Proxy
    • :protocols
    • :default_protocol
  • Regexp:
  • SmartList
    • separator (default: ',')
  • String
  • URI
    • :protocols
    • :default_protocol
  • URL
    • :default_protocol