def assert_parse_args_fails(self, args, message, exit_code=None, stdout=None):
        if exit_code is None:
            exit_code = 2
        if stdout is None:
            stdout = io.StringIO()
        x = ArgumentParser(stdout=stdout)
        with self.assertRaises(x.Error) as cm:
            x.parse_args(args)

        self.assertEqual("{}".format(cm.exception), "{}".format(message))
        self.assertEqual(cm.exception.exit_code, exit_code)
Exemple #2
0
def run():
    arg_parser = ArgumentParser()
    try:
        app = arg_parser.parse_args()
    except arg_parser.Error as e:
        if e.exit_code == 2:
            print("ERROR: invalid command-line arguments: {}".format(e), file=sys.stderr)
            print("Run with --help for help", file=sys.stderr)
        elif e.exit_code != 0:
            print("ERROR: {}".format(e), file=sys.stderr)
        return e.exit_code

    try:
        app.run()
    except app.Error as e:
        print("ERROR: {}".format(e), file=sys.stderr)
        return 1

    return 0
    def assert_parse_args_succeeds(
            self, args,
            target_language=DEFAULT_VALUE,
            source_file_path=DEFAULT_VALUE,
            output_file_paths=DEFAULT_VALUE,
            inline=DEFAULT_VALUE,
            encoding=DEFAULT_VALUE,
            newline=DEFAULT_VALUE,
    ):
        stdout = io.StringIO()
        x = ArgumentParser(stdout=stdout)

        app = x.parse_args(args)

        if target_language is self.DEFAULT_VALUE:
            target_language = x.targets["c"]
        else:
            target_language = x.targets[target_language]

        if source_file_path is self.DEFAULT_VALUE:
            source_file_path = "cligen.xml"

        if output_file_paths is self.DEFAULT_VALUE:
            output_file_paths = None
        else:
            output_file_paths = tuple(output_file_paths)

        if inline is self.DEFAULT_VALUE:
            inline = False

        if encoding is self.DEFAULT_VALUE:
            encoding = None

        if newline is self.DEFAULT_VALUE:
            newline = None

        self.assertIs(app.target_language, target_language)
        self.assertEqual(app.source_file_path, source_file_path)
        self.assertEqual(app.output_file_paths, output_file_paths)
        self.assertIs(app.inline, inline)
        self.assertIs(app.encoding, encoding)
        self.assertEqual(app.newline, newline)
Exemple #4
0
def run():
    arg_parser = ArgumentParser()
    try:
        app = arg_parser.parse_args()
    except arg_parser.Error as e:
        if e.exit_code == 2:
            print("ERROR: invalid command-line arguments: {}".format(e),
                  file=sys.stderr)
            print("Run with --help for help", file=sys.stderr)
        elif e.exit_code != 0:
            print("ERROR: {}".format(e), file=sys.stderr)
        return e.exit_code

    try:
        app.run()
    except app.Error as e:
        print("ERROR: {}".format(e), file=sys.stderr)
        return 1

    return 0
 def test_error_KeywordArgs(self):
     x = ArgumentParser()
     with self.assertRaises(x.Error) as cm:
         x.error(message="blah blah")
     self.assertEqual("{}".format(cm.exception), "blah blah")
     self.assertEqual(cm.exception.exit_code, 2)
 def test_exit_DefaultArgs(self):
     x = ArgumentParser()
     with self.assertRaises(x.Error) as cm:
         x.exit()
     self.assertEqual("{}".format(cm.exception), "None")
     self.assertEqual(cm.exception.exit_code, 0)
 def test_exit_KeywordArgs(self):
     x = ArgumentParser()
     with self.assertRaises(x.Error) as cm:
         x.exit(status=6, message="whatever")
     self.assertEqual("{}".format(cm.exception), "whatever")
     self.assertEqual(cm.exception.exit_code, 6)
 def test_exit_PositionalArgs(self):
     x = ArgumentParser()
     with self.assertRaises(x.Error) as cm:
         x.exit(5, "whatever")
     self.assertEqual("{}".format(cm.exception), "whatever")
     self.assertEqual(cm.exception.exit_code, 5)
 def test___init___DefaultArgs(self):
     x = ArgumentParser()
     self.assertIs(x.stdout, sys.stdout)
 def test___init___KeywordArgs(self):
     stdout = object()
     x = ArgumentParser(stdout=stdout)
     self.assertIs(x.stdout, stdout)
 def test_exit_code_2(self):
     with self.assertRaises(ArgumentParser.Error) as cm:
         raise ArgumentParser.Error(message="message", exit_code=2)
     self.assertEqual(cm.exception.exit_code, 2)
 def test___init___KeywordArgs(self):
     with self.assertRaises(ArgumentParser.Error) as cm:
         raise ArgumentParser.Error(message="message", exit_code=0)
     self.assertEqual("{}".format(cm.exception), "message")
     self.assertEqual(cm.exception.exit_code, 0)
 def test___init___PositionalArgs(self):
     stdout = object()
     x = ArgumentParser(stdout)
     self.assertIs(x.stdout, stdout)