Method: CommandMapper::Command.option

Defined in:
lib/command_mapper/command.rb

.option(flag, name: nil, value: nil, repeats: false, equals: nil, value_in_flag: nil, &block) ⇒ Object

Defines an option for the command.

Examples:

Defining an option:

option '--foo'

Defining an option with a custom name:

option '-F', name: :foo

Defining an option who's value is required:

option '--file', value: true

Defining an option who's value is optional:

option '--file', value: {required: false}

Defining an -Fvalue option:

option '--foo', value: true, value_in_flag: true

Defining an --opt=value option:

option '--foo', equals: true, value: true

Defining an option that can be repeated multiple times:

option '--foo', repeats: true

Defining an option that takes a comma-separated list:

option '--list', value: List.new

Parameters:

  • flag (String)

    The option's command-line flag.

  • name (Symbol, nil) (defaults to: nil)

    The option's name.

  • value (Hash, nil) (defaults to: nil)

    The option's value.

  • repeats (Boolean) (defaults to: false)

    Specifies whether the option can be given multiple times.

  • equals (Boolean) (defaults to: nil)

    Specifies whether the option's flag and value should be separated with a = character.

  • value_in_flag (Boolean) (defaults to: nil)

    Specifies that the value should be appended to the option's flag (ex: -Fvalue).

Options Hash (value:):

  • :required (Boolean)

    Specifies whether the option requires a value or not.

  • :type (Types:Type, Hash, nil)

    The explicit type for the option's value.

Raises:

  • (ArgumentError)

    The option flag conflicts with a pre-existing internal method, or another argument or subcommand.



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/command_mapper/command.rb', line 413

def self.option(flag, name: nil, value: nil, repeats: false,
                      # formatting options
                      equals:        nil,
                      value_in_flag: nil,
                      &block)
  option = Option.new(flag, name:    name,
                            value:   value,
                            repeats: repeats,
                            # formatting options
                            equals:        equals,
                            value_in_flag: value_in_flag,
                            &block)

  if is_internal_method?(option.name)
    if name
      raise(ArgumentError,"option #{flag.inspect} with name #{name.inspect} cannot override the internal method with same name: ##{option.name}")
    else
      raise(ArgumentError,"option #{flag.inspect} maps to method name ##{option.name} and cannot override the internal method with same name: ##{option.name}")
    end
  elsif has_argument?(option.name)
    raise(ArgumentError,"option #{flag.inspect} with name #{option.name.inspect} conflicts with another argument with the same name")
  elsif has_subcommand?(option.name)
    raise(ArgumentError,"option #{flag.inspect} with name #{option.name.inspect} conflicts with another subcommand with the same name")
  end

  self.options[option.name] = option
  attr_accessor option.name
end