Custom tab completion in python argparse

后端 未结 2 913
遥遥无期
遥遥无期 2020-12-23 09:44

How to get shell tab completion cooperating with argparse in a Python script?

#!/usr/bin/env python         


        
2条回答
  •  一整个雨季
    2020-12-23 10:30

    Have a look at argcomplete by Andrey Kislyuk.

    Install it with:

    pip install argcomplete
    

    Import the module and add one line in your source before calling parser.parse_args():

    #!/usr/bin/env python
    
    import argparse as ap
    import argcomplete
    
    def main(**args):
      pass
    
    if __name__ == '__main__':
      parser = ap.ArgumentParser()
      parser.add_argument('positional', choices=['spam', 'eggs'])
      parser.add_argument('--optional', choices=['foo1', 'foo2', 'bar'])
      argcomplete.autocomplete(parser)
      args = parser.parse_args()
      main(**vars(args))
    

    and to make sure that bash knows about this script, you use

    eval "$(register-python-argcomplete your_script)"
    

    you should put that line in your ~/.bashrc or follow argcomplete's docs and activate 'global' completion.

    After that you completion works as requested.

    The way this works is that the eval line creates a function _python_argcomlete which is registered using complete. (Run register-python-argcomplete your_script to just have a look at what gets eval-ed into bash). The autocomplete function looks for environment variables set by the bash completion mechanism to see if it needs to act. If it acts, it exits the program. If it doesn't act, this is a normal call to the program that function does nothing and the normal flow of the program continues.

提交回复
热议问题