No release in over a year
Implementation of validators for the ruby OptionParser lib. Mainly used in the CMSScanner gem to define the cli options available
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.6
~> 13.0
~> 3.10.0
~> 1.3.0
~> 0.21.0
~> 1.21.0

Runtime

>= 5.2, < 6.2.0
>= 2.5, < 2.9
 Project Readme

OptParseValidator

Gem Version Build Coverage Status Code Climate

Installation

gem install opt_parse_validator

Usage Example

# test.rb

require 'opt_parse_validator'

begin
  # For constructor 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
  parsed_cli_options = OptParseValidator::OptParser.new.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),
    OptParseValidator::OptAlias.new(['--alias', '-a'], alias_for: '--test -o file.txt')
  ).results

  p parsed_cli_options
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

It is worth to note that when aliases are used, it is recommended to provide them first in the CLI. Otherwise, they might override user-suplied cli options. For example, using the options above, ruby test.rb -m aa -o override.txt --alias won't have the desired effect (to have --output as override.txt), but ruby test.rb --alias -m aa -o override.txt will.

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)
  • :advanced (Whether or or not the option is an advanced one. If set to true, the option won't be displayed in the #simple_help, but will in the #full_help)

Available Validators & Associated Attributes:

  • Alias:
    • :alias_for (mandatory)
  • 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