Module: CommandKit::Options::ClassMethods

Defined in:
lib/command_kit/options.rb

Overview

Defines class-level methods.

Instance Method Summary collapse

Instance Method Details

#option(name, **kwargs) {|(value)| ... } ⇒ Option

Defines an option for the class.

Examples:

Define an option:

option :foo, desc: "Foo option"

Define an option with a multi-line description:

option :foo, desc: [
                     "Line 1",
                     "Line 2"
                   ]

Defines multiple options within a category:

option :foo, desc: "Foo option",
             category: 'Other Options'

option :bar, desc: "Bar option",
             category: 'Other Options'

With a custom short option:

option :foo, short: '-f',
             desc: "Foo option"

With a custom long option:

option :foo, short: '--foo-opt',
             desc: "Foo option"

With a custom usage string:

option :foo, value: {usage: 'FOO'},
             desc: "Foo option"

With a custom block:

option :foo, desc: "Foo option" do |value|
  @foo = Foo.new(value)
end

With a custom type:

option :foo, value: {type: Integer},
             desc: "Foo option"

With a default value:

option :foo, value: {type: Integer, default: 1},
             desc: "Foo option"

With a required value:

option :foo, value: {type: String, required: true},
             desc: "Foo option"

With a custom option value Hash map:

option :flag, value: {
                type: {
                  'enabled'  => :enabled,
                  'yes'      => :enabled,
                  'disabled' => :disabled,
                  'no'       => :disabled
                }
              },
              desc: "Flag option"

With a custom option value Array enum:

option :enum, value: {type: %w[yes no]},
              desc: "Enum option"

With a custom option value Regexp:

option :date, value: {type: /(\d+)-(\d+)-(\d{2,4})/},
              desc: "Regexp optin" do |date,d,m,y|
  # ...
end

Parameters:

  • name (Symbol)

    The option name.

  • kwargs (Hash{Symbol => Object})

    Keyword arguments.

  • value (Hash)

    a customizable set of options

Options Hash (**kwargs):

  • short (String, nil)

    Optional short-flag for the option.

  • long (String, nil)

    Optional explicit long-flag for the option.

  • equals (Boolean)

    Specifies whether the option is of the form (--opt=value).

  • value (Hash{Symbol => Object}, true, false, nil)

    Keyword arguments for OptionValue#initialize, or nil if the option has no additional value.

  • desc (String, Array<String>)

    The description for the option.

  • category (String, nil)

    The optional category to group the option under.

Yields:

  • ((value))

    If a block is given, it will be passed the parsed option value.

Yield Parameters:

  • value (Object, nil)

    The parsed option value.

Returns:

Raises:

  • (TypeError)

    The value keyword argument was not a Hash, true, false, or nil.



221
222
223
# File 'lib/command_kit/options.rb', line 221

def option(name,**kwargs,&block)
  options[name] = Option.new(name,**kwargs,&block)
end

#optionsHash{Symbol => Option}

All defined options for the command class.

Returns:



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

def options
  @options ||= if superclass.kind_of?(ClassMethods)
                 superclass.options.dup
               else
                 {}
               end
end