iterative / shtab

↔️ Automagic shell tab completion for Python CLI applications
https://docs.iterative.ai/shtab
Other
363 stars 35 forks source link

[Bug] bash autocompletion has strange behavior under certain conditions #116

Open alkatar21 opened 1 year ago

alkatar21 commented 1 year ago

dvc==2.31.0 shtab==1.5.6

I for example have dvc stage add --name Test -d folder/file -w and want to remove folder/file and replace it. Than -w is autocompleted (dvc stage add --name Test -d -w -w) instead of an option for folders. So for some reason the autocompletion with tab adds a -w instead of autocompletion for files and folders in this case. But the autocompletion works if nothing follows after -d. This gif shows the behavior: Bug1

An reproducable example is the following provided by pared#9484 on the Discord server:

#!/usr/bin/env python3

import argparse
import shtab

parser = argparse.ArgumentParser()
shtab.add_argument_to(parser, ["--print-completion"])

subparsers = parser.add_subparsers()

subparser1 = subparsers.add_parser('bla', help="hi")
subparser1.add_argument(
            "-d",
            "--directory",
            action="store",
            required=True,
            dest="dir",
            help="this is directory",
        ).complete = shtab.DIRECTORY  # type: ignore
subparser1.add_argument(
        "-w",
        "--wdir",
        help="Directory within your repo to run your command in.",
        metavar="<path>",
    )
subparser2 = subparsers.add_parser('blub', help="hi")

args = parser.parse_args()

Further context: https://discord.com/channels/485586884165107732/485596304961962003/1036650841425313843

casperdcl commented 1 year ago

Ah so dvc stage add -d <TAB> works, but dvc stage add -d <TAB> -w (tabbing in the middle of the command) doesn't work.

Happy to accept a PR fixing this (if at all possible).