NAME Complete::Bash - Completion module for bash shell VERSION This document describes version 0.03 of Complete::Bash (from Perl distribution Complete-Bash), released on 2014-07-17. DESCRIPTION Bash allows completion to come from various sources. The simplest is from a list of words ("-W"): % complete -W "one two three four" somecmd % somecmd t two three Another source is from a bash function ("-F"). The function will receive input in two variables: "COMP_WORDS" (array, command-line chopped into words) and "COMP_CWORD" (integer, index to the array of words indicating the cursor position). It must set an array variable "COMPREPLY" that contains the list of possible completion: % _foo() { local cur COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} COMPREPLY=($( compgen -W '--help --verbose --version' -- $cur ) ) } % complete -F _foo foo % foo --help --verbose --version And yet another source is an external command (including, a Perl script). The command receives two environment variables: "COMP_LINE" (string, raw command-line) and "COMP_POINT" (integer, cursor location). Program must split "COMP_LINE" into words, find the word to be completed, complete that, and return the list of words one per-line to STDOUT. An example: % cat foo-complete #!/usr/bin/perl use Complete::Bash qw(parse_cmdline format_completion); use Complete::Util qw(complete_array_elem); my ($words, $cword) = parse_cmdline(); my $res = complete_array_elem(array=>[qw/--help --verbose --version/], word=>$words->[$cword]); print format_completion($res); % complete -C foo-complete foo % foo --v --verbose --version This module provides routines for you to be doing the above. Instead of being called by bash as an external command every time user presses Tab, you can also use Perl to *generate* bash "complete" scripts for you. See Complete::BashGen. FUNCTIONS break_cmdline_into_words(@args) -> array Break command-line string into words. Note to users: this is an internal function. Normally you only need to use "parse_cmdline". The first step of shell completion is to break the command-line string (e.g. from COMP_LINE in bash) into words. Bash by default split using these characters (from COMP_WORDBREAKS): COMP_WORDBREAKS=$' \t\n"\'@><=;|&(:' We don't necessarily want to split using default bash's rule, for example in Perl we might want to complete module names which contain colons (e.g. "Module::Path"). By default, this routine splits by spaces and tabs and takes into account backslash and quoting. Unclosed quotes won't generate error. Arguments ('*' denotes required arguments): * cmdline* => *str* * word_breaks => *str* Return value: format_completion(@args) -> str Format completion for output to shell. Usually, like in bash, we just need to output the entries one line at a time, with some special characters in the entry escaped using backslashes so it's not interpreted by the shell. This function accepts a hash, not an array. You can put the result of "complete_*" function in the "completion" key of the hash. The other keys can be added for hints on how to format the completion reply more correctly/appropriately to the shell. Known hints: "type" (string, can be "filename", "env", or others; this helps the routine picks the appropriate escaping), "is_path" (bool, if set to true then "mimic_shell_dir_completion" logic is applied), "path_sep" (string, character to separate path, defaults to "/"). Arguments ('*' denotes required arguments): * shell_completion* => *hash* Result of shell completion. A hash containing list of completions and other metadata. For example: { completion => ['f1', 'f2', 'f3.txt', 'foo:bar.txt'], type => 'filename', } Return value: mimic_dir_completion(@args) -> array Make completion of paths behave more like shell. Note for users: normally you just need to use "format_completion()" and need not know about this function. This function employs a trick to make directory/path completion work more like shell's own. In shell, when completing directory, the sole completion for "foo/" is "foo/", the cursor doesn't automatically add a space (like the way it does when there is only a single completion possible). Instead it stays right after the "/" to allow user to continue completing further deeper in the tree ("foo/bar" and so on). To make programmable completion work like shell's builtin dir completion, the trick is to add another completion alternative "foo/" (with an added space) so shell won't automatically add a space because there are now more than one completion possible ("foo/" and "foo/"). Arguments ('*' denotes required arguments): * completion* => *array* * sep => *str* (default: "/") Return value: parse_cmdline(@args) -> any Parse shell command-line for processing by completion routines. Currently only supports bash. Returns a list: ($words, $cword). $words is array of str, equivalent to "COMP_WORDS" provided by shell to bash function. $cword is an integer, equivalent to shell-provided "COMP_CWORD" variable to bash function. Arguments ('*' denotes required arguments): * cmdline => *str* Command-line, defaults to COMP_LINE environment. * point => *int* Point/position to complete in command-line, defaults to COMP_POINT. * word_breaks => *str* Extra characters to break word at. In addition to space and tab. Example: "=:". Note that the characters won't break words if inside quotes or escaped. Return value: SEE ALSO Complete Complete::BashGen Other modules related to bash shell tab completion: Bash::Completion, Getopt::Complete. Term::Bash::Completion::Generator Programmable Completion section in Bash manual: HOMEPAGE Please visit the project's homepage at . SOURCE Source repository is at . BUGS Please report any bugs or feature requests on the bugtracker website When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. AUTHOR Steven Haryanto COPYRIGHT AND LICENSE This software is copyright (c) 2014 by Steven Haryanto. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.