Module: Simple::CLI::Runner::Autocompletion
- Included in:
- Simple::CLI::Runner
- Defined in:
- lib/simple/cli/runner/autocompletion.rb
Constant Summary collapse
- CommandHelp =
Simple::CLI::Runner::CommandHelp
- AUTOCOMPLETE_SHELL_CODE =
The shell function function is executed in the current shell environment. When it is executed,
- $1 is the name of the command whose arguments are being completed,
- $2 is the word being completed, and
- $3 is the word preceding the word being completed
When it finishes, the possible completions are retrieved from the value of the COMPREPLY array variable.
see https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html
"_{{BINARY}}() \n{\n local cmd=$1\n local cur=$2\n\n if [[ $COMP_CWORD == 1 ]]; then\n COMPREPLY=( $(\"$cmd\" autocomplete \"$cur\" ))\n else\n local subcommand=${COMP_WORDS[1]}\n COMPREPLY=( $(\"$cmd\" autocomplete \"$subcommand\" \"$cur\" ))\n fi\n\n return 0\n}\ncomplete -F _{{BINARY}} {{BINARY}}\n"
Instance Method Summary collapse
- #autocomplete(subcommand = nil, cur = nil) ⇒ Object
- #autocomplete_bash ⇒ Object
- #autocomplete_help ⇒ Object
- #autocomplete_subcommand_options(subcommand, cur) ⇒ Object
- #autocomplete_subcommands(cur) ⇒ Object
- #filter_completions(completions, prefix:) ⇒ Object
Instance Method Details
#autocomplete(subcommand = nil, cur = nil) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/simple/cli/runner/autocompletion.rb', line 4 def autocomplete(subcommand=nil, cur=nil) completions = if !cur autocomplete_subcommands(subcommand) else (subcommand, cur) end puts completions.join("\n") end |
#autocomplete_bash ⇒ Object
48 49 50 |
# File 'lib/simple/cli/runner/autocompletion.rb', line 48 def autocomplete_bash puts AUTOCOMPLETE_SHELL_CODE.gsub(/{{BINARY}}/, binary_name) end |
#autocomplete_help ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/simple/cli/runner/autocompletion.rb', line 38 def autocomplete_help STDERR.puts " \#{binary_name} supports autocompletion. To enable autocompletion please run \n\n eval \"$(\#{$0} autocomplete:bash)\"\n DOC\n\n exit 1\nend\n" |
#autocomplete_subcommand_options(subcommand, cur) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/simple/cli/runner/autocompletion.rb', line 28 def (subcommand, cur) completions = if subcommand == "help" commands.map(&:to_s) + [ "autocomplete" ] else CommandHelp.option_names(@app, subcommand) end filter_completions completions, prefix: cur end |
#autocomplete_subcommands(cur) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/simple/cli/runner/autocompletion.rb', line 19 def autocomplete_subcommands(cur) completions = filter_completions commands.map(&:to_s), prefix: cur if completions == [ cur ] (cur, nil) else completions end end |
#filter_completions(completions, prefix:) ⇒ Object
13 14 15 16 17 |
# File 'lib/simple/cli/runner/autocompletion.rb', line 13 def filter_completions(completions, prefix:) completions.select do |completion| !prefix || completion.start_with?(prefix) end end |