Class: TabTab::Definition::Flag

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

Instance Attribute Summary collapse

Attributes inherited from Base

#contents, #definition_block, #parent

Instance Method Summary collapse

Methods inherited from Base

#[], #autocompletable?, #command, #completions_of_contents, #current_token, #definition_type?, #filtered_completions, #find_active_definition_for_last_token, #yield_definition_block, #yield_result_block

Constructor Details

#initialize(definition, flags, description, &block) ⇒ Flag

Returns a new instance of Flag.



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

def initialize(definition, flags, description, &block)
  @flags       = flags.map { |flag| flag.to_s }
  @description = description
  super definition, &block
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



3
4
5
# File 'lib/tabtab/definitions/flag.rb', line 3

def definition
  @definition
end

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/tabtab/definitions/flag.rb', line 3

def description
  @description
end

#flagsObject (readonly)

Returns the value of attribute flags.



3
4
5
# File 'lib/tabtab/definitions/flag.rb', line 3

def flags
  @flags
end

Instance Method Details

#definition_typeObject



10
11
12
# File 'lib/tabtab/definitions/flag.rb', line 10

def definition_type
  :flag
end

#matches_token?(cmd_line_token) ⇒ Boolean

Determines if current token matches this command’s flags

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/tabtab/definitions/flag.rb', line 32

def matches_token?(cmd_line_token)
  return false unless cmd_line_token
  flags.include? cmd_line_token.gsub(/^-*/,'')
end

#own_completionsObject

convert flags into –flag or -f based on length of value



25
26
27
28
29
# File 'lib/tabtab/definitions/flag.rb', line 25

def own_completions
  flags.map do |flag|
    flag.size > 1 ? "--#{flag}" : "-#{flag}"
  end
end

#tokens_consumedObject

How many tokens/parts of a command-line expression does this Flag consume For example, the following consume 1 token:

--simple
-s

The following consumes 2 tokens:

--port 1234


20
21
22
# File 'lib/tabtab/definitions/flag.rb', line 20

def tokens_consumed
  definition_block.nil? ? 1 : 2
end