Class: TabTab::Definition::Root

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

Instance Attribute Summary collapse

Attributes inherited from Base

#contents, #definition_block, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #autocompletable?, #command, #completions_of_contents, #definition_type?, #filtered_completions, #find_active_definition_for_last_token, #flags, #own_completions, #tokens_consumed, #yield_definition_block, #yield_result_block

Constructor Details

#initialize(app_name, options = {}, &block) ⇒ Root

Returns a new instance of Root.



9
10
11
12
13
# File 'lib/tabtab/definitions/root.rb', line 9

def initialize(app_name, options = {}, &block)
  @app_name = app_name
  super(nil, &block)
  import_help_flags(options[:import]) if options[:import]
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



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

def app_name
  @app_name
end

#current_tokenObject (readonly)

Returns the value of attribute current_token.



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

def current_token
  @current_token
end

Class Method Details

.named(app_name, options = {}, &block) ⇒ Object



5
6
7
# File 'lib/tabtab/definitions/root.rb', line 5

def self.named(app_name, options = {}, &block)
  self.new(app_name, options, &block)
end

Instance Method Details

#default(description = nil, &block) ⇒ Object

Example usage:

c.default do
  %w[possible values following command name]
end

which map to example command-line expressions:

myapp this_command possible
myapp this_command value
myapp this_command following


47
48
49
# File 'lib/tabtab/definitions/root.rb', line 47

def default(description=nil, &block)
  contents << TabTab::Definition::Default.new(self, description, &block)
end

#definition_typeObject



35
36
37
# File 'lib/tabtab/definitions/root.rb', line 35

def definition_type
  :root
end

#extract_completions(previous_token, current_token, global_config = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tabtab/definitions/root.rb', line 15

def extract_completions(previous_token, current_token, global_config = {})
  @current_token = current_token
  @global_config = global_config
  current = find_active_definition_for_last_token(previous_token) || self
  current = (current.parent || self) if current.tokens_consumed == 1
  completions = current.filtered_completions(current_token)
  grouped = {:long => [], :short => [], :command => []}
  grouped = completions.inject(grouped) do |mem, token|
    if token =~ /^--/
      mem[:long] << token
    elsif token =~ /^-/
      mem[:short] << token unless hide_short_flags?
    else
      mem[:command] << token
    end
    mem
  end
  grouped[:command].sort + grouped[:long].sort + grouped[:short].sort
end

#hide_short_flags?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/tabtab/definitions/root.rb', line 66

def hide_short_flags?
  @global_config[:shortflags] == 'disable'
end

#import_help_flags(help_flag) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/tabtab/definitions/root.rb', line 56

def import_help_flags(help_flag)
  help_flag = "--help" unless help_flag.is_a?(String)
  imported_flags = TabTab::Completions::External.new(app_name, help_flag).extract
  imported_flags.each do |flag|
    flag.gsub!(/^-*/, '')
    next unless flag.size > 0
    self.flag(flag.to_sym)
  end
end

#matches_token?(cmd_line_token) ⇒ Boolean

Determines if current token matches the app name

Returns:

  • (Boolean)


52
53
54
# File 'lib/tabtab/definitions/root.rb', line 52

def matches_token?(cmd_line_token)
  cmd_line_token == app_name
end