Module: CommandKit::Options

Extended by:
ModuleMethods
Includes:
Arguments, Parser
Included in:
Command, Commands, Quiet, Verbose
Defined in:
lib/command_kit/options.rb,
lib/command_kit/options/quiet.rb,
lib/command_kit/options/option.rb,
lib/command_kit/options/parser.rb,
lib/command_kit/options/verbose.rb,
lib/command_kit/options/version.rb,
lib/command_kit/options/option_value.rb

Overview

Provides a thin DSL for defining options as attributes.

Examples

include CommandKit::Options

option :foo, short: '-f',
             value: {type: String},
             desc:  "Foo option"

option :bar, short: '-b',
             value: {
               type:  String,
               usage: 'STR:STR:...'
             },
             desc: "Bar option" do |arg|
  @bar = arg.split(':')
end

Multi-line Descriptions

option :opt, value: {type: String},
             desc: [
                     'line1',
                     'line2',
                     '...'
                   ]

Option Categories

option :opt1, value: {type: String},
              category: 'Foo Category',
              desc: 'Option 1'

option :opt2, value: {type: String},
              category: 'Foo Category',
              desc: 'Option 2'

initialize and using instance variables

option :number, value: {type: Integer},
                desc: 'Numbers' do |num|
  @numbers << num
end

def initialize(**kwargs)
  super(**kwargs)

  @numbers = []
end

Defined Under Namespace

Modules: ClassMethods, ModuleMethods, Parser, Quiet, Verbose, Version Classes: Option, OptionValue

Constant Summary

Constants included from Printing

Printing::EOL

Instance Attribute Summary collapse

Attributes included from Parser

#option_parser

Attributes included from CommandName

#command_name

Instance Method Summary collapse

Methods included from ModuleMethods

included

Methods included from Parser

#help_options, #main, #on_ambiguous_argument, #on_ambiguous_option, #on_invalid_argument, #on_invalid_option, #on_missing_argument, #on_needless_argument, #on_parse_error, #parse_options

Methods included from Parser::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

#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

Methods included from Arguments

#help_arguments, #main

Methods included from Arguments::ModuleMethods

#included

Instance Attribute Details

#optionsHash{Symbol => Object} (readonly)

Hash of parsed option values.

Returns:

  • (Hash{Symbol => Object})


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

def options
  @options
end

Instance Method Details

#helpObject

Overrides the default help method and calls CommandKit::Options::Parser#help_options and Arguments#help_arguments.



261
262
263
264
# File 'lib/command_kit/options.rb', line 261

def help
  help_options
  help_arguments
end

#initialize(options: {}, **kwargs) ⇒ Object

Note:

The CommandKit::Options::Parser#option_parser will populate #options and also call any option blocks with the parsed option values.

Initializes #options and populates the option parser.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    Optional prepopulated options hash.



247
248
249
250
251
252
253
# File 'lib/command_kit/options.rb', line 247

def initialize(options: {}, **kwargs)
  @options = options

  super(**kwargs)

  define_option_categories()
end