Class: Ame::Options::Undefined

Inherits:
Object
  • Object
show all
Defined in:
lib/ame-1.0/options/undefined.rb

Overview

The options to a method in its undefined state.

Instance Method Summary collapse

Constructor Details

#initializeUndefined

Returns a new instance of Undefined.



6
7
8
9
10
# File 'lib/ame-1.0/options/undefined.rb', line 6

def initialize
  @options = {}
  @ordered = []
  @options_must_precede_arguments = ENV.include? 'POSIXLY_CORRECT'
end

Instance Method Details

#defineOptions

Returns The defined version of the receiver.

Returns:

  • (Options)

    The defined version of the receiver



90
91
92
# File 'lib/ame-1.0/options/undefined.rb', line 90

def define
  Ame::Options.new(@options, @ordered, @options_must_precede_arguments)
end

#flag(short, long, default, description) {|?| ... } ⇒ self

Adds a new Flag to the receiver.

Parameters:

  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Boolean)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If SHORT or LONG have already been defined

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



28
29
30
# File 'lib/ame-1.0/options/undefined.rb', line 28

def flag(short, long, default, description, &validate)
  self << Ame::Flag.new(short, long, default, description, &validate)
end

#include?(name) ⇒ Boolean

Returns True if the receiver has any kind of option named NAME.

Parameters:

  • name (String)

Returns:

  • (Boolean)

    True if the receiver has any kind of option named NAME



85
86
87
# File 'lib/ame-1.0/options/undefined.rb', line 85

def include?(name)
  @options.include? name
end

#multioption(short, long, argument, type, description) {|?| ... } ⇒ self

Adds a new Multioption to the receiver.

Parameters:

  • type (::Class)
  • argument (String)
  • default (Object)
  • argument_default (Object)
  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Object)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If SHORT or LONG have already been defined

  • (ArgumentError)

    If TYPE isn’t one that Ame knows how to parse

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



79
80
81
# File 'lib/ame-1.0/options/undefined.rb', line 79

def multioption(short, long, argument, type, description, &validate)
  self << Ame::Multioption.new(short, long, argument, type, description, &validate)
end

#option(short, long, argument, default, description) {|?| ... } ⇒ self

Adds a new Ame::Option to the receiver.

Parameters:

  • argument (String)
  • default (Object)
  • argument_default (Object)
  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Object)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If SHORT or LONG have already been defined

  • (ArgumentError)

    If the type of DEFAULT isn’t one that Ame knows how to parse

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



68
69
70
# File 'lib/ame-1.0/options/undefined.rb', line 68

def option(short, long, argument, default, description, &validate)
  self << Ame::Option.new(short, long, argument, default, description, &validate)
end

#options_must_precede_argumentsself

Forces options to the method about to be defined to precede any arguments, lest they be seen as arguments. If not given, the behaviour will depend on whether ‘ENV` has been set or not.

Returns:

  • (self)


16
17
18
19
# File 'lib/ame-1.0/options/undefined.rb', line 16

def options_must_precede_arguments
  @options_must_precede_arguments = true
  self
end

#switch(short, long, argument, default, argument_default, description) {|?| ... } ⇒ self

Adds a new Switch to the receiver.

Parameters:

  • argument (String)
  • default (Object)
  • argument_default (Object)
  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Object)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If SHORT or LONG have already been defined

  • (ArgumentError)

    If the type of ARGUMENT_DEFAULT or, if ARGUMENT_DEFAULT is nil, DEFAULT isn’t one that Ame knows how to parse

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



57
58
59
# File 'lib/ame-1.0/options/undefined.rb', line 57

def switch(short, long, argument, default, argument_default, description, &validate)
  self << Ame::Switch.new(short, long, argument, default, argument_default, description, &validate)
end

#toggle(short, long, default, description) {|?| ... } ⇒ self

Adds a new Flag to the receiver. Also adds a --no-LONG flag that’s the inverse of this flag.

Parameters:

  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Boolean)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If LONG is #strip#empty?

  • (ArgumentError)

    If SHORT or LONG have already been defined

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



41
42
43
44
45
46
47
48
# File 'lib/ame-1.0/options/undefined.rb', line 41

def toggle(short, long, default, description, &validate)
  flag = Ame::Flag.new(short, long, default, description, &validate)
  raise ArgumentError, 'long can’t be empty' if flag.long.empty?
  self << flag
  add(Ame::Flag.new('', 'no-%s' % flag.long, nil, description){ |options, argument|
    options[flag.name] = validate ? validate.call(options, !argument) : !argument
  })
end