Exemple #1
0
  def testCompletionWithSubparser(self):
    """Verify that completion also works with sub parsers."""
    parser = CompletingArgumentParser(prog="subfoo", add_help=False)
    parser.add_argument("pos")
    parser.add_argument("--foo", action="store_true")

    subparsers = parser.add_subparsers()
    bar = subparsers.add_parser("bar", add_help=False)
    bar.add_argument("fooz")
    bar.add_argument("-b", "--baz", action="store_true")

    foobar = subparsers.add_parser("foobar")
    foobar.add_argument("--foobar", action="store_true")

    subparsers2 = foobar.add_subparsers()
    foobarbaz = subparsers2.add_parser("foobarbaz", add_help=False)
    foobarbaz.add_argument("--test", action="store_true")

    self.performCompletion(parser, ["-"], {"--foo"})
    self.performCompletion(parser, ["b"], {"bar"})
    self.performCompletion(parser, ["bar", ""], {"-b", "--baz"})
    self.performCompletion(parser, ["bar", "hallo", ""], {"-b", "--baz"})
    self.performCompletion(parser, ["positional", "bar", "hallo", ""], {"-b", "--baz"})
    self.performCompletion(parser, ["foobar", ""], {"foobarbaz", "-h", "--foobar", "--help"})
    self.performCompletion(parser, ["--foo", "foobar", ""], {"foobarbaz", "-h", "--foobar", "--help"})
    self.performCompletion(parser, ["foobar", "--f"], {"--foobar"})
    self.performCompletion(parser, ["foobar", "f"], {"foobarbaz"})
    self.performCompletion(parser, ["foobar", "--foobar", "foobarbaz", ""], {"--test"})
    self.performCompletion(parser, ["--foo", "foobar", "foobarbaz", ""], {"--test"})
Exemple #2
0
        def doTest(known_only=False):
            """Perform the completion test."""
            k = {"known_only": known_only}
            parser = CompletingArgumentParser(prog="foo", add_help=False)

            parser.add_argument("--foo", action="store_true")
            self.performCompletion(parser, ["-"], {"--foo"}, **k)

            parser.add_argument("-b", "--bar", action="store_true")
            self.performCompletion(parser, ["-"], {"--foo", "-b", "--bar"},
                                   **k)
            self.performCompletion(parser, ["--"], {"--foo", "--bar"}, **k)
            self.performCompletion(parser, ["-b"], {"-b"}, **k)
            self.performCompletion(parser, ["-b", "--foo", "-b"], {"-b"}, **k)
            self.performCompletion(parser, ["-b", "--foo", ""],
                                   {"--foo", "-b", "--bar"}, **k)

            # Also verify that an error is reported if there is no matching
            # completion.
            self.performCompletion(parser, ["--var"], set(), exit_code=1, **k)
            self.performCompletion(parser, ["-z"], set(), exit_code=1, **k)
            self.performCompletion(parser, ["-b", "-a"],
                                   set(),
                                   exit_code=1,
                                   **k)
Exemple #3
0
  def testCompleteAfterPositional(self):
    """Verify that a single keyword argument following a positional argument can be completed."""
    parser = CompletingArgumentParser(add_help=False)
    parser.add_argument("positional")
    parser.add_argument("--foo", action="store_true")

    self.performCompletion(parser, ["foobar", ""], {"--foo"})
Exemple #4
0
  def testHelpCompletion(self):
    """Verify that the -h/--help arguments can be completed properly."""
    parser = CompletingArgumentParser(prog="foo")
    parser.add_argument("--foo", action="store_true")

    self.performCompletion(parser, ["-"], {"-h", "--foo", "--help"})
    self.performCompletion(parser, ["--h"], {"--help"})
