NAME
Perinci::Sub::Complete - Shell completion routines using Rinci metadata
VERSION
version 0.35
SYNOPSIS
# require'd by Perinci::CmdLine when shell completion is enabled
DESCRIPTION
This module provides functionality for doing shell completion. It is meant to be used by Perinci::CmdLine, but nevertheless some routines are reusable outside it.
FUNCTIONS
None are exported by default, but they are exportable.
complete_arg_val(%args) -> array
Arguments ('*' denotes required arguments):
arg* => str
Argument name.
args => hash
Collected arguments so far, will be passed to completion routines.
ci => bool (default: 0)
Whether to be case-insensitive.
meta* => hash
Rinci function metadata.
parent_args => hash
To pass parent arguments to completion routines.
word => str (default: "")
Word to be completed.
Return value:
shell_complete_arg(%args) -> array
Assuming that command-line like:
foo a b c
is executing some function, and the command-line arguments will be parsed using Perinci::Sub::GetArgs::Argv
, then try to complete command-line arguments using information from Rinci metadata.
Algorithm:
If word begins with
$
, we complete from environment variables and are done.Call
get_args_from_argv()
to extract hash arguments from the givenwords
.Determine whether we need to complete argument name (e.g.
--arg<tab
>) or argument value (e.g.--arg1 <tab
> or<tab
> at 1st word where there is an argument specified at pos=0).Call
custom_completer
if defined. If a list of words is returned, we're done. This can be used for, e.g. nested function call, e.g.:somecmd --opt-for-cmd ... subcmd --opt-for-subcmd ...
If we are completing argument name, then supply a list of possible argument names, or fallback to completing filenames.
If we are completing argument value, first check if
custom_arg_completer
is defined. If yes, call that routine. If a list of words is returned, we're done. Fallback to completing argument values from information in Rinci metadata (usingcomplete_arg_val()
function).
Arguments ('*' denotes required arguments):
common_opts => array (default: [["--help", "-?", "-h"]])
Common options.
When completing argument name, this list will be added.
custom_arg_completer => code|hash
Supply custom argument value completion routines.
Either code or a hash of argument names and codes.
If supplied, instead of the default completion routine, this code will be called instead. Refer to function description to see when this routine is called.
Code will be called with hash arguments containing these keys:
word
(string, the word to be completed),arg
(string, the argument name that we are completing the value of),args
(hash, the arguments that have been collected so far),parent_args
.A use-case for using this option: getting argument value from Riap client using the
complete_arg_val
action. This allows getting completion from remote server.custom_completer => code
Supply custom completion routine.
If supplied, instead of the default completion routine, this code will be called instead. Refer to function description to see when this routine is called.
Code will be called with a hash argument, with these keys:
which
(a string with valuename
orvalue
depending on whether we should complete argument name or value),words
(an array, the command line split into words),cword
(int, position of word inwords
),word
(the word to be completed),parent_args
(hash, arguments given toshell_complete_arg()
),args
(hash, parsed function arguments fromwords
)remaining_words
(array, slice ofwords
aftercword
),meta
(the Rinci function metadata).Code should return an arrayref of completion, or
undef
to declare declination, on which case completion will resume using the standard builtin routine.A use-case of using this option: XXX.
cword => int
On which word cursor is located (zero-based).
If unset, will be taken from COMPLINE and COMPPOINT.
extra_completer_args => hash
Arguments to pass to custom completion routines.
meta* => hash
Rinci function metadata.
words => array
Command-line, broken as words.
If unset, will be taken from COMPLINE and COMPPOINT.
Return value:
BUGS/LIMITATIONS/TODOS
Due to parsing limitation (invokes subshell), can't complete unclosed quotes, e.g.
foo "bar <tab>
while shell function can complete this because they are provided COMP_WORDS
and COMP_CWORD
by bash.
SEE ALSO
Other shell completion modules on CPAN: Getopt::Complete, Bash::Completion.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Perinci-Sub-Complete.
SOURCE
Source repository is at https://github.com/sharyanto/perl-Perinci-Sub-Complete.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Sub-Complete
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 <stevenharyanto@gmail.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2013 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.