Module: CommandKit::Options::Parser

Extended by:
ModuleMethods
Includes:
Main, Printing, Usage
Included in:
CommandKit::Options
Defined in:
lib/command_kit/options/parser.rb

Overview

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

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

Defined Under Namespace

Modules: ModuleMethods

Constant Summary

Constants included from Printing

Printing::EOL

Instance Attribute Summary collapse

Attributes included from CommandName

#command_name

Instance Method Summary collapse

Methods included from ModuleMethods

included

Methods included from Printing

#print_error, #print_exception

Methods included from Stdio

#abort, #gets, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout

Methods included from Main

#run

Methods included from Main::ModuleMethods

#included

Methods included from Usage

#help_usage, #usage

Methods included from Usage::ModuleMethods

#included

Methods included from Help::ModuleMethods

#included

Methods included from CommandName::ModuleMethods

#included

Instance Attribute Details

#option_parserOptionParser (readonly)

The option parser.

Returns:

  • (OptionParser)


66
67
68
# File 'lib/command_kit/options/parser.rb', line 66

def option_parser
  @option_parser
end

Instance Method Details

#helpObject

See Also:



251
252
253
# File 'lib/command_kit/options/parser.rb', line 251

def help
  help_options
end

#help_optionsObject

Prints the --help output.



242
243
244
# File 'lib/command_kit/options/parser.rb', line 242

def help_options
  puts option_parser
end

#initialize(**kwargs) ⇒ OptionParser

The option parser.

Returns:

  • (OptionParser)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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

#main(argv = []) ⇒ Integer

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

Parameters:

  • argv (Array<String>) (defaults to: [])

    The given arguments Array.

Returns:

  • (Integer)

    The exit status code.



103
104
105
106
107
# 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) ⇒ Object

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

Parameters:

  • error (OptionParser::AmbiguousArgument)

See Also:



233
234
235
# File 'lib/command_kit/options/parser.rb', line 233

def on_ambiguous_argument(error)
  on_parse_error(error)
end

#on_ambiguous_option(error) ⇒ Object

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

Parameters:

  • error (OptionParser::AmbiguousOption)

See Also:



177
178
179
# File 'lib/command_kit/options/parser.rb', line 177

def on_ambiguous_option(error)
  on_parse_error(error)
end

#on_invalid_argument(error) ⇒ Object

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

Parameters:

  • error (OptionParser::InvalidArgument)

See Also:



191
192
193
# File 'lib/command_kit/options/parser.rb', line 191

def on_invalid_argument(error)
  on_parse_error(error)
end

#on_invalid_option(error) ⇒ Object

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

Parameters:

  • error (OptionParser::InvalidOption)

See Also:



163
164
165
# File 'lib/command_kit/options/parser.rb', line 163

def on_invalid_option(error)
  on_parse_error(error)
end

#on_missing_argument(error) ⇒ Object

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

Parameters:

  • error (OptionParser::MissingArgument)

See Also:



205
206
207
# File 'lib/command_kit/options/parser.rb', line 205

def on_missing_argument(error)
  on_parse_error(error)
end

#on_needless_argument(error) ⇒ Object

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

Parameters:

  • error (OptionParser::NeedlessArgument)

See Also:



219
220
221
# File 'lib/command_kit/options/parser.rb', line 219

def on_needless_argument(error)
  on_parse_error(error)
end

#on_parse_error(error) ⇒ Object

Prints an option parsing error.

Parameters:

  • error (OptionParser::ParseError)

    The error from OptionParser.



148
149
150
151
152
# File 'lib/command_kit/options/parser.rb', line 148

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) ⇒ Array<String>

Parses the given options.

Parameters:

  • argv (Array<String>)

    The given command-line arguments.

Returns:

  • (Array<String>)

    The remaining non-option arguments.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/command_kit/options/parser.rb', line 120

def parse_options(argv)
  begin
    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
end