Exemple #5
0
  def testCompleteWithCompleter(self):
    """Verify that the 'completer' argument works as expected."""
    def localFileCompleter(parser, values, word):
      """A completer for files in the current working directory."""
      for value in listdir():
        if value.startswith(word):
          yield value

    def doTest(parser, args):
      """Perform the completion test."""
      with TemporaryDirectory() as dir_,\
           NamedTemporaryFile(dir=dir_) as f1,\
           NamedTemporaryFile(dir=dir_) as f2,\
           NamedTemporaryFile(dir=dir_) as f3,\
           NamedTemporaryFile(dir=dir_) as f4,\
           NamedTemporaryFile(dir=dir_) as f5:
        all_files = set(map(basename, {f1.name, f2.name, f3.name, f4.name, f5.name}))

        old_cwd = getcwd()
        chdir(dir_)
        try:
          self.performCompletion(parser, args + [""], all_files | {"-h", "--help"})
          self.performCompletion(parser, args + ["no-file", ""], all_files | {"-h", "--help"})
        finally:
          chdir(old_cwd)

    parser = CompletingArgumentParser(prog="completer")
    parser.add_argument("test", completer=localFileCompleter, nargs="+")
    doTest(parser, [])

    parser = CompletingArgumentParser(prog="subcompleter")
    subparsers = parser.add_subparsers()
    subparser = subparsers.add_parser("subparser")
    subparser.add_argument("test", completer=localFileCompleter, nargs="+")
    doTest(parser, ["subparser"])
Exemple #6
0
  def testNoArgumentInNamespace(self):
    """Verify that the completion leaves no argument in the resulting namespace."""
    parser = CompletingArgumentParser(prog="test")
    parser.add_argument("--test", action="store_true")

    namespace = parser.parse_args(["--test"])
    self.assertEqual(vars(namespace), {"test": True})
Exemple #7
0
  def testCompleteChoicesForKeyword(self):
    """Verify that auto completion works if choices are given."""
    parser = CompletingArgumentParser(prog="choices")
    parser.add_argument("--move", choices=("rock", "paper", "scissors"))

    self.performCompletion(parser, ["--move", ""], {"rock", "paper", "scissors"})
    self.performCompletion(parser, ["--move", "p"], {"paper"})
Exemple #8
0
  def testCompleteSinglePositionalMax(self):
    """Verify that parser-level positionals can be exhausted."""
    parser = CompletingArgumentParser(prog="posLimited")
    parser.add_argument("positionals", nargs="?")

    self.performCompletion(parser, ["-"], {"-h", "--help"})
    self.performCompletion(parser, ["pos1", "--"], {"--help"})
    self.performCompletion(parser, ["pos1", "pos2", ""], set(), exit_code=1)
Exemple #9
0
  def testCompleteAnyPositionals(self):
    """Verify that we can handle a '*' parser-level positional argument correctly."""
    parser = CompletingArgumentParser(prog="posUnlimited")
    parser.add_argument("positionals", nargs="*")

    self.performCompletion(parser, ["-"], {"-h", "--help"})
    self.performCompletion(parser, ["pos1", "--"], {"--help"})
    self.performCompletion(parser, ["pos1", "pos2", ""], {"-h", "--help"})
Exemple #10
0
  def testCompleteFixedPositionals(self):
    """Verify that completion stops after exceeding the maximum parser-level positionals."""
    parser = CompletingArgumentParser(prog="posFixed")
    parser.add_argument("positionals", nargs=3)

    self.performCompletion(parser, ["-"], {"-h", "--help"})
    self.performCompletion(parser, ["pos1", "--"], {"--help"})
    self.performCompletion(parser, ["pos1", "pos2", ""], {"-h", "--help"})
    self.performCompletion(parser, ["pos1", "pos2", "pos3", ""], {"-h", "--help"})
    self.performCompletion(parser, ["pos1", "pos2", "pos3", "pos4", ""], set(), exit_code=1)
Exemple #11
0
  def testNonStrChoice(self):
    """Verify that non-string choices can be completed."""
    expected = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
    parser = CompletingArgumentParser(prog="nonStrChoices", add_help=False)
    parser.add_argument("addends", choices=range(10), nargs="+")

    self.performCompletion(parser, [""], expected)
    self.performCompletion(parser, ["1", ""], expected)
    self.performCompletion(parser, ["1", "9", ""], expected)
    self.performCompletion(parser, ["1337", "42", ""], expected)
