forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completion: autogenerate (iterative#3921)
* cli: split main_parser * cli: basic parser tree printing * cli: major completion tidy * completion: more missing root commands * completion: much automagic such beauty - fixes iterative#2985 automatically generate bash completion * tests: bash completion * tests: completion: tidy script a little * minor tidy * command: positional name consistency * completion: bash: add logging * double-check renames and consistency * cli: add completion subcommand * ci: completion: update test * minor logging * more completion logging verbosity * fix completion of files * add shtab * purge unneeded completion => shtab * completion: neaten UI * completion: use `shtab>=0.0.2` * completion: purge hardcoded completions! * revert unneeded renames for now * fix isort * completion: tidy CLI API * fix snap build * fix snap build again * snap: internally build completion * allow repo-less completion * command: tidy headless logic * use CmdBaseNoRepo * better DVC-file completion * merge command.choices -> command.completion * drop choices class * remove completion optionals * tidy docs
- Loading branch information
Showing
28 changed files
with
181 additions
and
518 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import argparse | ||
import logging | ||
|
||
import shtab | ||
|
||
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)'"}, | ||
} | ||
PREAMBLE = { | ||
"bash": """ | ||
# $1=COMP_WORDS[1] | ||
_dvc_compgen_DVCFiles() { | ||
compgen -d -S '/' -- $1 # recurse into subdirs | ||
compgen -f -X '!*?.dvc' -- $1 | ||
compgen -f -X '!*Dvcfile' -- $1 | ||
compgen -f -X '!*dvc.yaml' -- $1 | ||
} | ||
""", | ||
"zsh": "", | ||
} | ||
|
||
|
||
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], | ||
) | ||
print(script) | ||
return 0 | ||
|
||
|
||
def add_parser(subparsers, parent_parser): | ||
COMPLETION_HELP = "Generate shell tab completion." | ||
COMPLETION_DESCRIPTION = "Prints out shell tab completion scripts." | ||
completion_parser = subparsers.add_parser( | ||
"completion", | ||
parents=[parent_parser], | ||
description=append_doc_link(COMPLETION_DESCRIPTION, "completion"), | ||
help=COMPLETION_HELP, | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
) | ||
completion_parser.add_argument( | ||
"-s", | ||
"--shell", | ||
help="Shell syntax for completions.", | ||
default="bash", | ||
choices=["bash", "zsh"], | ||
) | ||
completion_parser.set_defaults(func=CmdCompletion) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.