diff --git a/dvc/command/add.py b/dvc/command/add.py index 59f6e728be..e81631d525 100644 --- a/dvc/command/add.py +++ b/dvc/command/add.py @@ -63,10 +63,6 @@ def add_parser(subparsers, parent_parser): metavar="", ) parser.add_argument( - "targets", - nargs="+", - help="Input files/directories to add.", - metavar="targets", - choices=completion.Required.FILE, - ) + "targets", nargs="+", help="Input files/directories to add.", + ).complete = completion.FILE parser.set_defaults(func=CmdAdd) diff --git a/dvc/command/cache.py b/dvc/command/cache.py index 0de9aeede9..f1186a7494 100644 --- a/dvc/command/cache.py +++ b/dvc/command/cache.py @@ -56,7 +56,5 @@ def add_parser(subparsers, parent_parser): help="Path to cache directory. Relative paths are resolved relative " "to the current directory and saved to config relative to the " "config file location.", - metavar="value", - choices=completion.Required.DIR, - ) + ).complete = completion.DIR cache_dir_parser.set_defaults(func=CmdCacheDir) diff --git a/dvc/command/checkout.py b/dvc/command/checkout.py index 2d30631ac6..ecc750e62e 100644 --- a/dvc/command/checkout.py +++ b/dvc/command/checkout.py @@ -114,7 +114,5 @@ def add_parser(subparsers, parent_parser): nargs="*", help="DVC-files to checkout. Optional. " "(Finds all DVC-files in the workspace by default.)", - metavar="targets", - choices=completion.Optional.DVC_FILE, - ) + ).complete = completion.DVC_FILE checkout_parser.set_defaults(func=CmdCheckout) diff --git a/dvc/command/commit.py b/dvc/command/commit.py index 56a4e66d51..c5a8276ea0 100644 --- a/dvc/command/commit.py +++ b/dvc/command/commit.py @@ -67,7 +67,5 @@ def add_parser(subparsers, parent_parser): nargs="*", help="DVC-files to commit. Optional. " "(Finds all DVC-files in the workspace by default.)", - metavar="targets", - choices=completion.Optional.DVC_FILE, - ) + ).complete = completion.DVC_FILE commit_parser.set_defaults(func=CmdCommit) diff --git a/dvc/command/completion.py b/dvc/command/completion.py index 6cef7c526a..644a411ed4 100644 --- a/dvc/command/completion.py +++ b/dvc/command/completion.py @@ -6,9 +6,11 @@ from dvc.command.base import CmdBaseNoRepo, append_doc_link logger = logging.getLogger(__name__) -CHOICE_FUNCTIONS = { - "bash": {"DVCFile": "_dvc_compgen_DVCFiles"}, - "zsh": {"DVCFile": "_files -g '(*?.dvc|Dvcfile|dvc.yaml)'"}, +FILE = shtab.FILE +DIR = shtab.DIRECTORY +DVC_FILE = { + "bash": "_dvc_compgen_DVCFiles", + "zsh": "_files -g '(*?.dvc|Dvcfile|dvc.yaml)'", } PREAMBLE = { "bash": """ @@ -24,26 +26,13 @@ } -class Optional(shtab.Optional): - DVC_FILE = [shtab.Choice("DVCFile", required=False)] - - -class Required(shtab.Required): - DVC_FILE = [shtab.Choice("DVCFile", required=True)] - - class CmdCompletion(CmdBaseNoRepo): def run(self): from dvc.cli import get_main_parser parser = get_main_parser() shell = self.args.shell - script = shtab.complete( - parser, - shell=shell, - preamble=PREAMBLE[shell], - choice_functions=CHOICE_FUNCTIONS[shell], - ) + script = shtab.complete(parser, shell=shell, preamble=PREAMBLE) print(script) return 0 diff --git a/dvc/command/daemon.py b/dvc/command/daemon.py index e7c977399f..daec0e8cef 100644 --- a/dvc/command/daemon.py +++ b/dvc/command/daemon.py @@ -66,9 +66,6 @@ def add_parser(subparsers, parent_parser): help=DAEMON_ANALYTICS_HELP, ) daemon_analytics_parser.add_argument( - "target", - help="Analytics file.", - metavar="target", - choices=completion.Required.FILE, - ) + "target", help="Analytics file.", + ).complete = completion.FILE daemon_analytics_parser.set_defaults(func=CmdDaemonAnalytics) diff --git a/dvc/command/data_sync.py b/dvc/command/data_sync.py index 413735751b..3701c59d97 100644 --- a/dvc/command/data_sync.py +++ b/dvc/command/data_sync.py @@ -105,9 +105,7 @@ def shared_parent_parser(): nargs="*", help="Limit command scope to these DVC-files. " "Using -R, directories to search DVC-files in can also be given.", - metavar="targets", - choices=completion.Optional.DVC_FILE, - ) + ).complete = completion.DVC_FILE return parent_parser diff --git a/dvc/command/freeze.py b/dvc/command/freeze.py index 7044569e50..14b319f0ec 100644 --- a/dvc/command/freeze.py +++ b/dvc/command/freeze.py @@ -40,12 +40,8 @@ def add_parser(subparsers, parent_parser): formatter_class=argparse.RawDescriptionHelpFormatter, ) freeze_parser.add_argument( - "targets", - nargs="+", - help="Stages or .dvc files to freeze", - metavar="targets", - choices=completion.Required.DVC_FILE, - ) + "targets", nargs="+", help="Stages or .dvc files to freeze", + ).complete = completion.DVC_FILE freeze_parser.set_defaults(func=CmdFreeze) UNFREEZE_HELP = "Unfreeze stages or .dvc files." @@ -57,10 +53,6 @@ def add_parser(subparsers, parent_parser): formatter_class=argparse.RawDescriptionHelpFormatter, ) unfreeze_parser.add_argument( - "targets", - nargs="+", - help="Stages or .dvc files to unfreeze", - metavar="targets", - choices=completion.Required.DVC_FILE, - ) + "targets", nargs="+", help="Stages or .dvc files to unfreeze", + ).complete = completion.DVC_FILE unfreeze_parser.set_defaults(func=CmdUnfreeze) diff --git a/dvc/command/get.py b/dvc/command/get.py index 404aa3e48e..7f27e34fef 100644 --- a/dvc/command/get.py +++ b/dvc/command/get.py @@ -63,19 +63,15 @@ def add_parser(subparsers, parent_parser): "url", help="Location of DVC or Git repository to download from" ) get_parser.add_argument( - "path", - help="Path to a file or directory within the repository", - metavar="path", - choices=completion.Required.FILE, - ) + "path", help="Path to a file or directory within the repository", + ).complete = completion.FILE get_parser.add_argument( "-o", "--out", nargs="?", help="Destination path to download files to", metavar="", - choices=completion.Optional.DIR, - ) + ).complete = completion.DIR get_parser.add_argument( "--rev", nargs="?", diff --git a/dvc/command/get_url.py b/dvc/command/get_url.py index 214b2739c4..015e4119ba 100644 --- a/dvc/command/get_url.py +++ b/dvc/command/get_url.py @@ -34,10 +34,6 @@ def add_parser(subparsers, parent_parser): "url", help="See `dvc import-url -h` for full list of supported URLs." ) get_parser.add_argument( - "out", - nargs="?", - help="Destination path to put data to.", - metavar="out", - choices=completion.Optional.DIR, - ) + "out", nargs="?", help="Destination path to put data to.", + ).complete = completion.DIR get_parser.set_defaults(func=CmdGetUrl) diff --git a/dvc/command/imp.py b/dvc/command/imp.py index e11116ce60..524f288499 100644 --- a/dvc/command/imp.py +++ b/dvc/command/imp.py @@ -44,19 +44,15 @@ def add_parser(subparsers, parent_parser): "url", help="Location of DVC or Git repository to download from" ) import_parser.add_argument( - "path", - help="Path to a file or directory within the repository", - metavar="path", - choices=completion.Required.FILE, - ) + "path", help="Path to a file or directory within the repository", + ).complete = completion.FILE import_parser.add_argument( "-o", "--out", nargs="?", help="Destination path to download files to", metavar="", - choices=completion.Optional.DIR, - ) + ).complete = completion.DIR import_parser.add_argument( "--rev", nargs="?", diff --git a/dvc/command/imp_url.py b/dvc/command/imp_url.py index ea7c7290cd..4451b8e298 100644 --- a/dvc/command/imp_url.py +++ b/dvc/command/imp_url.py @@ -58,18 +58,13 @@ def add_parser(subparsers, parent_parser): "remote://myremote/path/to/file (see `dvc remote`)", ) import_parser.add_argument( - "out", - nargs="?", - help="Destination path to put files to.", - metavar="out", - choices=completion.Optional.DIR, - ) + "out", nargs="?", help="Destination path to put files to.", + ).complete = completion.DIR import_parser.add_argument( "--file", help="Specify name of the DVC-file this command will generate.", metavar="", - choices=completion.Optional.DIR, - ) + ).complete = completion.DIR import_parser.add_argument( "--no-exec", action="store_true", diff --git a/dvc/command/ls/__init__.py b/dvc/command/ls/__init__.py index a24d640907..62d9e844ed 100644 --- a/dvc/command/ls/__init__.py +++ b/dvc/command/ls/__init__.py @@ -75,7 +75,5 @@ def add_parser(subparsers, parent_parser): "path", nargs="?", help="Path to directory within the repository to list outputs for", - metavar="path", - choices=completion.Optional.DIR, - ) + ).complete = completion.DIR list_parser.set_defaults(func=CmdList) diff --git a/dvc/command/metrics.py b/dvc/command/metrics.py index 7d669b9036..fcffba8fd5 100644 --- a/dvc/command/metrics.py +++ b/dvc/command/metrics.py @@ -170,9 +170,7 @@ def add_parser(subparsers, parent_parser): "Limit command scope to these metric files. Using -R, " "directories to search metric files in can also be given." ), - metavar="targets", - choices=completion.Optional.FILE, - ) + ).complete = completion.FILE metrics_show_parser.add_argument( "-a", "--all-branches", @@ -238,8 +236,7 @@ def add_parser(subparsers, parent_parser): "directories to search metric files in can also be given." ), metavar="", - choices=completion.Optional.FILE, - ) + ).complete = completion.FILE metrics_diff_parser.add_argument( "-R", "--recursive", diff --git a/dvc/command/move.py b/dvc/command/move.py index 0aae9999ed..cffcd1ef82 100644 --- a/dvc/command/move.py +++ b/dvc/command/move.py @@ -37,15 +37,9 @@ def add_parser(subparsers, parent_parser): formatter_class=argparse.RawDescriptionHelpFormatter, ) move_parser.add_argument( - "src", - help="Source path to a data file or directory.", - metavar="src", - choices=completion.Required.FILE, - ) + "src", help="Source path to a data file or directory.", + ).complete = completion.FILE move_parser.add_argument( - "dst", - help="Destination path.", - metavar="dst", - choices=completion.Required.FILE, - ) + "dst", help="Destination path.", + ).complete = completion.FILE move_parser.set_defaults(func=CmdMove) diff --git a/dvc/command/plots.py b/dvc/command/plots.py index 2a6d80d492..8aaf07d8f2 100644 --- a/dvc/command/plots.py +++ b/dvc/command/plots.py @@ -129,9 +129,7 @@ def add_parser(subparsers, parent_parser): "targets", nargs="*", help="Plots files to visualize. Shows all plots by default.", - metavar="targets", - choices=completion.Optional.FILE, - ) + ).complete = completion.FILE _add_props_arguments(plots_show_parser) _add_output_arguments(plots_show_parser) plots_show_parser.set_defaults(func=CmdPlotsShow) @@ -152,8 +150,7 @@ def add_parser(subparsers, parent_parser): nargs="*", help="Plots file to visualize. Shows all plots by default.", metavar="", - choices=completion.Optional.FILE, - ) + ).complete = completion.FILE plots_diff_parser.add_argument( "revisions", nargs="*", default=None, help="Git commits to plot from", ) @@ -170,11 +167,8 @@ def add_parser(subparsers, parent_parser): formatter_class=argparse.RawDescriptionHelpFormatter, ) plots_modify_parser.add_argument( - "target", - help="Metric file to set properties to", - metavar="target", - choices=completion.Required.FILE, - ) + "target", help="Metric file to set properties to", + ).complete = completion.FILE _add_props_arguments(plots_modify_parser) plots_modify_parser.add_argument( "--unset", @@ -198,8 +192,7 @@ def _add_props_arguments(parser): ) ), metavar="", - choices=completion.Optional.FILE, - ) + ).complete = completion.FILE parser.add_argument( "-x", default=None, help="Field name for X axis.", metavar="" ) @@ -231,8 +224,7 @@ def _add_output_arguments(parser): default=None, help="Destination path to save plots to", metavar="", - choices=completion.Optional.DIR, - ) + ).complete = completion.DIR parser.add_argument( "--show-vega", action="store_true", diff --git a/dvc/command/remove.py b/dvc/command/remove.py index b3d833bf15..a816ac031e 100644 --- a/dvc/command/remove.py +++ b/dvc/command/remove.py @@ -37,10 +37,6 @@ def add_parser(subparsers, parent_parser): help="Remove outputs as well.", ) remove_parser.add_argument( - "targets", - nargs="+", - help="DVC-files to remove.", - metavar="targets", - choices=completion.Required.DVC_FILE, - ) + "targets", nargs="+", help="DVC-files to remove.", + ).complete = completion.DVC_FILE remove_parser.set_defaults(func=CmdRemove) diff --git a/dvc/command/repro.py b/dvc/command/repro.py index e5c244aae3..986b9f0b48 100644 --- a/dvc/command/repro.py +++ b/dvc/command/repro.py @@ -72,9 +72,7 @@ def add_parser(subparsers, parent_parser): "targets", nargs="*", help=f"Stages to reproduce. '{PIPELINE_FILE}' by default.", - metavar="targets", - choices=completion.Optional.DVC_FILE, - ) + ).complete = completion.DVC_FILE repro_parser.add_argument( "-f", "--force", diff --git a/dvc/command/unprotect.py b/dvc/command/unprotect.py index afbc19841c..e066dc493a 100644 --- a/dvc/command/unprotect.py +++ b/dvc/command/unprotect.py @@ -33,10 +33,6 @@ def add_parser(subparsers, parent_parser): formatter_class=argparse.RawDescriptionHelpFormatter, ) unprotect_parser.add_argument( - "targets", - nargs="+", - help="Data files/directories to unprotect.", - metavar="targets", - choices=completion.Required.FILE, - ) + "targets", nargs="+", help="Data files/directories to unprotect.", + ).complete = completion.FILE unprotect_parser.set_defaults(func=CmdUnprotect) diff --git a/dvc/command/update.py b/dvc/command/update.py index 20f1b1fda0..2bd50b9a09 100644 --- a/dvc/command/update.py +++ b/dvc/command/update.py @@ -33,12 +33,8 @@ def add_parser(subparsers, parent_parser): formatter_class=argparse.RawDescriptionHelpFormatter, ) update_parser.add_argument( - "targets", - nargs="+", - help="DVC-files to update.", - metavar="targets", - choices=completion.Required.DVC_FILE, - ) + "targets", nargs="+", help="DVC-files to update.", + ).complete = completion.DVC_FILE update_parser.add_argument( "--rev", nargs="?", diff --git a/setup.py b/setup.py index 696f37e58e..815ef55019 100644 --- a/setup.py +++ b/setup.py @@ -78,7 +78,7 @@ def run(self): "tabulate>=0.8.7", "pygtrie==2.3.2", "dpath>=2.0.1,<3", - "shtab>=1.0.3", + "shtab>=1.1.0,<2", ]