module CommandKit::Options::Parser

Adds an [OptionParser] to the command class and automatically parses options before calling `main`.

[OptionParser]: rubydoc.info/stdlib/optparse/OptionParser

include CommandKit::OptParser

def initialize
  @opts.on('-c','--custom','Custom option') do
    @custom = true
  end
end

def main(*argv)
  if @custom
    puts "Custom mode enabled"
  end
end

Attributes

option_parser[R]

The option parser.

@return [OptionParser]

@api semipublic

Public Class Methods

new(**kwargs) click to toggle source

The option parser.

@return [OptionParser]

@api public

Calls superclass method
# File lib/command_kit/options/parser.rb, line 75
def initialize(**kwargs)
  super(**kwargs)

  @option_parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{usage}"

    opts.separator ''
    opts.separator 'Options:'

    opts.on_tail('-h','--help','Print help information') do
      help
      exit(0)
    end
  end
end

Public Instance Methods

help() click to toggle source

@see help_options

@api public

# File lib/command_kit/options/parser.rb, line 249
def help
  help_options
end
help_options() click to toggle source

Prints the `–help` output.

@api semipublic

# File lib/command_kit/options/parser.rb, line 240
def help_options
  puts option_parser
end
main(argv=[]) click to toggle source

Parses the options and passes any additional non-option arguments to the superclass'es `#main` method.

@param [Array<String>] argv

The given arguments Array.

@return [Integer]

The exit status code.

@api public

Calls superclass method CommandKit::Main#main
# File lib/command_kit/options/parser.rb, line 103
def main(argv=[])
  super(parse_options(argv))
rescue SystemExit => system_exit
  system_exit.status
end
on_ambiguous_argument(error) click to toggle source

Place-holder method for handling `OptionParser::AmbiguousArgument` exceptions.

@param [OptionParser::AmbiguousArgument] error

@see on_parse_error

@api semipublic

# File lib/command_kit/options/parser.rb, line 231
def on_ambiguous_argument(error)
  on_parse_error(error)
end
on_ambiguous_option(error) click to toggle source

Place-holder method for handling `OptionParser::AmbiguousOption` exceptions.

@param [OptionParser::AmbiguousOption] error

@see on_parse_error

@api semipublic

# File lib/command_kit/options/parser.rb, line 175
def on_ambiguous_option(error)
  on_parse_error(error)
end
on_invalid_argument(error) click to toggle source

Place-holder method for handling `OptionParser::InvalidArgument` exceptions.

@param [OptionParser::InvalidArgument] error

@see on_parse_error

@api semipublic

# File lib/command_kit/options/parser.rb, line 189
def on_invalid_argument(error)
  on_parse_error(error)
end
on_invalid_option(error) click to toggle source

Place-holder method for handling `OptionParser::InvalidOption` exceptions.

@param [OptionParser::InvalidOption] error

@see on_parse_error

@api semipublic

# File lib/command_kit/options/parser.rb, line 161
def on_invalid_option(error)
  on_parse_error(error)
end
on_missing_argument(error) click to toggle source

Place-holder method for handling `OptionParser::MissingArgument` exceptions.

@param [OptionParser::MissingArgument] error

@see on_parse_error

@api semipublic

# File lib/command_kit/options/parser.rb, line 203
def on_missing_argument(error)
  on_parse_error(error)
end
on_needless_argument(error) click to toggle source

Place-holder method for handling `OptionParser::NeedlessArgument` exceptions.

@param [OptionParser::NeedlessArgument] error

@see on_parse_error

@api semipublic

# File lib/command_kit/options/parser.rb, line 217
def on_needless_argument(error)
  on_parse_error(error)
end
on_parse_error(error) click to toggle source

Prints an option parsing error.

@param [OptionParser::ParseError] error

The error from `OptionParser`.

@api semipublic

# File lib/command_kit/options/parser.rb, line 146
def on_parse_error(error)
  print_error("#{command_name}: #{error.message}")
  print_error("Try '#{command_name} --help' for more information.")
  exit(1)
end
parse_options(argv) click to toggle source

Parses the given options.

@param [Array<String>] argv

The given command-line arguments.

@return [Array<String>]

The remaining non-option arguments.

@api semipublic

# File lib/command_kit/options/parser.rb, line 120
def parse_options(argv)
  option_parser.parse(argv)
rescue OptionParser::InvalidOption => error
  on_invalid_option(error)
rescue OptionParser::AmbiguousOption => error
  on_ambiguous_option(error)
rescue OptionParser::InvalidArgument => error
  on_invalid_argument(error)
rescue OptionParser::MissingArgument => error
  on_missing_argument(error)
rescue OptionParser::NeedlessArgument => error
  on_needless_argument(error)
rescue OptionParser::AmbiguousArgument => error
  on_ambiguous_argument(error)
rescue OptionParser::ParseError => error
  on_parse_error(error)
end