Can I use a helper function from a ZSH completion file in another

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question
























    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?







    share|improve this question






















      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?







      share|improve this question












      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?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 '17 at 20:51









      dcrosta

      1083




      1083




















          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.






          share|improve this answer




















          • 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










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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.






          share|improve this answer




















          • 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














          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.






          share|improve this answer




















          • 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












          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?