def test_msvc_flag_with_value(self): parser = ArgumentParserEx(prefix_chars="/-") parser.set(ignore_case=True) parser.add_argument("/flag", action="msvc_flag_with_value") parser.add_argument("/ENABLE", action="msvc_flag_with_value", append=True, dest="list") ns, remaining = parser.parse_known_args(["/FLAG"]) self.assertListEqual(["/FLAG"], remaining) self.assertIsNone(vars(ns).get("flag")) ns, remaining = parser.parse_known_args(["/flag"]) self.assertListEqual(["/flag"], remaining) self.assertIsNone(vars(ns).get("flag")) ns, remaining = parser.parse_known_args(["/flag:"]) self.assertListEqual(["/flag:"], remaining) self.assertIsNone(vars(ns).get("flag")) ns = parser.parse_args(["/flag:value"]) self.assertEqual("value", ns.flag) ns = parser.parse_args(["-FLAG:1,2,3"]) self.assertEqual("1,2,3", ns.flag) ns = parser.parse_args(["/ENABLE:1", "-FlAg:2", "-enable:3"]) self.assertEqual("2", ns.flag) self.assertEqual(["1", "3"], ns.list)
def test_raw_dest(self): parser = ArgumentParserEx() parser.add_argument("-arg1", action="store_true", raw_dest="args") parser.add_argument("--arg2", "-f", dest=None, raw_dest="args") parser.add_argument("file", raw_dest="args") parser.add_argument("files", nargs="*", dest=None, raw_dest="args") parser.add_argument("-D", dest=None, raw_dest="args", raw_format="".join) ns = parser.parse_args(["--arg2", "1", "2"]) self.assertListEqual([["--arg2", "1"], "2"], ns.args) ns = parser.parse_args(["--arg2=1", "1", "-arg1", "2", "3"]) self.assertListEqual(["--arg2=1", "1", "-arg1", "2", "3"], ns.args) ns, remaining = parser.parse_known_args( ["1", "-fvalue", "2", "-unknown"]) self.assertListEqual(["-unknown"], remaining) self.assertListEqual(["1", "-fvalue", "2"], ns.args) ns, remaining = parser.parse_known_args( ["1", "-unknown", "-fvalue", "2"]) self.assertListEqual(["-unknown"], remaining) self.assertListEqual(["1", "-fvalue", "2"], ns.args) ns = parser.parse_args(["1", "-D", "2", "3", "-D4"]) self.assertListEqual(["1", "-D2", "3", "-D4"], ns.args)
def test_action_append_const(self): parser = ArgumentParserEx() parser.add_argument("-a", action="append_const", const="v") ns, remaining = parser.parse_known_args(["-a", "1", "-a2", "-a"]) self.assertListEqual(["1", "-a2"], remaining) self.assertEqual(["v", "v"], ns.a)
def test_prefix_chars(self): parser = ArgumentParserEx(prefix_chars="/-") parser.add_argument("-attr1", action="store") ns, remaining = parser.parse_known_args(["/attr1", "val"]) self.assertListEqual([], remaining) self.assertEqual("val", ns.attr1) ns, remaining = parser.parse_known_args(["-attr1", "val"]) self.assertListEqual([], remaining) self.assertEqual("val", ns.attr1) parser.set(prefix_chars="+") parser.add_argument("+attr2", action="store") ns, remaining = parser.parse_known_args( ["-attr1", "val1", "+attr2", "val2"]) self.assertListEqual([], remaining) self.assertEqual("val1", ns.attr1) self.assertEqual("val2", ns.attr2) ns, remaining = parser.parse_known_args( ["/attr1", "val1", "+attr2", "val2"]) self.assertListEqual([], remaining) self.assertEqual("val1", ns.attr1) self.assertEqual("val2", ns.attr2) ns, remaining = parser.parse_known_args( ["+attr1", "val1", "-attr2", "val2"]) self.assertListEqual(["+attr1", "val1", "-attr2", "val2"], remaining) self.assertIsNone(vars(ns).get("attr1")) self.assertIsNone(vars(ns).get("attr2"))
def test_case_insensitive_windows_style(self): parser = ArgumentParserEx(prefix_chars="/") parser.add_argument("/nologo", action="store_true", ignore_case=True) ns = parser.parse_args(["/NOLOGO"]) self.assertTrue(ns.nologo) ns = parser.parse_args(["/nologo"]) self.assertTrue(ns.nologo)
def test_intermixed_args(self): parser = ArgumentParserEx() parser.add_argument("files", action="append") parser.add_argument("-D", action="append", dest="definitions") ns = parser.parse_args(["-DA=1", "a.cpp", "-DB=2", "b.cpp"]) # standard ArgumentParser won't append 'b.cpp' here self.assertListEqual(["a.cpp", "b.cpp"], ns.files) self.assertListEqual(["A=1", "B=2"], ns.definitions)
def test_concatenate_flags(self): self.skipTest("not implemented yet") parser = ArgumentParserEx() parser.add_argument("-a", action="store_true") parser.add_argument("-b", action="store_true") ns = parser.parse_args(["-ab"]) self.assertTrue(ns.a) self.assertTrue(ns.b)
def test_disable_concatenation(self): self.skipTest("not implemented yet") parser = ArgumentParserEx() parser.enable_flag_concatenation(False) parser.add_argument("-a", action="store_true") parser.add_argument("-b", action="store_true") ns, remaining = parser.parse_args(["-ab"]) self.assertListEqual(["-ab"], remaining)
def test_deepcopy_1(self): parser = ArgumentParserEx() parser.add_argument("-a") parser = deepcopy(parser) ns = parser.parse_args(["-a", "1"]) self.assertEqual("1", ns.a)
def test_single_hyphen_multichar_flag(self): parser = ArgumentParserEx() parser.add_argument("-flag") ns = parser.parse_args(["-flag", "value"]) self.assertEqual("value", ns.flag) ns = parser.parse_args(["-flag=value"]) self.assertEqual("value", ns.flag)
def test_action_default(self): parser = ArgumentParserEx() parser.add_argument("attr1") parser.add_argument("--attr2") ns, remaining = parser.parse_known_args(["--attr2", "val1", "val2"]) self.assertListEqual([], remaining) self.assertEqual("val2", ns.attr1) self.assertEqual("val1", ns.attr2)
def test_force_suffix_value(self): parser = ArgumentParserEx() parser.add_argument(prefixes=["-Xclang"]) ns = parser.parse_args(["-Xclangvalue"]) self.assertEqual("value", ns.Xclang) parser = ArgumentParserEx() parser.add_argument("-Xclang", prefix=True) ns = parser.parse_args(["-Xclangvalue"]) self.assertEqual("value", ns.Xclang)
def test_shortening(self): parser = ArgumentParserEx() parser.add_argument("--abc") parser.add_argument("--a") # standard ArgumentParser will parse --ab as --abc ns, remaining = parser.parse_known_args( ["--abc", "1", "--ab", "2", "--a", "3"]) self.assertListEqual(["--ab", "2"], remaining) self.assertEqual("1", ns.abc) self.assertEqual("3", ns.a)
def test_msvc_flag_with_value_with_append(self): parser = ArgumentParserEx(prefix_chars="/-") parser.set(ignore_case=True) parser.add_argument("/flag", action="msvc_flag_with_value", dest="flags", append=True) ns = parser.parse_args(["/flag:a", "/flag:b"]) self.assertListEqual(["a", "b"], ns.flags)
def test_optional_positional_argument(self): parser = ArgumentParserEx() parser.add_argument("infile") parser.add_argument("outfile", nargs="?") ns = parser.parse_args(["inout.txt"]) self.assertEqual("inout.txt", ns.infile) ns = parser.parse_args(["in.txt", "out.txt"]) self.assertEqual("in.txt", ns.infile) self.assertEqual(["out.txt"], ns.outfile)
def test_action_store_false(self): parser = ArgumentParserEx() parser.add_argument("-a", action="store_false") ns, remaining = parser.parse_known_args([]) self.assertListEqual([], remaining) self.assertTrue(ns.a) ns, remaining = parser.parse_known_args(["-a"]) self.assertListEqual([], remaining) self.assertFalse(ns.a)
def test_correct_list_order(self): parser = ArgumentParserEx(prefix_chars="-/") parser.set_defaults(definitions=[]) parser.add_argument("/c", action="store_true", dest="compile_only") parser.add_argument("-D", action="append", dest="definitions") parser.add_argument("-I", action="append", dest="include_dirs") parser.add_argument("-U", action="append", dest="definitions") parser.add_argument("files", nargs="*") ns = parser.parse_args( ["/c", "-D1", "/U", "2", "/D3", "-I.", "-U4", "a.cpp"]) self.assertListEqual(["1", "2", "3", "4"], ns.definitions)
def test_action_store_positional(self): parser = ArgumentParserEx() parser.add_argument("attr", action="store") ns, remaining = parser.parse_known_args(["-val"]) self.assertListEqual(["-val"], remaining) self.assertIsNone(vars(ns).get("attr")) ns, remaining = parser.parse_known_args(["val"]) self.assertListEqual([], remaining) self.assertEqual("val", ns.attr)
class CMake(Parser): priority = 7 @staticmethod def add_arguments(arg_parser): pass @staticmethod def is_applicable(project=None, log_type=None): return True def __init__(self, context, platform=None): self.context = context self.platform = get_platform(platform) self.program_re = self.platform.get_program_path_re("cmake") self.parser = ArgumentParserEx(prog="cmake") self.parser.add_argument("-E", dest="command", nargs="+") def parse(self, target): tokens = target.get("tokens") if not tokens: return target if not self.program_re.match(tokens[0]): return target namespace = self.parser.parse_args(tokens[1:]) if not namespace.command: return target command = namespace.command[0] args = namespace.command[1:] dependencies = [] targets = [] if command == "cmake_symlink_library": # on Mac source = self.context.get_file_arg( self.context.platform.normalize_path(args[0]), dependencies) destinations = args[1:] for dest in destinations: target_dependencies = copy(dependencies) output = self.context.get_output(dest, target_dependencies) targets.append( get_copy_target(None, source, output, target_dependencies)) else: logger.warning("Unsupported CMake command: {}".format(command)) return targets
def test_no_prefix_chars(self): parser = ArgumentParserEx() parser.add_argument("-D", action="append", dest="definitions") parser.add_argument( flags=["cmd"], action="append", nargs="+", dest="commands", args_regexp=re.compile(r"^(?!cmd)"), ) ns = parser.parse_args( ["-DA=1", "cmd", "cl.exe", "cmd", "gcc", "/c", "a.cpp"]) self.assertListEqual(["A=1"], ns.definitions) self.assertListEqual([["cl.exe"], ["gcc", "/c", "a.cpp"]], ns.commands)
def test_conditional_arguments(self): self.skipTest("not implemented yet") parser = ArgumentParserEx() arg = parser.add_argument("-a") parser.add_argument("-b", after=[arg]) ns = parser.parse_args(["-a", "1", "-b", "2"]) self.assertEqual("1", ns.a) self.assertEqual("2", ns.b) ns = parser.parse_args(["-a", "1"]) self.assertEqual("1", ns.a) ns, remaining = parser.parse_known_args(["-b", "1"]) self.assertListEqual(["-b", "1"], remaining) self.assertIsNone(vars(ns).get("b"))
def test_compatiblity_arguments(self): # test ArgumentParser interface compatibility # these arguments are simply ignored parser = ArgumentParserEx( prog="test", usage="usage", description="description", epilog="epilog", parents=[], formatter_class=object, fromfile_prefix_chars="fromfile_prefix_chars", argument_default=None, conflict_handler="error", add_help=True, ) parser.add_argument("-a", help="help", metavar="metavar") ns = parser.parse_args(["-a1"]) self.assertEqual("1", ns.a)
def test_action_store_const(self): parser = ArgumentParserEx() parser.add_argument("-a", action="store_const", const="a") ns, remaining = parser.parse_known_args([]) self.assertListEqual([], remaining) self.assertIsNone(vars(ns).get("a")) ns, remaining = parser.parse_known_args(["-a"]) self.assertListEqual([], remaining) self.assertEqual("a", ns.a) parser = ArgumentParserEx() parser.add_argument("-a", action="store_const", const="a", default="b") ns, remaining = parser.parse_known_args([]) self.assertListEqual([], remaining) self.assertEqual("b", ns.a)
def test_action_append(self): parser = ArgumentParserEx() parser.add_argument("-a", action="append") ns, remaining = parser.parse_known_args(["-a1", "-a2"]) self.assertListEqual([], remaining) self.assertEqual(["1", "2"], ns.a) ns, remaining = parser.parse_known_args(["-a", "2", "-a", "1"]) self.assertListEqual([], remaining) self.assertEqual(["2", "1"], ns.a) parser.add_argument("b", action="append") ns, remaining = parser.parse_known_args(["-a", "1", "2", "-a", "3"]) self.assertListEqual([], remaining) self.assertEqual(["1", "3"], ns.a) self.assertEqual(["2"], ns.b)
def test_choices(self): parser = ArgumentParserEx() parser.add_argument("--a1", choices=["abc", "ab", "a"]) parser.add_argument("a2", choices=["def", "ef"]) ns, remaining = parser.parse_known_args(["--a1=abc", "de"]) self.assertListEqual(["de"], remaining) self.assertEqual("abc", ns.a1) self.assertIsNone(vars(ns).get("a2")) ns, remaining = parser.parse_known_args(["--a1", "abc", "de"]) self.assertListEqual(["de"], remaining) self.assertEqual("abc", ns.a1) self.assertIsNone(vars(ns).get("a2")) ns, remaining = parser.parse_known_args(["--a1", "b", "def"]) self.assertListEqual(["--a1", "b"], remaining) self.assertIsNone(vars(ns).get("a1")) self.assertEqual("def", ns.a2)
def test_dest_is_none(self): parser = ArgumentParserEx() parser.add_argument("-opt", dest=None, required=True) parser.add_argument("pos", dest=None) ns = parser.parse_args(["-opt", "1", "2"]) self.assertIsNone(vars(ns).get("opt")) self.assertIsNone(vars(ns).get("pos")) ns, remaining = parser.parse_known_args(["-opt"]) self.assertListEqual(["-opt"], remaining) ns, remaining = parser.parse_known_args(["1"]) self.assertListEqual([], remaining) self.assertIsNone(vars(ns).get("opt")) self.assertIsNone(vars(ns).get("pos")) self.assertRaisesRegexp(ValueError, "Required attribute not found", parser.parse_args, ["1"])
def test_action_store_override(self): parser = ArgumentParserEx() parser.add_argument("attr1", action="store") parser.add_argument("--attr2", action="store") ns, remaining = parser.parse_known_args(["val1", "--attr2", "val2"]) self.assertListEqual([], remaining) self.assertEqual("val1", ns.attr1) self.assertEqual("val2", ns.attr2) ns, remaining = parser.parse_known_args(["--attr2", "val2", "val1"]) self.assertListEqual([], remaining) self.assertEqual("val1", ns.attr1) self.assertEqual("val2", ns.attr2) ns, remaining = parser.parse_known_args( ["--attr2", "val1", "val2", "--attr2", "val3"]) self.assertListEqual([], remaining) self.assertEqual("val2", ns.attr1) self.assertEqual("val3", ns.attr2)
def test_dest_naming(self): parser = ArgumentParserEx(prefix_chars="/-") parser.add_argument("-a-b", action="store_true") parser.add_argument("-++", action="store_true") parser.add_argument("/b:c", dest="_f", action="store_true") parser.add_argument("--c~d", action="store_true") parser.add_argument("-UPPER", action="store_true") self.assertRaisesRegexp( ValueError, "Invalid dest: '@flag'", parser.add_argument, "-flag", dest="@flag", action="store_true", ) ns = parser.parse_args(["-a-b", "-++", "/b:c", "--c~d", "-UPPER"]) self.assertTrue(ns.a_b) self.assertTrue(ns.__) self.assertTrue(ns._f) self.assertTrue(ns.c_d) self.assertTrue(ns.UPPER)
def test_type_conversion(self): parser = ArgumentParserEx() parser.add_argument("--a1", type=int) # disable prefix chars to allow negative numbers as positional arguments parser.set(prefix_chars="") parser.add_argument("a2", type=float) ns, remaining = parser.parse_known_args(["--a1", "h", "0.1"]) self.assertListEqual(["--a1", "h"], remaining) self.assertIsNone(vars(ns).get("a1")) self.assertEqual(0.1, ns.a2) ns, remaining = parser.parse_known_args(["-1", "--a1=123"]) self.assertListEqual([], remaining) self.assertEqual(123, ns.a1) self.assertEqual(-1, ns.a2) ns, remaining = parser.parse_known_args(["--a1", "-5", "-1"]) self.assertListEqual([], remaining) self.assertEqual(-5, ns.a1) self.assertEqual(-1, ns.a2)
def test_unknown_args_dest(self): parser = ArgumentParserEx() parser.add_argument("--flag", action="store_true", raw_dest="args") parser.add_argument("--key", raw_dest="args") parser.add_argument("infile", raw_dest="args") ns, unknown = parser.parse_known_args(["--flag"], unknown_dest="args") self.assertListEqual(["--flag"], ns.args) self.assertListEqual([], unknown) ns, unknown = parser.parse_known_args( ["--flag", "--unknownflag", "--key=value"], unknown_dest="args") self.assertListEqual(["--flag", "--unknownflag", "--key=value"], ns.args) self.assertListEqual(["--unknownflag"], unknown) ns, unknown = parser.parse_known_args( ["--flag", "--unknownflag", "a.txt"], unknown_dest="args") self.assertListEqual(["--flag", "--unknownflag", "a.txt"], ns.args) self.assertListEqual(["--unknownflag"], unknown) ns, unknown = parser.parse_known_args( ["--flag", "a.txt", "--unknownflag"], unknown_dest="args") self.assertListEqual(["--flag", "a.txt", "--unknownflag"], ns.args) self.assertListEqual(["--unknownflag"], unknown) ns, unknown = parser.parse_known_args(["--unknown"], unknown_dest=["args", "extra"]) self.assertListEqual(["--unknown"], ns.args) self.assertListEqual(["--unknown"], ns.extra) self.assertListEqual(["--unknown"], unknown)