Class: GLI::Flag
- Inherits:
-
CommandLineOption
- Object
- CommandLineToken
- CommandLineOption
- GLI::Flag
- Defined in:
- lib/gli/flag.rb
Overview
Defines a flag, which is to say a switch that takes an argument
Instance Attribute Summary collapse
-
#argument_name ⇒ Object
readonly
Name of the argument that user configured.
-
#must_match ⇒ Object
readonly
Regexp that is used to see if the flag’s argument matches.
-
#type ⇒ Object
readonly
Type to which we want to cast the values.
Attributes inherited from CommandLineOption
#associated_command, #default_value
Attributes inherited from CommandLineToken
#aliases, #description, #long_description, #name
Instance Method Summary collapse
-
#all_forms(joiner = ', ') ⇒ Object
Returns a string of all possible forms of this flag.
- #arguments_for_option_parser ⇒ Object
-
#initialize(names, options) ⇒ Flag
constructor
Creates a new option.
- #safe_default_value ⇒ Object
Methods inherited from CommandLineOption
Methods inherited from CommandLineToken
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
- :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.
29 30 31 32 33 34 35 36 |
# File 'lib/gli/flag.rb', line 29 def initialize(names,) super(names,) @argument_name = [:arg_name] || "arg" @default_value = [:default_value] @must_match = [:must_match] @type = [:type] @mask = [:mask] end |
Instance Attribute Details
#argument_name ⇒ Object (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_match ⇒ Object (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 |
#type ⇒ Object (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.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gli/flag.rb', line 56 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_parser ⇒ Object
46 47 48 49 50 51 |
# File 'lib/gli/flag.rb', line 46 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 |
#safe_default_value ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/gli/flag.rb', line 38 def safe_default_value if @mask "********" else default_value end end |