### Example completer for environment variables Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst An example completer for names of environment variables. ```python def EnvironCompleter(**kwargs): return os.environ ``` -------------------------------- ### IPython Completer Example Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Example of using an IPython completer with argcomplete. ```python import IPython parser.add_argument("--python-name").completer = IPython.core.completer.Completer() ``` -------------------------------- ### Warn Function Example Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Demonstrates how to use the 'warn' function to print messages when completions fail. ```python from argcomplete import warn def AwesomeWebServiceCompleter(prefix, **kwargs): if login_failed: warn("Please log in to Awesome Web Service to use autocompletion") return completions ``` -------------------------------- ### Installation Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Install argcomplete using pip and activate global completion. ```bash pip install argcomplete activate-global-python-argcomplete ``` -------------------------------- ### ChoicesCompleter Example Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Demonstrates how to use a custom ChoicesCompleter to provide static choices for an argparse argument. ```python class ChoicesCompleter(object): def __init__(self, choices): self.choices = choices def __call__(self, **kwargs): return self.choices ``` ```python 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')) ``` -------------------------------- ### Custom Completion Validator Example Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Shows how to override the default completion validation with a custom validator. ```python def my_validator(completion_candidate, current_input): """Complete non-prefix substring matches.""" return current_input in completion_candidate argcomplete.autocomplete(parser, validator=my_validator) ``` -------------------------------- ### PowerShell Profile Import Source: https://github.com/kislyuk/argcomplete/blob/main/contrib/README.rst Import a created PowerShell completion module into the user's profile. ```powershell Import-Module "~/my-awesome-script.psm1" ``` -------------------------------- ### PowerShell Completion File Creation Source: https://github.com/kislyuk/argcomplete/blob/main/contrib/README.rst Create a completion file for a script in PowerShell. ```powershell register-python-argcomplete --shell powershell my-awesome-script > ~/my-awesome-script.psm1 ``` -------------------------------- ### Fish Shell Completion File Creation Source: https://github.com/kislyuk/argcomplete/blob/main/contrib/README.rst Create a completion file for a script in the fish shell. ```bash register-python-argcomplete --shell fish my-awesome-script > ~/.config/fish/completions/my-awesome-script.fish ``` -------------------------------- ### Fish Shell Completion Sourcing Source: https://github.com/kislyuk/argcomplete/blob/main/contrib/README.rst Source a created completion file in the fish shell to load the completions. ```bash source ~/.config/fish/completions/my-awesome-script.fish ``` -------------------------------- ### Fish Shell Completion Registration (with Path) Source: https://github.com/kislyuk/argcomplete/blob/main/contrib/README.rst Register completions for a script using its absolute path in the fish shell. ```bash register-python-argcomplete --shell fish $(realpath ./my-awesome-script) > ~/.config/fish/completions/my-awesome-script.fish ``` -------------------------------- ### Registering an application Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Register your Python application with your shell's completion framework. ```bash eval "$(register-python-argcomplete my-python-app)" ``` -------------------------------- ### PowerShell Completion Registration Source: https://github.com/kislyuk/argcomplete/blob/main/contrib/README.rst Register completions for a script in PowerShell. ```powershell register-python-argcomplete --shell powershell my-awesome-script | Out-String | Invoke-Expression ``` -------------------------------- ### Git Bash Support Configuration Source: https://github.com/kislyuk/argcomplete/blob/main/contrib/README.rst To enable argcomplete support in Git Bash on Windows, set the ARGCOMPLETE_USE_TEMPFILES environment variable. ```bash export ARGCOMPLETE_USE_TEMPFILES=1 ``` -------------------------------- ### Bash version compatibility Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Checks the version of the running copy of bash. ```bash echo $BASH_VERSION ``` -------------------------------- ### Fish Shell Completion Registration (Direct) Source: https://github.com/kislyuk/argcomplete/blob/main/contrib/README.rst Register completions for a script in the fish shell by piping the output of register-python-argcomplete to the source command. ```bash register-python-argcomplete --shell fish my-awesome-script | source ``` -------------------------------- ### Synopsis Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Add the PYTHON_ARGCOMPLETE_OK marker and a call to argcomplete.autocomplete() to your Python application. ```python #!/usr/bin/env python # PYTHON_ARGCOMPLETE_OK import argcomplete, argparse parser = argparse.ArgumentParser() ... argcomplete.autocomplete(parser) args = parser.parse_args() ... ``` -------------------------------- ### Fish Shell Completion Description Disabling Source: https://github.com/kislyuk/argcomplete/blob/main/contrib/README.rst Disable the default behavior of adding help strings as completion descriptions in fish by filtering out the _ARGCOMPLETE_DFS variable. ```bash register-python-argcomplete --shell fish my-awesome-script | grep -v _ARGCOMPLETE_DFS | source ``` -------------------------------- ### GitHub Organization Members Autocompletion Script Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst A Python script that uses argcomplete to autocompletes GitHub organization members based on user input. ```python #!/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()) ``` -------------------------------- ### External Argcomplete Script Registration Source: https://github.com/kislyuk/argcomplete/blob/main/contrib/README.rst Register an argcomplete script for an arbitrary name using the --external-argcomplete-script argument. ```bash eval "$(register-python-argcomplete --external-argcomplete-script /path/to/script arbitrary-name)" ``` -------------------------------- ### Debugging argcomplete Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Enables verbose debug output for argcomplete. ```bash _ARC_DEBUG=1 your_command ``` -------------------------------- ### Specifying completers for arguments Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Specify a completer for an argument or option by setting the completer attribute of its associated action. ```python from argcomplete.completers import EnvironCompleter parser = argparse.ArgumentParser() parser.add_argument("--env-var1").completer = EnvironCompleter parser.add_argument("--env-var2").completer = EnvironCompleter ``` -------------------------------- ### Fixing default completion function Source: https://github.com/kislyuk/argcomplete/blob/main/README.rst Command to remove a default completion function for a script. ```bash complete -r my-python-app ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.