Giter Club home page Giter Club logo

Comments (10)

kislyuk avatar kislyuk commented on July 18, 2024

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.

asmeurer avatar asmeurer commented on July 18, 2024

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.

asmeurer avatar asmeurer commented on July 18, 2024

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.

kislyuk avatar kislyuk commented on July 18, 2024

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.

asmeurer avatar asmeurer commented on July 18, 2024

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.

kislyuk avatar kislyuk commented on July 18, 2024
#!/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.

asmeurer avatar asmeurer commented on July 18, 2024

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.

kislyuk avatar kislyuk commented on July 18, 2024

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.

bewest avatar bewest commented on July 18, 2024

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.

asmeurer avatar asmeurer commented on July 18, 2024

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)

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.