-
Notifications
You must be signed in to change notification settings - Fork 8
Shell Completion
mosop edited this page Dec 9, 2016
·
49 revisions
Every command can automatically generate its shell completion script.
The supported shell environments are:
- bash
- zsh
To generate a script, use the generate_xxx_completion method of a command class.
class TicketToRide < Cli::Command
class Options
string "--by", any_of: %w(train plane taxi)
arg "for", any_of: %w(kyoto kanazawa kamakura)
end
end
puts TicketToRide.generate_bash_completion
# or
puts TicketToRide.generate_zsh_completion
See the gist for bash and the gist for zsh.
By default, generate_zsh_completion returns a script used as a zsh function. To generate a script that can be sourced, set the functional option to false.
TicketToRide.generate_zsh_completion(functional: false)
You can set a special completion method to each command line option/argument.
The method types are:
- word-list
- action
- command
It replies specified strings as completions.
class Horoscope
string "--star", completion: %w(
aries
taurus
gemini
cancer
leo
virgo
libra
scorpio
sagittarius
capricorn
aquarius
pisces
)
end
$ horoscope a
aries aquarius
[WIP]
Crystal CLI supports the bash's native completion actions, the -A option of the compgen command. For zsh, Crystal CLI emulates the behavior by the zsh dependent code.