Module: Thor::Completion::Bash::Thor::ClassMethods

Defined in:
lib/thor/completion/bash.rb

Overview

Methods to be mixed as class methods in to Thor::Completion::Bash::Thor.

Instance Method Summary collapse

Instance Method Details

#all_commands_by_ui_name(include_hidden: false) ⇒ Hash<String, Thor::Command>

Get this class’ Thor::Command instances, keyed by how their names will appear in the UI (replace ‘_` with `-`).

Parameters:

  • include_hidden: (Boolean) (defaults to: false)

    When ‘true`, “hidden” commands will also be included.

Returns:



67
68
69
70
71
72
73
# File 'lib/thor/completion/bash.rb', line 67

def all_commands_by_ui_name include_hidden: false
  all_commands.
    each_with_object( {} ) { |(name, cmd), hash|
      next if cmd.hidden? && !include_hidden
      hash[ name.tr( '_', '-' ) ] = cmd
    }
end

#bash_complete(comp_req:, index:) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/thor/completion/bash.rb', line 77

def bash_complete comp_req:, index:
  # Find the command, if any
  
  logger.info __method__,
    comp_req: comp_req,
    index: index,
    word: comp_req.words[index]
  
  scan_index = index
  
  while (comp_req.words[scan_index] || '').start_with? '-'
    scan_index += 1
  end
  
  cmd_ui_name = comp_req.words[scan_index] || ''
  
  cmd = all_commands_by_ui_name[cmd_ui_name]
  
  if cmd.nil?
    return all_commands_by_ui_name.keys.select { |ui_name|
      ui_name.start_with? cmd_ui_name
    }
  end
  
  index = scan_index + 1
  
  # is it a subcommand?
  if subcommand_classes.key? cmd.name
    # It is, hand it off to there
    subcommand_classes[cmd.name].bash_complete \
      comp_req: comp_req,
      index: index
  else
    # It's a command, have that handle it
    cmd.bash_complete \
      comp_req: comp_req,
      index: index
  end
end