Can I use a helper function from a ZSH completion file in another
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm writing a ZSH completion file for a command which is a wrapper around another command that already has ZSH completion support. I'd like to use "private" helper functions defined in the other command's completion, but the naive approach isn't working. Here's what I am trying so far:
#compdef wrapper
_arguments ':action:(foo bar baz)' ':target:_other_command_helper_func'
when I try to do completion at the command line, I get
_arguments:450: command not found: _other_command_helper_func
Is it possible to do what I want to do here?
zsh
add a comment |Â
up vote
1
down vote
favorite
I'm writing a ZSH completion file for a command which is a wrapper around another command that already has ZSH completion support. I'd like to use "private" helper functions defined in the other command's completion, but the naive approach isn't working. Here's what I am trying so far:
#compdef wrapper
_arguments ':action:(foo bar baz)' ':target:_other_command_helper_func'
when I try to do completion at the command line, I get
_arguments:450: command not found: _other_command_helper_func
Is it possible to do what I want to do here?
zsh
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm writing a ZSH completion file for a command which is a wrapper around another command that already has ZSH completion support. I'd like to use "private" helper functions defined in the other command's completion, but the naive approach isn't working. Here's what I am trying so far:
#compdef wrapper
_arguments ':action:(foo bar baz)' ':target:_other_command_helper_func'
when I try to do completion at the command line, I get
_arguments:450: command not found: _other_command_helper_func
Is it possible to do what I want to do here?
zsh
I'm writing a ZSH completion file for a command which is a wrapper around another command that already has ZSH completion support. I'd like to use "private" helper functions defined in the other command's completion, but the naive approach isn't working. Here's what I am trying so far:
#compdef wrapper
_arguments ':action:(foo bar baz)' ':target:_other_command_helper_func'
when I try to do completion at the command line, I get
_arguments:450: command not found: _other_command_helper_func
Is it possible to do what I want to do here?
zsh
asked Nov 10 '17 at 20:51
dcrosta
1083
1083
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
There are no private functions, so it'll work if _other_command_helper_func
is already loaded. Since completion functions are normally autoloaded, that means that, with this approach, the completion for wrapper
will only work if completion of the original command has already been attempted.
One solution is to force the loading of the original command's completion function file, but this is not straightforward. You can use autoload -X +U
to load the completer for the original command, but what this in fact does is to create a function _other_command
(assuming that's the name of the original command) whose definition is the content of the _other_command
file, consisting of the definition of auxiliary functions and other initialization followed by a call to the main function.
_other_command ()
_other_command_helper_func ()
â¦
_other_command ()
â¦
_other_command "$@"
You could try to parse that. If the definition is under your control, you can make some assumptions regarding the initialization code. If it comes from a third party, that's more risky. Most complex completion function definition files follow the model of having initialization followed by a last line of the form _main_function "$@"
, so this will usually work:
if ((!$+functions[_other_command_helper_func])); then
autoload -X +U _other_command
functions[_other_command]=$functions[_other_command]%$'n'*
_other_command
fi
Alternatively, you can just run the completion function in your .zshrc
and let it choke silently because it wasn't run in a completion context. For example, I had this precise problem with CVS; I put _cvs 2>/dev/null
in my .zshrc
.
If the definition of the original is under your control, then there are better solutions. You could use the same completion function for the original command and for the wrapper (check $service
to see what command's arguments are being completed). Or you could define the auxiliary function in a separate file.
Ah, so the whole contents of the_other_command
file in the completions dir becomes a single function, then? I think that's what I was missing. I'll poke at this some more and report back with any findings, etc. Thanks!
â dcrosta
Nov 13 '17 at 14:54
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
There are no private functions, so it'll work if _other_command_helper_func
is already loaded. Since completion functions are normally autoloaded, that means that, with this approach, the completion for wrapper
will only work if completion of the original command has already been attempted.
One solution is to force the loading of the original command's completion function file, but this is not straightforward. You can use autoload -X +U
to load the completer for the original command, but what this in fact does is to create a function _other_command
(assuming that's the name of the original command) whose definition is the content of the _other_command
file, consisting of the definition of auxiliary functions and other initialization followed by a call to the main function.
_other_command ()
_other_command_helper_func ()
â¦
_other_command ()
â¦
_other_command "$@"
You could try to parse that. If the definition is under your control, you can make some assumptions regarding the initialization code. If it comes from a third party, that's more risky. Most complex completion function definition files follow the model of having initialization followed by a last line of the form _main_function "$@"
, so this will usually work:
if ((!$+functions[_other_command_helper_func])); then
autoload -X +U _other_command
functions[_other_command]=$functions[_other_command]%$'n'*
_other_command
fi
Alternatively, you can just run the completion function in your .zshrc
and let it choke silently because it wasn't run in a completion context. For example, I had this precise problem with CVS; I put _cvs 2>/dev/null
in my .zshrc
.
If the definition of the original is under your control, then there are better solutions. You could use the same completion function for the original command and for the wrapper (check $service
to see what command's arguments are being completed). Or you could define the auxiliary function in a separate file.
Ah, so the whole contents of the_other_command
file in the completions dir becomes a single function, then? I think that's what I was missing. I'll poke at this some more and report back with any findings, etc. Thanks!
â dcrosta
Nov 13 '17 at 14:54
add a comment |Â
up vote
2
down vote
accepted
There are no private functions, so it'll work if _other_command_helper_func
is already loaded. Since completion functions are normally autoloaded, that means that, with this approach, the completion for wrapper
will only work if completion of the original command has already been attempted.
One solution is to force the loading of the original command's completion function file, but this is not straightforward. You can use autoload -X +U
to load the completer for the original command, but what this in fact does is to create a function _other_command
(assuming that's the name of the original command) whose definition is the content of the _other_command
file, consisting of the definition of auxiliary functions and other initialization followed by a call to the main function.
_other_command ()
_other_command_helper_func ()
â¦
_other_command ()
â¦
_other_command "$@"
You could try to parse that. If the definition is under your control, you can make some assumptions regarding the initialization code. If it comes from a third party, that's more risky. Most complex completion function definition files follow the model of having initialization followed by a last line of the form _main_function "$@"
, so this will usually work:
if ((!$+functions[_other_command_helper_func])); then
autoload -X +U _other_command
functions[_other_command]=$functions[_other_command]%$'n'*
_other_command
fi
Alternatively, you can just run the completion function in your .zshrc
and let it choke silently because it wasn't run in a completion context. For example, I had this precise problem with CVS; I put _cvs 2>/dev/null
in my .zshrc
.
If the definition of the original is under your control, then there are better solutions. You could use the same completion function for the original command and for the wrapper (check $service
to see what command's arguments are being completed). Or you could define the auxiliary function in a separate file.
Ah, so the whole contents of the_other_command
file in the completions dir becomes a single function, then? I think that's what I was missing. I'll poke at this some more and report back with any findings, etc. Thanks!
â dcrosta
Nov 13 '17 at 14:54
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
There are no private functions, so it'll work if _other_command_helper_func
is already loaded. Since completion functions are normally autoloaded, that means that, with this approach, the completion for wrapper
will only work if completion of the original command has already been attempted.
One solution is to force the loading of the original command's completion function file, but this is not straightforward. You can use autoload -X +U
to load the completer for the original command, but what this in fact does is to create a function _other_command
(assuming that's the name of the original command) whose definition is the content of the _other_command
file, consisting of the definition of auxiliary functions and other initialization followed by a call to the main function.
_other_command ()
_other_command_helper_func ()
â¦
_other_command ()
â¦
_other_command "$@"
You could try to parse that. If the definition is under your control, you can make some assumptions regarding the initialization code. If it comes from a third party, that's more risky. Most complex completion function definition files follow the model of having initialization followed by a last line of the form _main_function "$@"
, so this will usually work:
if ((!$+functions[_other_command_helper_func])); then
autoload -X +U _other_command
functions[_other_command]=$functions[_other_command]%$'n'*
_other_command
fi
Alternatively, you can just run the completion function in your .zshrc
and let it choke silently because it wasn't run in a completion context. For example, I had this precise problem with CVS; I put _cvs 2>/dev/null
in my .zshrc
.
If the definition of the original is under your control, then there are better solutions. You could use the same completion function for the original command and for the wrapper (check $service
to see what command's arguments are being completed). Or you could define the auxiliary function in a separate file.
There are no private functions, so it'll work if _other_command_helper_func
is already loaded. Since completion functions are normally autoloaded, that means that, with this approach, the completion for wrapper
will only work if completion of the original command has already been attempted.
One solution is to force the loading of the original command's completion function file, but this is not straightforward. You can use autoload -X +U
to load the completer for the original command, but what this in fact does is to create a function _other_command
(assuming that's the name of the original command) whose definition is the content of the _other_command
file, consisting of the definition of auxiliary functions and other initialization followed by a call to the main function.
_other_command ()
_other_command_helper_func ()
â¦
_other_command ()
â¦
_other_command "$@"
You could try to parse that. If the definition is under your control, you can make some assumptions regarding the initialization code. If it comes from a third party, that's more risky. Most complex completion function definition files follow the model of having initialization followed by a last line of the form _main_function "$@"
, so this will usually work:
if ((!$+functions[_other_command_helper_func])); then
autoload -X +U _other_command
functions[_other_command]=$functions[_other_command]%$'n'*
_other_command
fi
Alternatively, you can just run the completion function in your .zshrc
and let it choke silently because it wasn't run in a completion context. For example, I had this precise problem with CVS; I put _cvs 2>/dev/null
in my .zshrc
.
If the definition of the original is under your control, then there are better solutions. You could use the same completion function for the original command and for the wrapper (check $service
to see what command's arguments are being completed). Or you could define the auxiliary function in a separate file.
answered Nov 11 '17 at 0:06
Gilles
507k12010031532
507k12010031532
Ah, so the whole contents of the_other_command
file in the completions dir becomes a single function, then? I think that's what I was missing. I'll poke at this some more and report back with any findings, etc. Thanks!
â dcrosta
Nov 13 '17 at 14:54
add a comment |Â
Ah, so the whole contents of the_other_command
file in the completions dir becomes a single function, then? I think that's what I was missing. I'll poke at this some more and report back with any findings, etc. Thanks!
â dcrosta
Nov 13 '17 at 14:54
Ah, so the whole contents of the
_other_command
file in the completions dir becomes a single function, then? I think that's what I was missing. I'll poke at this some more and report back with any findings, etc. Thanks!â dcrosta
Nov 13 '17 at 14:54
Ah, so the whole contents of the
_other_command
file in the completions dir becomes a single function, then? I think that's what I was missing. I'll poke at this some more and report back with any findings, etc. Thanks!â dcrosta
Nov 13 '17 at 14:54
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403820%2fcan-i-use-a-helper-function-from-a-zsh-completion-file-in-another%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password