module CommandKit::Options::ClassMethods

Defines class-level methods.

Public Instance Methods

option(name,**kwargs,&block) click to toggle source

Defines an {Option option} for the class.

@param [Symbol] name

The option name.

@param [Hash{Symbol => Object}] kwargs

Keyword arguments.

@option kwargs [String, nil] short

Optional short-flag for the option.

@option kwargs [String, nil] long

Optional explicit long-flag for the option.

@option kwargs [Boolean] equals

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

@option kwargs [Hash{Symbol => Object}, true, false, nil] value

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

@option value [Class, Hash, Array, Regexp] type

The type of the option's value.

@option value [String, nil] usage

The usage string for the option's value.

@option kwargs [String] desc

The description for the option.

@yield [(value)]

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

@yieldparam [Object, nil] value

The parsed option value.

@return [Option]

@raise [TypeError]

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

@example Define an option:

option :foo, desc: "Foo option"

@example With a custom short option:

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

@example With a custom long option:

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

@example With a custom usage string:

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

@example With a custom block:

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

@example With a custom type:

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

@example With a default value:

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

@example With a required value:

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

@example With a custom option value Hash map:

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

@example With a custom option value Array enum:

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

@example With a custom option value Regexp:

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

@api public

# File lib/command_kit/options.rb, line 182
def option(name,**kwargs,&block)
  options[name] = Option.new(name,**kwargs,&block)
end
options() click to toggle source

All defined options for the command class.

@return [Hash{Symbol => Option}]

@api semipublic

# File lib/command_kit/options.rb, line 76
def options
  @options ||= if superclass.kind_of?(ClassMethods)
                 superclass.options.dup
               else
                 {}
               end
end