Giter Club home page Giter Club logo

Comments (13)

kislyuk avatar kislyuk commented on August 17, 2024

Yes, this works out of the box. Have you tried using it?

from argcomplete.

felskrone avatar felskrone commented on August 17, 2024

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.

shehzadi avatar shehzadi commented on August 17, 2024

I'm seeing the same problem. Default does not work for options without dashes.

from argcomplete.

kislyuk avatar kislyuk commented on August 17, 2024

Thanks for the report. Can you please paste a test use case that you expect to work?

from argcomplete.

shehzadi avatar shehzadi commented on August 17, 2024

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.

wapiflapi avatar wapiflapi commented on August 17, 2024

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.

shehzadi avatar shehzadi commented on August 17, 2024

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.

wapiflapi avatar wapiflapi commented on August 17, 2024

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.

shehzadi avatar shehzadi commented on August 17, 2024

I would suggest an explicit mention for that in the doc.

from argcomplete.

kislyuk avatar kislyuk commented on August 17, 2024

@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.

kislyuk avatar kislyuk commented on August 17, 2024

@felskrone, please let us know if the discussion (and examples with the "choices" parameters) above also answer your questions.

from argcomplete.

gnydick avatar gnydick commented on August 17, 2024

Yeah, this would be good if the positionals were autocompleted like

foocommand foosubcommand foosubsubcommand {positional_param_1} {positional_param_2}

from argcomplete.

evanunderscore avatar evanunderscore commented on August 17, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.