Module: CommandKit::Options

Extended by:
ModuleMethods
Includes:
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

initialize and using ivars

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, #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, #help_usage, #usage

Methods included from Usage::ModuleMethods

#included

Methods included from Help

#help

Methods included from Help::ModuleMethods

#included

Methods included from CommandName::ModuleMethods

#included

Instance Attribute Details

#optionsHash{Symbol => Object} (readonly)

Hash of parsed option values.

Returns:

  • (Hash{Symbol => Object})


193
194
195
# File 'lib/command_kit/options.rb', line 193

def options
  @options
end

Instance Method Details

#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 pre-populated options hash.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/command_kit/options.rb', line 208

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

  super(**kwargs)

  self.class.options.each_value do |option|
    option_parser.on(*option.usage,option.type,option.desc) do |arg,*captures|
      @options[option.name] = if arg.nil?
                                option.default_value
                              else
                                arg
                              end

      if option.block
        instance_exec(*arg,*captures,&option.block)
      end
    end
  end
end