Open RonLek opened 4 years ago
I took a slightly different approach to auto complete using complete -C /path/to/command command
where instead of a script generated, bash calls the command directly to ask what should be used.
Related to #415
My code to fetch some of the data was:
# list command names
def list_commands
commands = self.class.all_commands.keys.map { |elem| elem.tr('_', '-') }
commands.reject! { |elem| %w[autocomplete default].include?(elem) }
end
# list flags for a command
def list_arguments(command:)
options = self.class.all_commands[command].options.values
exit 1 if options.empty?
options.map(&:aliases).flatten! +
options.map(&:switch_name)
end
I'm trying to get a list of all commands, subcommands and options of a class that inherits from Thor. For example, the module CLI below inherits from Thor and has two commands actor and service as shown below:
actor.rb
service.rb
I'm looking for a command
list_commands
that could list all commands and their methods and subcommands in a JSON like formatCurrently, kind of get what I what by putting
help
in front of each command/subcommand to get the next subcommand. My intention to use the output of the above command is to auto-generate a bash-completion script as new commands are added into theCommands
module that inherits fromCLI
which inherits fromThor
. Suggestions for any other approaches using some inbuilt mechanism in Thor are also welcome!PS: Also take a look at a similar SO question