Esempio n. 1
0
def main():
    args = parser.parse_args()
    if args.print_bash_completion:
        print(shtab.complete(parser, shell="bash"))
        sys.exit(0)
    if args.print_zsh_completion:
        print(shtab.complete(parser, shell="zsh"))
        sys.exit(0)

    msg = "k thx bai!" if args.goodbye else "hai!"
    print("{} says '{}' to {}".format(args.me, msg, args.you))
Esempio n. 2
0
def usage(args, **kwargs):
    """Handle Main repodoc Entrypoint without subcommands."""
    if args.bash_completion:
        import shtab

        print(shtab.complete(args.parser, shell="bash"))
        return
    if args.zsh_completion:
        import shtab

        print(shtab.complete(args.parser, shell="zsh"))
        return
Esempio n. 3
0
def generate_completion(parser, args):
    try:
        import shtab
        print(shtab.complete(parser, shell=args.shell))
    except ImportError:  # pragma: no cover
        print('shtab not found; install via `pip install shtab`')
        return 1
Esempio n. 4
0
    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)
        print(script)
        return 0
Esempio n. 5
0
    def run(self):
        from dvc.cli.parser import get_main_parser

        parser = get_main_parser()
        shell = self.args.shell
        script = shtab.complete(parser, shell=shell, preamble=PREAMBLE)
        ui.write(script, force=True)
        return 0
Esempio n. 6
0
    def run(self):
        import shtab

        shell = self.args.shell
        parser = self.args.parser
        script = shtab.complete(parser, shell=shell, preamble=PREAMBLE)
        ui.write(script, force=True)
        return 0
Esempio n. 7
0
def test_complete(shell, caplog):
    parser = get_main_parser()
    with caplog.at_level(logging.INFO):
        completion = shtab.complete(parser, shell=shell)
    print(completion)

    if shell == "bash":
        shell = Bash(completion)
        shell.compgen('-W "$_shtab_shtab_options_"', "--h", "--help")

    assert not caplog.record_tuples
Esempio n. 8
0
def generate_completion(parser, subparser, args, extra):
    if extra:
        subparser.error('unrecognized arguments: {}'.format(' '.join(extra)))

    try:
        import shtab
        p = parser if args.program == 'bfg9000' else simple_parser()
        print(shtab.complete(p, shell=args.shell))
    except ImportError:  # pragma: no cover
        print('shtab not found; install via `pip install shtab`')
        return 1
Esempio n. 9
0
def test_positional_choices(shell, caplog):
    parser = ArgumentParser(prog="test")
    parser.add_argument("posA", choices=["one", "two"])
    with caplog.at_level(logging.INFO):
        completion = shtab.complete(parser, shell=shell)
    print(completion)

    if shell == "bash":
        shell = Bash(completion)
        shell.compgen('-W "$_shtab_test_commands_"', "o", "one")

    assert not caplog.record_tuples
Esempio n. 10
0
def test_add_argument_to_optional(shell, caplog):
    parser = ArgumentParser(prog="test")
    shtab.add_argument_to(parser, ["-s", "--shell"])
    with caplog.at_level(logging.INFO):
        completion = shtab.complete(parser, shell=shell)
    print(completion)

    if shell == "bash":
        shell = Bash(completion)
        shell.compgen('-W "$_shtab_test_options_"', "--s", "--shell")

    assert not caplog.record_tuples
Esempio n. 11
0
    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
Esempio n. 12
0
    def __call__(self, parser, namespace, values, option_string=None):
        if not HAS_AUTOCOMPLETE_SUPPORT:
            raise ImportError(
                "Unable to export to shell. shtab is not installed")

        if values in SUPPORTED_SHELLS:
            print(shtab.complete(parser, values))
            sys.stderr.write(
                f"Completed writing {values} shell output to stdout\n")
            raise TerminalEagerCommand

        raise ValueError(
            f"Unsupported shell type ({values}. Supported shells {SUPPORTED_SHELLS} "
        )
