在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:kislyuk/argcomplete开源软件地址:https://github.com/kislyuk/argcomplete开源编程语言:Python 97.2%开源软件介绍:argcomplete - Bash tab completion for argparseTab complete all the things! Argcomplete provides easy, extensible command line tab completion of arguments for your Python script. It makes two assumptions:
Argcomplete is particularly useful if your program has lots of options or subparsers, and if your program can dynamically suggest completions for your argument/option values (for example, if the user is browsing resources over the network). Installationpip3 install argcomplete activate-global-python-argcomplete See Activating global completion below for details about the second step (or if it reports an error). Refresh your bash environment (start a new shell or SynopsisPython code (e.g. #!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argcomplete, argparse
parser = argparse.ArgumentParser()
...
argcomplete.autocomplete(parser)
args = parser.parse_args()
... Shellcode (only necessary if global completion is not activated - see Global completion below), to be put in e.g. eval "$(register-python-argcomplete my-awesome-script)" argcomplete.autocomplete(parser)This method is the entry point to the module. It must be called after ArgumentParser construction is complete, but
before the Side effects Argcomplete gets completions by running your program. It intercepts the execution flow at the moment
Performance If the program takes a long time to get to the point where Specifying completersYou can specify custom completion functions for your options and arguments. Two styles are supported: callable and readline-style. Callable completers are simpler. They are called with the following keyword arguments:
Completers should return their completions as a list of strings. An example completer for names of environment variables might look like this: def EnvironCompleter(**kwargs):
return os.environ To specify a completer for an argument or option, set the from argcomplete.completers import EnvironCompleter
parser = argparse.ArgumentParser()
parser.add_argument("--env-var1").completer = EnvironCompleter
parser.add_argument("--env-var2").completer = EnvironCompleter
argcomplete.autocomplete(parser) If you specify the A completer that is initialized with a set of all possible choices of values for its action might look like this: class ChoicesCompleter(object):
def __init__(self, choices):
self.choices = choices
def __call__(self, **kwargs):
return self.choices The following two ways to specify a static set of choices are equivalent for completion purposes: from argcomplete.completers import ChoicesCompleter
parser.add_argument("--protocol", choices=('http', 'https', 'ssh', 'rsync', 'wss'))
parser.add_argument("--proto").completer=ChoicesCompleter(('http', 'https', 'ssh', 'rsync', 'wss')) Note that if you use the The following script uses
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argcomplete, argparse, requests, pprint
def github_org_members(prefix, parsed_args, **kwargs):
resource = "https://api.github.com/orgs/{org}/members".format(org=parsed_args.organization)
return (member['login'] for member in requests.get(resource).json() if member['login'].startswith(prefix))
parser = argparse.ArgumentParser()
parser.add_argument("--organization", help="GitHub organization")
parser.add_argument("--member", help="GitHub member").completer = github_org_members
argcomplete.autocomplete(parser)
args = parser.parse_args()
pprint.pprint(requests.get("https://api.github.com/users/{m}".format(m=args.member)).json()) Try it like this: ./describe_github_user.py --organization heroku --member <TAB> If you have a useful completer to add to the completer library, send a pull request! Readline-style completersThe readline module defines a completer protocol in rlcompleter. Readline-style completers are also supported by argcomplete, so you can use the same completer object both in an interactive readline-powered shell and on the bash command line. For example, you can use the readline-style completer provided by IPython to get introspective completions like you would get in the IPython shell: import IPython
parser.add_argument("--python-name").completer = IPython.core.completer.Completer()
Printing warnings in completersNormal stdout/stderr output is suspended when argcomplete runs. Sometimes, though, when the user presses from argcomplete import warn
def AwesomeWebServiceCompleter(prefix, **kwargs):
if login_failed:
warn("Please log in to Awesome Web Service to use autocompletion")
return completions Using a custom completion validatorBy default, argcomplete validates your completions by checking if they start with the prefix given to the completer. You
can override this validation check by supplying the def my_validator(current_input, keyword_to_check_against):
# Pass through ALL options even if they don't all start with 'current_input'
return True
argcomplete.autocomplete(parser, validator=my_validator) Global completionIn global completion mode, you don't have to register each argcomplete-capable executable separately. Instead, bash will look for the string PYTHON_ARGCOMPLETE_OK in the first 1024 bytes of any executable that it's running completion for, and if it's found, follow the rest of the argcomplete protocol as described above. Additionally, completion is activated for scripts run as Bash version compatibility Global completion requires bash support for Global completion is not currently compatible with zsh. Note If you use setuptools/distribute If you choose not to use global completion, or ship a bash completion module that depends on argcomplete, you must
register your script explicitly using Activating global completionThe script activate-global-python-argcomplete --dest=/path/to/bash_completion.d Otherwise, you can redirect its shellcode output into a file: activate-global-python-argcomplete --dest=- > file The file's contents should then be sourced in e.g. Zsh SupportTo activate completions for zsh you need to have autoload -U bashcompinit bashcompinit Afterwards you can enable completion for your scripts with eval "$(register-python-argcomplete my-awesome-script)" Tcsh SupportTo activate completions for tcsh use: eval `register-python-argcomplete --shell tcsh my-awesome-script` The complete my-awesome-script 'p@*@`python-argcomplete-tcsh my-awesome-script`@' Fish SupportTo activate completions for fish use: register-python-argcomplete --shell fish my-awesome-script | source or create new completion file, e.g: register-python-argcomplete --shell fish my-awesome-script > ~/.config/fish/completions/my-awesome-script.fish Completion Description For FishBy default help string is added as completion description. You can disable this feature by removing register-python-argcomplete --shell fish my-awesome-script | grep -v _ARGCOMPLETE_DFS | source Absolute Path CompletionIf script is not in path you still can register it's completion by specifying absolute path: register-python-argcomplete --shell fish /home/awesome-user/my-awesome-script | source then you can complete it by using Unfortunately register-python-argcomplete --shell fish my-awesome-script -e /home/awesome-user/my-awesome-script | source This would enable completion for any Git Bash SupportDue to limitations of file descriptor inheritance on Windows,
Git Bash not supported out of the box. You can opt in to using
temporary files instead of file descriptors for for IPC
by setting the environment variable For full support, consider using Bash with the Windows Subsystem for Linux (WSL). External argcomplete scriptTo register an argcomplete script for an arbitrary name, the eval "$(register-python-argcomplete --external-argcomplete-script /path/to/script arbitrary-name)" This allows, for example, to use the auto completion functionality of argcomplete for an application not written in Python. The command line interface of this program must be additionally implemented in a Python script with argparse and argcomplete and whenever the application is called the registered external argcomplete script is used for auto completion. This option can also be used in combination with the other supported shells. Python SupportArgcomplete requires Python 3.6+. Common ProblemsIf global completion is not completing your script, bash may have registered a default completion function: $ complete | grep my-awesome-script complete -F _minimal my-awesome-script You can fix this by restarting your shell, or by running
DebuggingSet the AcknowledgmentsInspired and informed by the optcomplete module by Martin Blais. Links
BugsPlease report bugs, issues, feature requests, etc. on GitHub. LicenseLicensed under the terms of the Apache License, Version 2.0. |
2022-08-15
2022-08-17
2023-10-27
2022-09-23
2022-08-18
请发表评论