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 run(*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:



246
247
248
# File 'lib/command_kit/options/parser.rb', line 246

def help
  help_options
end

#help_optionsObject

Prints the --help output.



237
238
239
# File 'lib/command_kit/options/parser.rb', line 237

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
# 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.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.



100
101
102
103
104
# File 'lib/command_kit/options/parser.rb', line 100

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:



228
229
230
# File 'lib/command_kit/options/parser.rb', line 228

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:



172
173
174
# File 'lib/command_kit/options/parser.rb', line 172

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:



186
187
188
# File 'lib/command_kit/options/parser.rb', line 186

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:



158
159
160
# File 'lib/command_kit/options/parser.rb', line 158

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:



200
201
202
# File 'lib/command_kit/options/parser.rb', line 200

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:



214
215
216
# File 'lib/command_kit/options/parser.rb', line 214

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.



143
144
145
146
147
# File 'lib/command_kit/options/parser.rb', line 143

def on_parse_error(error)
  print_error(error.message)
  stderr.puts("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.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/command_kit/options/parser.rb', line 117

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