Esempio n. 13
0
def test_positional_choices(shell, caplog):
    parser = ArgumentParser(prog="test")
    parser.add_argument("posA", choices=["one", "two"])
    with caplog.at_level(logging.INFO):
        completion = shtab.complete(parser, shell=shell)
    print(completion)

    if shell == "bash":
        shell = Bash(completion)
        # ideally should check completion
        # https://github.com/iterative/shtab/issues/11
        shell.compgen('-W "$_shtab_test_options_"', "", "-h --help")

    assert not caplog.record_tuples
Esempio n. 14
0
def test_add_argument_to_positional(shell, caplog):
    parser = ArgumentParser(prog="test")
    subparsers = parser.add_subparsers()
    sub = subparsers.add_parser("completion")
    shtab.add_argument_to(sub, "shell")
    with caplog.at_level(logging.INFO):
        completion = shtab.complete(parser, shell=shell)
    print(completion)

    if shell == "bash":
        shell = Bash(completion)
        shell.compgen('-W "$_shtab_test_completion"', "ba", "bash")
        shell.compgen('-W "$_shtab_test_completion"', "z", "zsh")

    assert not caplog.record_tuples
Esempio n. 15
0
def test_custom_complete(shell, caplog):
    parser = ArgumentParser(prog="test")
    parser.add_argument("posA").complete = {"bash": "_shtab_test_some_func"}
    preamble = {
        "bash": "_shtab_test_some_func() { compgen -W 'one two' -- $1 ;}"
    }
    with caplog.at_level(logging.INFO):
        completion = shtab.complete(parser, shell=shell, preamble=preamble)
    print(completion)

    if shell == "bash":
        shell = Bash(completion)
        shell.test('"$($_shtab_test_COMPGEN o)" = "one"')

    assert not caplog.record_tuples
Esempio n. 16
0
"""
import argparse

import shtab  # for completion magic


def get_main_parser():
    parser = argparse.ArgumentParser(prog="pathcomplete")
    parser.add_argument(
        "-s",
        "--print-completion-shell",
        choices=["bash", "zsh"],
        help="prints completion script",
    )
    # file & directory tab complete
    parser.add_argument("file", nargs="?").complete = shtab.FILE
    parser.add_argument("--dir", default=".").complete = shtab.DIRECTORY
    return parser


if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()

    # completion magic
    shell = args.print_completion_shell
    if shell:
        print(shtab.complete(parser, shell=shell))
    else:
        print("received <file>=%r --dir=%r" % (args.file, args.dir))
Esempio n. 17
0
#!/usr/bin/env python
"""Greetings and partings.

Usage:
  greeter [options] [<you>] [<me>]

Options:
  -g, --goodbye  : Say "goodbye" (instead of "hello")
  -b, --print-bash-completion  : Output a bash tab-completion script
  -z, --print-zsh-completion  : Output a zsh tab-completion script

Arguments:
  <you>  : Your name [default: Anon]
  <me>  : My name [default: Casper]
"""
import sys, argopt, shtab  # NOQA

parser = argopt.argopt(__doc__)
if __name__ == "__main__":
    args = parser.parse_args()
    if args.print_bash_completion:
        print(shtab.complete(parser, shell="bash"))
        sys.exit(0)
    if args.print_zsh_completion:
        print(shtab.complete(parser, shell="zsh"))
        sys.exit(0)

    msg = "k thx bai!" if args.goodbye else "hai!"
    print("{} says '{}' to {}".format(args.me, msg, args.you))
Esempio n. 18
0
        "-s",
        "--print-completion-shell",
        choices=["bash", "zsh"],
        help="prints completion script",
    )
    # `*.txt` file tab completion
    parser.add_argument("input_txt", nargs="?").complete = TXT_FILE
    # file tab completion builtin shortcut
    parser.add_argument("-i", "--input-file").complete = shtab.FILE
    parser.add_argument(
        "-o",
        "--output-name",
        help=("output file name. Completes directory names to avoid users"
              " accidentally overwriting existing files."),
    ).complete = shtab.DIRECTORY  # directory tab completion builtin shortcut
    return parser


if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()

    # completion magic
    shell = args.print_completion_shell
    if shell:
        script = shtab.complete(parser, shell=shell, preamble=PREAMBLE)
        print(script)
    else:
        print("received <input_txt>=%r --output-dir=%r --output-name=%r" %
              (args.input_txt, args.output_dir, args.output_name))