Class: TabTab::Definition::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tabtab/definitions/base.rb

Direct Known Subclasses

Command, Default, Flag, Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, &block) ⇒ Base

Returns a new instance of Base.



4
5
6
7
8
9
# File 'lib/tabtab/definitions/base.rb', line 4

def initialize(parent, &block)
  @parent           = parent
  @contents         = []
  @definition_block = block
  yield_definition_block
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



2
3
4
# File 'lib/tabtab/definitions/base.rb', line 2

def contents
  @contents
end

#definition_blockObject (readonly)

Returns the value of attribute definition_block.



2
3
4
# File 'lib/tabtab/definitions/base.rb', line 2

def definition_block
  @definition_block
end

#parentObject (readonly)

Returns the value of attribute parent.



2
3
4
# File 'lib/tabtab/definitions/base.rb', line 2

def parent
  @parent
end

Instance Method Details

#[](token) ⇒ Object

Find a direct child/contents definition that supports a given token



49
50
51
# File 'lib/tabtab/definitions/base.rb', line 49

def [](token)
  contents.find { |definition| definition.definition_type != :default && definition.matches_token?(token) }
end

#autocompletable?(cmd_line_or_tokens) ⇒ Boolean

Helper for test frameworks

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tabtab/definitions/base.rb', line 94

def autocompletable?(cmd_line_or_tokens)
  return false if cmd_line_or_tokens.nil?
  tokens = cmd_line_or_tokens.is_a?(String) ? cmd_line_or_tokens.split(/\s/) : cmd_line_or_tokens
  current, *remainder = tokens
  return false unless matches_token?(current) # current cmd-line doesn't match this Definition
  return true if remainder.empty?
  if definition = find_child_definition_by_token(current, remainder)
    return definition.autocompletable?(remainder)
  end
  yield_result_block
end

#command(name, description = "", &block) ⇒ Object

Example usage:

c.command :run
c.command :run, "Runs the good stuff"
c.command :choose, "Runs the good stuff", do
   %w[possible values for the commands argument]
end
c.command :set_speed, "Runs the good stuff", do |run|
   run.flags :a_flag
   run.command :slowly
   run.command :fast
end

which map to example command-line expressions:

myapp run
myapp choose possible
myapp set_speed --a_flag fast


44
45
46
# File 'lib/tabtab/definitions/base.rb', line 44

def command(name, description="", &block)
  contents << TabTab::Definition::Command.new(self, name, description, &block)
end

#completions_of_contentsObject



73
74
75
76
77
78
79
80
# File 'lib/tabtab/definitions/base.rb', line 73

def completions_of_contents
  results = yield_result_block if contents.empty?
  return results if results
  contents.inject([]) do |mem, definition|
    mem << definition.own_completions
    mem
  end.flatten
end

#current_tokenObject

recursively ask parents, until finding root, for current_token being completed



129
130
131
# File 'lib/tabtab/definitions/base.rb', line 129

def current_token
  parent.current_token
end

#definition_type?(def_type) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/tabtab/definitions/base.rb', line 86

def definition_type?(def_type)
  self.definition_type == def_type
end

#filtered_completions(prefix) ⇒ Object



69
70
71
# File 'lib/tabtab/definitions/base.rb', line 69

def filtered_completions(prefix)
  completions_of_contents.grep(/^#{prefix}/)
end

#find_active_definition_for_last_token(last_token) ⇒ Object

Find any child definition that supports a given token



54
55
56
57
58
59
60
# File 'lib/tabtab/definitions/base.rb', line 54

def find_active_definition_for_last_token(last_token)
  self[last_token] || contents.inject([]) do |mem, definition|
    child = definition[last_token]
    mem << child if child
    mem
  end.first
end

#flags(*flags_and_description, &block) ⇒ Object Also known as: flag

Example usage:

c.flags :some_flag
c.flags :some_flag, :s, "a description"
c.flags :another_flag, "a description" do
    %w[possible values for the flag]
end

which map to example command-line expressions:

myapp --some_flag
myapp -s
myapp --another_flag possible
myapp --another_flag values


22
23
24
25
26
# File 'lib/tabtab/definitions/base.rb', line 22

def flags(*flags_and_description, &block)
  description = flags_and_description.pop if flags_and_description.last.is_a?(String)
  flag_names  = flags_and_description
  contents << TabTab::Definition::Flag.new(self, flag_names, description, &block)
end

#own_completionsObject



82
83
84
# File 'lib/tabtab/definitions/base.rb', line 82

def own_completions
  []
end

#tokens_consumedObject

How many tokens/parts of a command-line expression does this Flag consume By default, it is 1 token unless overridden by subclass



65
66
67
# File 'lib/tabtab/definitions/base.rb', line 65

def tokens_consumed
  1
end

#yield_definition_blockObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tabtab/definitions/base.rb', line 106

def yield_definition_block
  if definition_block.nil?
    return
  elsif definition_block.arity == -1 || definition_block.arity == 0
    # these blocks return a result/do lots of work - don't run them now
  elsif definition_block.arity == 1
    definition_block.call self
  else
    raise TabTab::Definition::InvalidDefinitionBlockArguments
  end
end

#yield_result_blockObject



118
119
120
121
122
123
124
125
126
# File 'lib/tabtab/definitions/base.rb', line 118

def yield_result_block
  if definition_block.nil? || definition_block.arity == 1
    nil
  elsif definition_block.arity == -1
    definition_block.call
  else
    raise TabTab::Definition::InvalidDefinitionBlockArguments
  end
end