在python argparse自定义选项卡完成 [英] Custom tab completion in python argparse

查看:1158
本文介绍了在python argparse自定义选项卡完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能变得更聪明标签完成在Python脚本中使用 argparse 合作?我的shell是bash。我能找到这个东西网上的信息是一岁多,大多涉及 optparse ,我不希望使用的(de precated)。

Is it possible to get smarter tab completion cooperating with argparse in python scripts? My shell is bash. The information I can find about this stuff online is over a year old and mostly relates to optparse, which I don't want to use (deprecated).

有关以下脚本:

#!/usr/bin/env python

import argparse as ap

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'])
    args = parser.parse_args()
    main(**vars(args))

使用chmod + X 上的文件,我预期的使用会是这样的:

After chmod +x on the file, my expected usage would be something like:

./example.py sp<tab>             
   ->  completes to ./example.py spam
./example.py --op<tab> 
   ->  completes to ./example.py --optional
./example.py --optional b<tab>
   ->  completes to ./example.py --optional bar 
./example.py --optional f<tab>   
   ->  completes to ./example.py --optional foo 
       and further, prints  foo1  foo2  on stdout

我不知道在哪里甚至开始攻击这个...

I have no idea where to even start attacking on this ...

推荐答案

在看一看<一个href=\"https://argcomplete.readthedocs.org/en/latest/#activating-global-completion%20argcomplete\">argcomplete安德烈·Kislyuk。

Have a look at argcomplete by Andrey Kislyuk.

与安装它:

sudo pip install argcomplete

导入模块,并在源调用之前添加一行 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)"

你应该把那条线在你的〜/ .bashrc或者按照argcomplete的文档并激活全球完成。

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

之后的要求,你工作完成。

After that you completion works as requested.

的方法这项工作是,EVAL行创建了一个函数 _python_argcomlete 这是使用完整注册。 (润寄存器中的python-argcomplete your_script 来只是看看得到什么EVAL-ED成的bash)。自动完成功能查找由bash补机制设置,看是否需要采取行动的环境变量。如果它的行为,它退出该程序。如果不采取行动,这是该函数什么都不做,程序的正常流动继续执行程序正常通话。

The way this work 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.

这篇关于在python argparse自定义选项卡完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