ZSH completion depending on the provided argument

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Is there a way to access already provided argument as a variable in a ZSH completion scripts?



here is the example:



#compdef test

_test_comp()
t=($(cat /tmp/file_with_opts))
_wanted t expl "availavle options" compadd -a t


_arguments -C -S
'-f[input file]:filename:_files'
'*:test_autocomplete:_test_comp'


the test script will pick up options from the hardcoded file /tmp/file_with_opts, I'd like to be able to pick up autocomplete options from a file defined via -f. Or rather if -f <filename> is defined I'd like to parse this file and not the default one. Any hint how to do this?










share|improve this question



























    up vote
    1
    down vote

    favorite












    Is there a way to access already provided argument as a variable in a ZSH completion scripts?



    here is the example:



    #compdef test

    _test_comp()
    t=($(cat /tmp/file_with_opts))
    _wanted t expl "availavle options" compadd -a t


    _arguments -C -S
    '-f[input file]:filename:_files'
    '*:test_autocomplete:_test_comp'


    the test script will pick up options from the hardcoded file /tmp/file_with_opts, I'd like to be able to pick up autocomplete options from a file defined via -f. Or rather if -f <filename> is defined I'd like to parse this file and not the default one. Any hint how to do this?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Is there a way to access already provided argument as a variable in a ZSH completion scripts?



      here is the example:



      #compdef test

      _test_comp()
      t=($(cat /tmp/file_with_opts))
      _wanted t expl "availavle options" compadd -a t


      _arguments -C -S
      '-f[input file]:filename:_files'
      '*:test_autocomplete:_test_comp'


      the test script will pick up options from the hardcoded file /tmp/file_with_opts, I'd like to be able to pick up autocomplete options from a file defined via -f. Or rather if -f <filename> is defined I'd like to parse this file and not the default one. Any hint how to do this?










      share|improve this question















      Is there a way to access already provided argument as a variable in a ZSH completion scripts?



      here is the example:



      #compdef test

      _test_comp()
      t=($(cat /tmp/file_with_opts))
      _wanted t expl "availavle options" compadd -a t


      _arguments -C -S
      '-f[input file]:filename:_files'
      '*:test_autocomplete:_test_comp'


      the test script will pick up options from the hardcoded file /tmp/file_with_opts, I'd like to be able to pick up autocomplete options from a file defined via -f. Or rather if -f <filename> is defined I'd like to parse this file and not the default one. Any hint how to do this?







      zsh autocomplete






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday

























      asked yesterday









      mestia

      785




      785




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          This seems something like an XY problem; what exactly motivates this changing of the completion via a provided option? Like could the change instead be done via an environment variable, or why does the completion need to vary?



          Anyways, a file at some point during the option processing may cause problems depending on where -f ... appears relative to other things on the command line. Also the completion code will be repeatedly invoked so maintaining state across different calls to the same and new instances of a command may be tricky. And also $(cat /tmp/file_with_opts) is bad as it needlessly forks out to cat and then splits the contents on...well, who knows.



          With these problems in mind, one way to figure out what options are available is to add something like set > ~/tmp/what-is-set to an appropriate part of the completion code, then hit tab with the cursor at various places with and without the -f ... option and then check the output file to see what things are available to a completion script:



          % grep file_ what-is-set
          BUFFER='foo -f file_with_opts '
          RBUFFER=' -f file_with_opts '
          opt_args=( [-f]=file_with_opts )
          words=( foo '' -f file_with_opts )


          In that case I was tab completing just after foo and before -f ..., so to vary the completion behavior one could check BUFFER or better yet the words array as BUFFER might have -f ... from other commands unrelated to your program that could confuse your completion script.



          So if we loop over words, we can vary what file is read depending on whether (without much error checking) the -f ... option is present with something like:



          #compdef foo

          local curcontext="$curcontext" state line ret=1

          _arguments -C -S
          '-f[input file]:filename:_files'
          '*:options:->vary'
          && ret=0

          case "$state" in
          vary)
          integer i
          local optsfile
          #set > ~/tmp/what-is-set
          # default filename
          optsfile=~/tmp/file_with_opts
          for (( i = 1; i <= $#words - 1; i++ )); do
          if [[ $words[$i] == -f ]]; then
          optsfile=$words[$((i+1))]
          break
          fi
          done
          # cat-free read of file with split on newlines
          t=($(f)"$(<$optsfile)")
          _wanted t expl "available options" compadd -a t
          ;;
          esac

          return $ret


          It may be helpful to study the existing completions under the $fpath[-1] directory.






          share|improve this answer






















          • Thank you very much! this clarifies a lot for me. I can't use environment vars in this case. It is very handy to pick up completions from a file, but sometimes the program operates on a different file and thus it would be silly to provide wrong completions. Thank you also for providing example for cat-free read of file it is definitely useful and finally I know what it means in other _completions files on my system:).
            – mestia
            13 hours ago











          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          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%2f481515%2fzsh-completion-depending-on-the-provided-argument%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










          This seems something like an XY problem; what exactly motivates this changing of the completion via a provided option? Like could the change instead be done via an environment variable, or why does the completion need to vary?



          Anyways, a file at some point during the option processing may cause problems depending on where -f ... appears relative to other things on the command line. Also the completion code will be repeatedly invoked so maintaining state across different calls to the same and new instances of a command may be tricky. And also $(cat /tmp/file_with_opts) is bad as it needlessly forks out to cat and then splits the contents on...well, who knows.



          With these problems in mind, one way to figure out what options are available is to add something like set > ~/tmp/what-is-set to an appropriate part of the completion code, then hit tab with the cursor at various places with and without the -f ... option and then check the output file to see what things are available to a completion script:



          % grep file_ what-is-set
          BUFFER='foo -f file_with_opts '
          RBUFFER=' -f file_with_opts '
          opt_args=( [-f]=file_with_opts )
          words=( foo '' -f file_with_opts )


          In that case I was tab completing just after foo and before -f ..., so to vary the completion behavior one could check BUFFER or better yet the words array as BUFFER might have -f ... from other commands unrelated to your program that could confuse your completion script.



          So if we loop over words, we can vary what file is read depending on whether (without much error checking) the -f ... option is present with something like:



          #compdef foo

          local curcontext="$curcontext" state line ret=1

          _arguments -C -S
          '-f[input file]:filename:_files'
          '*:options:->vary'
          && ret=0

          case "$state" in
          vary)
          integer i
          local optsfile
          #set > ~/tmp/what-is-set
          # default filename
          optsfile=~/tmp/file_with_opts
          for (( i = 1; i <= $#words - 1; i++ )); do
          if [[ $words[$i] == -f ]]; then
          optsfile=$words[$((i+1))]
          break
          fi
          done
          # cat-free read of file with split on newlines
          t=($(f)"$(<$optsfile)")
          _wanted t expl "available options" compadd -a t
          ;;
          esac

          return $ret


          It may be helpful to study the existing completions under the $fpath[-1] directory.






          share|improve this answer






















          • Thank you very much! this clarifies a lot for me. I can't use environment vars in this case. It is very handy to pick up completions from a file, but sometimes the program operates on a different file and thus it would be silly to provide wrong completions. Thank you also for providing example for cat-free read of file it is definitely useful and finally I know what it means in other _completions files on my system:).
            – mestia
            13 hours ago















          up vote
          2
          down vote



          accepted










          This seems something like an XY problem; what exactly motivates this changing of the completion via a provided option? Like could the change instead be done via an environment variable, or why does the completion need to vary?



          Anyways, a file at some point during the option processing may cause problems depending on where -f ... appears relative to other things on the command line. Also the completion code will be repeatedly invoked so maintaining state across different calls to the same and new instances of a command may be tricky. And also $(cat /tmp/file_with_opts) is bad as it needlessly forks out to cat and then splits the contents on...well, who knows.



          With these problems in mind, one way to figure out what options are available is to add something like set > ~/tmp/what-is-set to an appropriate part of the completion code, then hit tab with the cursor at various places with and without the -f ... option and then check the output file to see what things are available to a completion script:



          % grep file_ what-is-set
          BUFFER='foo -f file_with_opts '
          RBUFFER=' -f file_with_opts '
          opt_args=( [-f]=file_with_opts )
          words=( foo '' -f file_with_opts )


          In that case I was tab completing just after foo and before -f ..., so to vary the completion behavior one could check BUFFER or better yet the words array as BUFFER might have -f ... from other commands unrelated to your program that could confuse your completion script.



          So if we loop over words, we can vary what file is read depending on whether (without much error checking) the -f ... option is present with something like:



          #compdef foo

          local curcontext="$curcontext" state line ret=1

          _arguments -C -S
          '-f[input file]:filename:_files'
          '*:options:->vary'
          && ret=0

          case "$state" in
          vary)
          integer i
          local optsfile
          #set > ~/tmp/what-is-set
          # default filename
          optsfile=~/tmp/file_with_opts
          for (( i = 1; i <= $#words - 1; i++ )); do
          if [[ $words[$i] == -f ]]; then
          optsfile=$words[$((i+1))]
          break
          fi
          done
          # cat-free read of file with split on newlines
          t=($(f)"$(<$optsfile)")
          _wanted t expl "available options" compadd -a t
          ;;
          esac

          return $ret


          It may be helpful to study the existing completions under the $fpath[-1] directory.






          share|improve this answer






















          • Thank you very much! this clarifies a lot for me. I can't use environment vars in this case. It is very handy to pick up completions from a file, but sometimes the program operates on a different file and thus it would be silly to provide wrong completions. Thank you also for providing example for cat-free read of file it is definitely useful and finally I know what it means in other _completions files on my system:).
            – mestia
            13 hours ago













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          This seems something like an XY problem; what exactly motivates this changing of the completion via a provided option? Like could the change instead be done via an environment variable, or why does the completion need to vary?



          Anyways, a file at some point during the option processing may cause problems depending on where -f ... appears relative to other things on the command line. Also the completion code will be repeatedly invoked so maintaining state across different calls to the same and new instances of a command may be tricky. And also $(cat /tmp/file_with_opts) is bad as it needlessly forks out to cat and then splits the contents on...well, who knows.



          With these problems in mind, one way to figure out what options are available is to add something like set > ~/tmp/what-is-set to an appropriate part of the completion code, then hit tab with the cursor at various places with and without the -f ... option and then check the output file to see what things are available to a completion script:



          % grep file_ what-is-set
          BUFFER='foo -f file_with_opts '
          RBUFFER=' -f file_with_opts '
          opt_args=( [-f]=file_with_opts )
          words=( foo '' -f file_with_opts )


          In that case I was tab completing just after foo and before -f ..., so to vary the completion behavior one could check BUFFER or better yet the words array as BUFFER might have -f ... from other commands unrelated to your program that could confuse your completion script.



          So if we loop over words, we can vary what file is read depending on whether (without much error checking) the -f ... option is present with something like:



          #compdef foo

          local curcontext="$curcontext" state line ret=1

          _arguments -C -S
          '-f[input file]:filename:_files'
          '*:options:->vary'
          && ret=0

          case "$state" in
          vary)
          integer i
          local optsfile
          #set > ~/tmp/what-is-set
          # default filename
          optsfile=~/tmp/file_with_opts
          for (( i = 1; i <= $#words - 1; i++ )); do
          if [[ $words[$i] == -f ]]; then
          optsfile=$words[$((i+1))]
          break
          fi
          done
          # cat-free read of file with split on newlines
          t=($(f)"$(<$optsfile)")
          _wanted t expl "available options" compadd -a t
          ;;
          esac

          return $ret


          It may be helpful to study the existing completions under the $fpath[-1] directory.






          share|improve this answer














          This seems something like an XY problem; what exactly motivates this changing of the completion via a provided option? Like could the change instead be done via an environment variable, or why does the completion need to vary?



          Anyways, a file at some point during the option processing may cause problems depending on where -f ... appears relative to other things on the command line. Also the completion code will be repeatedly invoked so maintaining state across different calls to the same and new instances of a command may be tricky. And also $(cat /tmp/file_with_opts) is bad as it needlessly forks out to cat and then splits the contents on...well, who knows.



          With these problems in mind, one way to figure out what options are available is to add something like set > ~/tmp/what-is-set to an appropriate part of the completion code, then hit tab with the cursor at various places with and without the -f ... option and then check the output file to see what things are available to a completion script:



          % grep file_ what-is-set
          BUFFER='foo -f file_with_opts '
          RBUFFER=' -f file_with_opts '
          opt_args=( [-f]=file_with_opts )
          words=( foo '' -f file_with_opts )


          In that case I was tab completing just after foo and before -f ..., so to vary the completion behavior one could check BUFFER or better yet the words array as BUFFER might have -f ... from other commands unrelated to your program that could confuse your completion script.



          So if we loop over words, we can vary what file is read depending on whether (without much error checking) the -f ... option is present with something like:



          #compdef foo

          local curcontext="$curcontext" state line ret=1

          _arguments -C -S
          '-f[input file]:filename:_files'
          '*:options:->vary'
          && ret=0

          case "$state" in
          vary)
          integer i
          local optsfile
          #set > ~/tmp/what-is-set
          # default filename
          optsfile=~/tmp/file_with_opts
          for (( i = 1; i <= $#words - 1; i++ )); do
          if [[ $words[$i] == -f ]]; then
          optsfile=$words[$((i+1))]
          break
          fi
          done
          # cat-free read of file with split on newlines
          t=($(f)"$(<$optsfile)")
          _wanted t expl "available options" compadd -a t
          ;;
          esac

          return $ret


          It may be helpful to study the existing completions under the $fpath[-1] directory.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 8 hours ago

























          answered yesterday









          thrig

          23.4k12955




          23.4k12955











          • Thank you very much! this clarifies a lot for me. I can't use environment vars in this case. It is very handy to pick up completions from a file, but sometimes the program operates on a different file and thus it would be silly to provide wrong completions. Thank you also for providing example for cat-free read of file it is definitely useful and finally I know what it means in other _completions files on my system:).
            – mestia
            13 hours ago

















          • Thank you very much! this clarifies a lot for me. I can't use environment vars in this case. It is very handy to pick up completions from a file, but sometimes the program operates on a different file and thus it would be silly to provide wrong completions. Thank you also for providing example for cat-free read of file it is definitely useful and finally I know what it means in other _completions files on my system:).
            – mestia
            13 hours ago
















          Thank you very much! this clarifies a lot for me. I can't use environment vars in this case. It is very handy to pick up completions from a file, but sometimes the program operates on a different file and thus it would be silly to provide wrong completions. Thank you also for providing example for cat-free read of file it is definitely useful and finally I know what it means in other _completions files on my system:).
          – mestia
          13 hours ago





          Thank you very much! this clarifies a lot for me. I can't use environment vars in this case. It is very handy to pick up completions from a file, but sometimes the program operates on a different file and thus it would be silly to provide wrong completions. Thank you also for providing example for cat-free read of file it is definitely useful and finally I know what it means in other _completions files on my system:).
          – mestia
          13 hours ago


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481515%2fzsh-completion-depending-on-the-provided-argument%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)