Exemple #12
0
  def testKnownArgsParsing(self):
    """Verify that the parse_known_args method works as expected."""
    parser = CompletingArgumentParser(prog="known")
    parser.add_argument("--foo", action="store_false", default=True)

    namespace, remainder = parser.parse_known_args(["-f"])
    self.assertEqual(vars(namespace), {"foo": True})
    self.assertEqual(remainder, ["-f"])

    namespace, remainder = parser.parse_known_args(["--foo"])
    self.assertEqual(vars(namespace), {"foo": False})
    self.assertEqual(remainder, [])
Exemple #13
0
  def testCompleteKeywordWithCompleter(self):
    """Verify that the 'completer' argument works as expected for keyword arguments."""
    def completeKeyword(parser, values, word):
      """A completer function."""
      for choice in ("rock", "paper", "scissors"):
        if choice.startswith(word):
          yield choice

    parser = CompletingArgumentParser(prog="keywordcompleter")
    parser.add_argument("-k", "--keyword", completer=completeKeyword)

    self.performCompletion(parser, ["-k", ""], {"rock", "paper", "scissors"})
    self.performCompletion(parser, ["--keyword", ""], {"rock", "paper", "scissors"})
    self.performCompletion(parser, ["-h", "--keyword", "r"], {"rock"})
Exemple #14
0
  def testCompleteWithArgumentGroups(self):
    """Verify that argument groups are considered in completions."""
    parser = CompletingArgumentParser(prog="withGroups", add_help=False)
    parser.add_argument("-t", action="store_true")

    group1 = parser.add_argument_group("test group 1")
    group1.add_argument("-f", "--foo", action="store_true")
    group1.add_argument("-h", "--help", action="store_true")

    group2 = parser.add_argument_group("test group 2")
    group2.add_argument("--bar", action="store_true")

    self.performCompletion(parser, ["-"], {"-f", "-h", "-t", "--bar", "--foo", "--help"})
    self.performCompletion(parser, ["--"], {"--bar", "--foo", "--help"})
    self.performCompletion(parser, ["-f"], {"-f"})
Exemple #15
0
  def testCompleterParserInvocation(self):
    """Verify that the parser argument in a completer can be used properly."""
    def completeParser(parser, values, word):
      """Dummy completer simply parsing the given 'values' and returning the positional string."""
      namespace, _ = parser.parse_known_args(values)
      yield namespace.positional

    positional = "pos!"

    parser = CompletingArgumentParser()
    parser.add_argument("positional")
    parser.add_argument("--foo", completer=completeParser)

    self.performCompletion(parser, [positional, "--foo", ""], {positional})
    # Also verify that parser failures (due to a missing positional
    # argument) are handled correctly.
    self.performCompletion(parser, ["--foo", ""], set(), exit_code=1)
Exemple #16
0
  def testSubparsersCanCompleteSubCommands(self):
    """Verify that sub parsers can complete arguments themselves."""
    root = CompletingArgumentParser(prog="root", add_help=False)
    root.add_argument("--rootopt", action="store_true")

    subparsers = root.add_subparsers()
    sub1 = subparsers.add_parser("sub1", add_help=False, help="Perform sub1.")
    sub1.add_argument("-s", "--sub1opt", action="store_true")

    sub2 = subparsers.add_parser("sub2", add_help=False)
    sub2.add_argument("-s", "--sub2opt", action="store_true")

    subparsers2 = sub2.add_subparsers()
    sub21 = subparsers2.add_parser("sub21", help="Perform sub21.")
    sub21.add_argument("--sub21opt", action="store_true")

    self.performCompletion(root, [""], {"--rootopt", "sub1", "sub2"})
    self.performCompletion(sub1, [""], {"-s", "--sub1opt"})
    self.performCompletion(sub1, ["--"], {"--sub1opt"})
    self.performCompletion(sub2, [""], {"sub21", "-s", "--sub2opt"})
    self.performCompletion(sub21, [""], {"-h", "--help", "--sub21opt"})
