Class: ShellOpts::Grammar::Option
- Defined in:
- lib/shellopts/grammar/option.rb
Overview
Models an Option
Sets Node#key to the first long option name if present or else the first short option
Instance Attribute Summary collapse
-
#label ⇒ Object
readonly
Informal name of argument (eg. ‘FILE’).
-
#long_names ⇒ Object
readonly
List of long names (incl. ‘–’).
-
#short_names ⇒ Object
readonly
List of short names (incl. ‘-’).
Attributes inherited from Node
Instance Method Summary collapse
- #argument? ⇒ Boolean
-
#dump ⇒ Object
:nocov:.
-
#flags ⇒ Object
List of flags (Symbol).
- #float? ⇒ Boolean
-
#initialize(short_names, long_names, flags, label = nil) ⇒ Option
constructor
Initialize an option.
- #integer? ⇒ Boolean
-
#names ⇒ Object
Array of option names with short names first and then the long names.
- #optional? ⇒ Boolean
-
#repeated? ⇒ Boolean
Flag query methods.
- #string? ⇒ Boolean
Constructor Details
#initialize(short_names, long_names, flags, label = nil) ⇒ Option
Initialize an option. Short and long names are arrays of the short/long option names (incl. the ‘-’/‘–’ prefix). It is assumed that at least one name is given. Flags is a list of symbolic flags. Allowed flags are :repeated, :argument, :optional, :integer, and :float. Note that there’s no :string flag, it’s status is inferred. label is the optional informal name of the option argument (eg. ‘FILE’) or nil if not present
25 26 27 28 29 30 |
# File 'lib/shellopts/grammar/option.rb', line 25 def initialize(short_names, long_names, flags, label = nil) super((long_names.first || short_names.first).sub(/^-+/, "").to_sym) @short_names, @long_names = short_names, long_names @flags = flags.map { |flag| [flag, true] }.to_h @label = label end |
Instance Attribute Details
#label ⇒ Object (readonly)
Informal name of argument (eg. ‘FILE’). nil if not present
17 18 19 |
# File 'lib/shellopts/grammar/option.rb', line 17 def label @label end |
#long_names ⇒ Object (readonly)
List of long names (incl. ‘–’)
11 12 13 |
# File 'lib/shellopts/grammar/option.rb', line 11 def long_names @long_names end |
#short_names ⇒ Object (readonly)
List of short names (incl. ‘-’)
8 9 10 |
# File 'lib/shellopts/grammar/option.rb', line 8 def short_names @short_names end |
Instance Method Details
#argument? ⇒ Boolean
37 |
# File 'lib/shellopts/grammar/option.rb', line 37 def argument?() @flags[:argument] || false end |
#dump ⇒ Object
:nocov:
44 45 46 47 48 49 50 51 |
# File 'lib/shellopts/grammar/option.rb', line 44 def dump super { puts "short_names: #{short_names.inspect}" puts "long_names: #{long_names.inspect}" puts "flags: #{flags.inspect}" puts "label: #{label.inspect}" } end |
#flags ⇒ Object
List of flags (Symbol)
14 |
# File 'lib/shellopts/grammar/option.rb', line 14 def flags() @flags.keys end |
#float? ⇒ Boolean
41 |
# File 'lib/shellopts/grammar/option.rb', line 41 def float?() argument? && @flags[:float] || false end |
#integer? ⇒ Boolean
40 |
# File 'lib/shellopts/grammar/option.rb', line 40 def integer?() argument? && @flags[:integer] || false end |
#names ⇒ Object
Array of option names with short names first and then the long names
33 |
# File 'lib/shellopts/grammar/option.rb', line 33 def names() @short_names + @long_names end |
#optional? ⇒ Boolean
38 |
# File 'lib/shellopts/grammar/option.rb', line 38 def optional?() argument? && @flags[:optional] || false end |
#repeated? ⇒ Boolean
Flag query methods. Returns true if the flag is present and otherwise nil
36 |
# File 'lib/shellopts/grammar/option.rb', line 36 def repeated?() @flags[:repeated] || false end |
#string? ⇒ Boolean
39 |
# File 'lib/shellopts/grammar/option.rb', line 39 def string?() argument? && !integer? && !float? end |