Comments (13)
Yes, this works out of the box. Have you tried using it?
from argcomplete.
I did try, but could not get it to work at all. It only completed anything if i had options with dashes.
Do you have a short example?
from argcomplete.
I'm seeing the same problem. Default does not work for options without dashes.
from argcomplete.
Thanks for the report. Can you please paste a test use case that you expect to work?
from argcomplete.
Thank you for the quick response. I got it working. If you add the argument to the main parser like in the following snippet it does not work:
parser = argparse.ArgumentParser(description='description')
parser.add_argument("option", dest="option", help="positional option help")
argcomplete.autocomplete(parser)
args = parser.parse_args()
If you add the argument as a subparser then the code works as intended. Example snippet:
parser = argparse.ArgumentParser(description='description')
subparsers = parser.add_subparsers()
parser_option = subparsers.add_parser("option", help="positional option help")
parser_option.add_argument("--optional_option", dest="optional_option", help="optional option under option help")
argcomplete.autocomplete(parser)
args = parser.parse_args()
I was expecting argcomplete to work with the first snippet. Hope that help 👍
from argcomplete.
Argcomplete can not possibly know what the possible values are in the first case.
If you specify the possible option choices like follows the first method should also work:
parser.add_argument('move', choices=['rock', 'paper', 'scissors'])
The second option is however the preferred way to achieve this result in my opinion.
from argcomplete.
The confusion arises from the fact that this works:
parser = argparse.ArgumentParser(description='description')
parser.add_argument("--option", dest="option", help="optional option help")
argcomplete.autocomplete(parser)
args = parser.parse_args()
but this does not:
parser = argparse.ArgumentParser(description='description')
parser.add_argument("option", dest="option", help="positional option help")
argcomplete.autocomplete(parser)
args = parser.parse_args()
A positional argument may not necessarily have "choices". So if there were no choices I would still have to create a subparser to autocomplete the positional argument. That, to me, is less intuitive. Following demonstrates how to autocomplete a positional argument that doesn't have "choices"
parser = argparse.ArgumentParser(description='description')
subparsers = parser.add_subparsers()
parser_option = subparsers.add_parser("option", help="positional option help")
argcomplete.autocomplete(parser)
args = parser.parse_args()
from argcomplete.
Your last example does have 'choices'. The only valid thing a user can type is literally option
because that is the name of your subparser. This can be easily visualized by running the example and letting argparse do its job:
$ python test.py
usage: test.py [-h] {option} ...
test.py: error: too few arguments
$ python test.py foobar
usage: test.py [-h] {option} ...
test.py: error: invalid choice: 'foobar' (choose from 'option')
In general you can't expect argcomplete to complete stuff that it doesn't know the valid values for. In the first example the only valid value is ---option
in the third it's option
. But the positional argument in the second example will accept any user-supplied value and therefor can't be completed.
from argcomplete.
I would suggest an explicit mention for that in the doc.
from argcomplete.
@shehzadi, I think you're a bit confused about how argparse works. The rule of thumb for argcomplete is that it won't generate an invalid completion (one that causes the argparse parser to throw an error). If we were to complete anything on a positional that we don't have any choices information for, the completions would not be valid (to our best knowledge, anyway). There is nothing to complete on a positional with no completion information. However, once you declare a subparser, we do have the ability to complete on the subparser name.
The main thing you need to do is just look at the form of arguments that you supply to your program, and look at how you can inform argcomplete about what to suggest at any point in the command line.
Ultimately, what you might be looking for is out-of-band help information for completions: in other words, in addition to suggesting completions to your command line, the tab completion driver would also print - not into the command line! - help information for the option currently under the cursor. I know Zsh has this capability, but unfortunately I'm not aware of any way for bash to support it (though perhaps I could try printing to stderr).
If you have any specific wording for a warning that you want me to insert into the doc (including examples), please feel free to paste it here.
from argcomplete.
@felskrone, please let us know if the discussion (and examples with the "choices" parameters) above also answer your questions.
from argcomplete.
Yeah, this would be good if the positionals were autocompleted like
foocommand foosubcommand foosubsubcommand {positional_param_1} {positional_param_2}
from argcomplete.
@gnydick The consensus from this discussion is that completion of subcommands works if your argument parser is built using subparsers. If you're not able to get this working the way you want, please open a new issue and include a code example.
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 6
- Inconsistent completion when using a custom validator between `fish` and `bash` HOT 1
- Consider using completer declaration other than "-default-" for zsh global completion 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 7
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.