Exemple #17
0
  def testMultipleArguments(self):
    """Verify that options with arguments can be handled and completed."""
    parser = CompletingArgumentParser()
    parser.add_argument("--test", nargs=1, type=str)
    parser.add_argument("-f", nargs="*", type=int)
    parser.add_argument("-b", "--bar", action="store_true")

    self.performCompletion(parser, ["--test", ""], set(), exit_code=1)
Exemple #18
0
  def testCompleteChoicesForPositional(self):
    """Verify that choice completion works for parser-level positionals."""
    parser = CompletingArgumentParser(prog="choices", add_help=False)
    parser.add_argument("player")
    parser.add_argument("move", choices=("rock", "paper", "scissors"))
    parser.add_argument("--foo", action="store_true")

    self.performCompletion(parser, ["bar", "--foo", ""], {"rock", "paper", "scissors", "--foo"})
    self.performCompletion(parser, ["--foo", ""], {"--foo"})
    self.performCompletion(parser, ["--foo", "bar", "s"], {"scissors"})
Exemple #19
0
  def testCompleteAfterPositionals(self):
    """Verify that keyword arguments following positional arguments can be completed."""
    parser = CompletingArgumentParser(add_help=False)
    parser.add_argument("positional1")
    parser.add_argument("positional2")
    parser.add_argument("--foo", action="store_true")
    parser.add_argument("-b", "--bar", action="store_true")

    self.performCompletion(parser, ["foobar", ""], {"-b", "--bar", "--foo"})
    self.performCompletion(parser, ["-b", "foobar", "--"], {"--bar", "--foo"})
    self.performCompletion(parser, ["foobar", "bazzer", ""], {"-b", "--bar", "--foo"})
    self.performCompletion(parser, ["foobar", "--bar", "bazzer", ""], {"-b", "--bar", "--foo"})
    self.performCompletion(parser, ["foobar", "--bar", "bazzer", "booh"], set(), exit_code=1)
Exemple #20
0
        sess.run(init_op)

        # Restore checkpoint
        tf_helpers.maching_variable_restore(sess, args.checkpoint)
        # trainer.saver.restore(sess, args.checkpoint)

        evaluator.evaluation_step(name="Evaluation")


if __name__ == "__main__":
    parser = ArgumentParser(prog='eknog',
                            description="Learning KG embeddings made easy.")
    subparsers = parser.add_subparsers()

    parser.add_argument('--debug',
                        action='store_true',
                        help="Activate Tensorflow-Debugging.")

    # Training Mode
    train_cmd = subparsers.add_parser(
        name="train",
        help="Train a model (new or continue from saved checkpoint)")
    train_cmd.set_defaults(func=train)

    train_cmd.add_argument('--model',
                           '-m',
                           type=str,
                           default="TransE",
                           help="Name of model.")
    train_cmd.add_argument('--dataset',
                           '-d',
Exemple #21
0
  def testArgumentsWithAllActions(self):
    """Check that we can add arguments for all sorts of actions."""
    parser = CompletingArgumentParser(add_help=False)
    parser.add_argument("store", action="store")
    parser.add_argument("store", action="store", nargs=42)
    parser.add_argument("store_const", action="store_const", const="foo")
    parser.add_argument("store_false", action="store_false")
    parser.add_argument("append", action="append")
    parser.add_argument("append_const", action="append_const", const="foo")
    parser.add_argument("count", action="count")

    parser.add_argument("--store", action="store", dest="store")
    parser.add_argument("--store2", action="store", nargs=3, dest="store2")
    parser.add_argument("--store_const", action="store_const", const="foo", dest="store_const")
    parser.add_argument("--store_false", action="store_false", dest="store_false")
    parser.add_argument("--append", action="append", dest="append")
    parser.add_argument("--append_const", action="append_const", const="foo", dest="append_const")
    parser.add_argument("--count", action="count", dest="count")
    parser.add_argument("--help", action="help")
    parser.add_argument("--version", action="version")