Class: TermUtils::AP::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/term_utils/ap/syntax.rb

Overview

Represents the argument list syntax. It holds a list of parameters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSyntax

Constructs a new Syntax.



27
28
29
# File 'lib/term_utils/ap/syntax.rb', line 27

def initialize
  @parameters = []
end

Instance Attribute Details

#parametersArray<Parameter>

Returns:



24
25
26
# File 'lib/term_utils/ap/syntax.rb', line 24

def parameters
  @parameters
end

Instance Method Details

#define_parameter(id = nil, opts = {}, &block) ⇒ Parameter

Creates and adds a new Parameter.

Parameters:

  • id (Symbol, nil) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (Symbol)
  • :min_occurs (Integer)
  • :max_occurs (Integer)

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/term_utils/ap/syntax.rb', line 54

def define_parameter(id = nil, opts = {}, &block)
  if id
    param = @parameters.find { |p| p.id == id }
    if param
      block.call(param) if block
      return param
    end

    opts[:id] = id
  end
  new_parameter = TermUtils::AP::Parameter.new(opts)
  @parameters << new_parameter
  block.call(new_parameter) if block
  new_parameter
end

#fetch_parametersArray

Fetches all flagged parameters and unflagged parameters.

Returns:

  • (Array)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/term_utils/ap/syntax.rb', line 72

def fetch_parameters
  unflagged_params = []
  flagged_params = {}
  shortcut_flags = {}
  @parameters.each do |p|
    if p.flagged?
      # Flagged
      p.flags.each do |f|
        flagged_params[f.label] = p
        if f.long?
          shortcut_flags["#{f.label}="] = f
        else
          shortcut_flags[f.label] = f
        end
      end
    else
      # Unflagged
      unflagged_params << p
    end
  end
  [unflagged_params, flagged_params, shortcut_flags]
end

#finalize!(opts = {}) ⇒ nil

Finalizes this one. Internal use.

Returns:

  • (nil)

Raises:



40
41
42
43
44
45
# File 'lib/term_utils/ap/syntax.rb', line 40

def finalize!(opts = {})
  opts[:anonymous] = 0 unless opts.key? :anonymous
  opts[:flag_labels] = []
  @parameters.each { |p| p.finalize!(opts) }
  nil
end

#initialize_dup(other) ⇒ Object

For dup method.



32
33
34
35
# File 'lib/term_utils/ap/syntax.rb', line 32

def initialize_dup(other)
  super(other)
  @parameters = other.parameters.map(&:dup) if other.parameters
end