Class: GLI::Flag

Inherits:
CommandLineOption show all
Defined in:
lib/gli/flag.rb

Overview

Defines a flag, which is to say a switch that takes an argument

Instance Attribute Summary collapse

Attributes inherited from CommandLineOption

#associated_command

Attributes inherited from CommandLineToken

#aliases, #description, #long_description, #name

Instance Method Summary collapse

Methods inherited from CommandLineOption

name_as_string

Methods inherited from CommandLineToken

#<=>, #names_and_aliases

Constructor Details

#initialize(names, options) ⇒ Flag

Creates a new option

names

Array of symbols or strings representing the names of this switch

options

hash of options:

:desc

the short description

:long_desc

the long description

:default_value

the default value of this option

:arg_name

the name of the flag’s argument, default is “arg”

:must_match

a regexp that the flag’s value must match

:type

a class to convert the value to

:required

if true, this flag must be specified on the command line

:multiple

if true, flag may be used multiple times and values are stored in an array

:mask

if true, the default value of this flag will not be output in the help. This is useful for password flags where you might not want to show it on the command-line.



31
32
33
34
35
36
37
38
39
# File 'lib/gli/flag.rb', line 31

def initialize(names,options)
  super(names,options)
  @argument_name = options[:arg_name] || "arg"
  @must_match = options[:must_match]
  @type = options[:type]
  @mask = options[:mask]
  @required = options[:required]
  @multiple = options[:multiple]
end

Instance Attribute Details

#argument_nameObject (readonly)

Name of the argument that user configured



14
15
16
# File 'lib/gli/flag.rb', line 14

def argument_name
  @argument_name
end

#must_matchObject (readonly)

Regexp that is used to see if the flag’s argument matches



8
9
10
# File 'lib/gli/flag.rb', line 8

def must_match
  @must_match
end

#typeObject (readonly)

Type to which we want to cast the values



11
12
13
# File 'lib/gli/flag.rb', line 11

def type
  @type
end

Instance Method Details

#all_forms(joiner = ', ') ⇒ Object

Returns a string of all possible forms of this flag. Mostly intended for printing to the user.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gli/flag.rb', line 85

def all_forms(joiner=', ')
  forms = all_forms_a
  string = forms.join(joiner)
  if forms[-1] =~ /^\-\-/
    string += '='
  else
    string += ' '
  end
  string += @argument_name
  return string
end

#arguments_for_option_parserObject



75
76
77
78
79
80
# File 'lib/gli/flag.rb', line 75

def arguments_for_option_parser
  args = all_forms_a.map { |name| "#{name} VAL" }
  args << @must_match if @must_match
  args << @type if @type
  args
end

#default_valueObject

The default value for this flag. Uses the value passed if one is set; otherwise uses ‘[]` if the flag support multiple arguments and `nil` if it does not.



67
68
69
70
71
72
73
# File 'lib/gli/flag.rb', line 67

def default_value
  if @default_value
    @default_value
  elsif @multiple
    []
  end
end

#multiple?Boolean

True if the flag may be used multiple times.

Returns:

  • (Boolean)


47
48
49
# File 'lib/gli/flag.rb', line 47

def multiple?
  @multiple
end

#required?Boolean

True if this flag is required on the command line

Returns:

  • (Boolean)


42
43
44
# File 'lib/gli/flag.rb', line 42

def required?
  @required
end

#safe_default_valueObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gli/flag.rb', line 51

def safe_default_value
  if @mask
    "********"
  else
    # This uses @default_value instead of the `default_value` method because
    # this method is only used for display, and for flags that may be passed
    # multiple times, we want to display whatever is set in the code as the
    # the default, or the string "none" rather than displaying an empty
    # array.
    @default_value
  end
end