Comments (10)
There isn't currently a built in way to do this.
Could you provide an example of how you implement this in the argparse configuration? Do you use parse_known_args()
and examine the remainder?
You could write a completer that calls command-subcommand
while passing through the appropriate environment variables and args, then returns the result, but I'm not sure if that's the nicest way.
Another possibility would be to have the Python portion of argcomplete exit with an exit code that specifies "call the supplied executable name for completions instead", and make the shellcode portion understand that.
from argcomplete.
No, there is no argparsing. All it does is if the subcommand is now known, it looks for command-subcommand
on the PATH and calls out to that instead in a subprocess (with appropriately changed sys.argv).
from argcomplete.
Oh maybe that isn't entirely true. It uses a custom subclass of ArgumentParser with an overridden error method: https://github.com/conda/conda/blob/master/conda/cli/conda_argparse.py#L35.
from argcomplete.
Use of the argparse error handler in this way precludes compatibility with argcomplete (at least for the purpose of returning these completions), because the Argparse class is being extended in a way incompatible with how argcomplete extends it.
I recommend enumerating the command-subcommand combinations as actual argparse actions, instead of overriding the error mechanism.
from argcomplete.
I'll take a look. Is it possible to not search the PATH unless a command is given that isn't found with that way?
from argcomplete.
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
import argparse, argcomplete, subprocess
def complete_subparser_action(prefix, **kwargs):
internal_actions = ['foo', 'bar']
if prefix and any(action.startswith(prefix) for action in internal_actions):
return internal_actions
else:
external_actions = subprocess.check_output("compgen -c "+prefix, shell=True, executable='/bin/bash')
external_actions = [a.decode() for a in external_actions.splitlines()]
return internal_actions + external_actions
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparsers.completer = complete_subparser_action
subparsers.add_parser('foo')
subparsers.add_parser('bar')
argcomplete.autocomplete(parser)
args = parser.parse_args()
print(args)
from argcomplete.
Thanks, I'll look at doing something like that.
By the way, you should probably avoid using shell=True
, as it can introduce security vulnerabilities via shell injection (although I couldn't figure out how to actually do that in this case).
from argcomplete.
This is sample code. I have to use shell=True
, because otherwise compgen is not available. And yes, you should quote and escape it.
from argcomplete.
I have a similar issue, I'm using calling out subshells to do autocompletion: https://github.com/openaps/openaps/blob/master/bin/openaps
However, after the subprocess has done it's completion, the original wrapper is adding the top-level completion opts again, (after the last nargs=argparse.REMAINDER
in the top level shell). Does anyone know how to prevent that from happening?
from argcomplete.
I ended up with the following implementation, which subclasses argcomplete.CompletionFinder
and overrides __call__
conda/conda#1443. I also implement calling it automatically inside of a custom subclass of ArgumentParser, so that extensions only have to import and use that custom subclass to get completion.
from argcomplete.
Related Issues (20)
- How do I exclude files in the users directory from the tab completion? HOT 2
- Unable to get completion to work for NetExec HOT 2
- zsh: completion fails with script path starting with '~/' HOT 1
- 3.2.2: pytest is failing HOT 1
- Output of `argcomplete.warn` does not show HOT 4
- Generating lazy completion scripts for Bash/Zsh HOT 1
- Problem with os.get_terminal_size() and argcomplete HOT 2
- completion with a leading dash HOT 2
- Problems with trust path when packaging HOT 6
- No automatic testing on windows? HOT 2
- Python 3.13: Some tests fail when run with latest Python pre-release HOT 5
- Check recent argparse API changes for compatibility HOT 2
- completer falls back to Bash filename completion
- Python 3.12.3: Test suite fails HOT 1
- autocompletion is slow and stuck HOT 2
- Issue with File Path Autocompletion in Argparse Outside Virtual Environment HOT 4
- Inconsistent completion when using a custom validator between `fish` and `bash` HOT 1
- zsh global completion should not be "-default-" HOT 7
- Filesystem style double tab output HOT 1
- Tab Completion does not work, if we register locally using register-python-argcomplete for a particular file only HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from argcomplete.