Class: ShellOpts::Grammar::Option

Inherits:
Node
  • Object
show all
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

Attributes inherited from Node

#key

Instance Method Summary collapse

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

#labelObject (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_namesObject (readonly)

List of long names (incl. ‘–’)



11
12
13
# File 'lib/shellopts/grammar/option.rb', line 11

def long_names
  @long_names
end

#short_namesObject (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

Returns:

  • (Boolean)


37
# File 'lib/shellopts/grammar/option.rb', line 37

def argument?() @flags[:argument] || false end

#dumpObject

: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

#flagsObject

List of flags (Symbol)



14
# File 'lib/shellopts/grammar/option.rb', line 14

def flags() @flags.keys end

#float?Boolean

Returns:

  • (Boolean)


41
# File 'lib/shellopts/grammar/option.rb', line 41

def float?() argument? && @flags[:float] || false end

#integer?Boolean

Returns:

  • (Boolean)


40
# File 'lib/shellopts/grammar/option.rb', line 40

def integer?() argument? && @flags[:integer] || false end

#namesObject

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

Returns:

  • (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

Returns:

  • (Boolean)


36
# File 'lib/shellopts/grammar/option.rb', line 36

def repeated?() @flags[:repeated] || false end

#string?Boolean

Returns:

  • (Boolean)


39
# File 'lib/shellopts/grammar/option.rb', line 39

def string?() argument? && !integer? && !float? end