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

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})


195
196
197
# File 'lib/command_kit/options.rb', line 195

def options
  @options
end

Instance Method Details

#helpObject

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



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

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.



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

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

  super(**kwargs)

  self.class.options.each_value do |option|
    default_value = option.default_value

    @options[option.name] = default_value unless default_value.nil?

    option_parser.on(*option.usage,option.type,option.desc) do |arg,*captures|
      @options[option.name] = arg

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