def new_ArgumentParserSpec(self, arguments=DEFAULT_VALUE, help_argument=DEFAULT_VALUE): if arguments is self.DEFAULT_VALUE: input_file_argument = ArgumentParserSpec.Argument( keys=["-i", "--input-file"], type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text="The input file", ) output_file_argument = ArgumentParserSpec.Argument( keys=["-o", "--output-file"], type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text="The output file", ) arguments = [input_file_argument, output_file_argument] if help_argument is self.DEFAULT_VALUE: help_argument = ArgumentParserSpec.Argument( keys=["-h", "--help"], type=ArgumentParserSpec.Argument.TYPE_BUILTIN_HELP, help_text="Show the help screen then exit", ) if help_argument is not None: arguments.append(help_argument) return ArgumentParserSpec( arguments=arguments, help_argument=help_argument, )
def test_2Arguments(self): self.assert_xml_parse_success( """<?xml version="1.0" ?> <cligen xmlns="http://schemas.cligen.io/arguments"> <argument> <key>-n</key> <key>--name</key> </argument> <argument> <key>-t</key> <key>--title</key> </argument> </cligen> """, arguments=[ ArgumentParserSpec.Argument( keys=("-n", "--name"), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text=None, ), ArgumentParserSpec.Argument( keys=("-t", "--title"), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text=None, ), ], )
def assert_xml_parse_success(self, xml_string, arguments=None, help_argument=None, add_builtin_help_argument=None): x = ArgumentSpecParser() actual = x.parse_string(xml_string) if arguments is None: arguments = [] arguments = tuple(arguments) if add_builtin_help_argument is None or add_builtin_help_argument: help_argument = ArgumentParserSpec.Argument( keys=("-h", "--help"), type=ArgumentParserSpec.Argument.TYPE_BUILTIN_HELP, help_text="Print the help information then exit", ) arguments += (help_argument, ) expected = ArgumentParserSpec( arguments=arguments, help_argument=help_argument, ) self.assertEqual(actual, expected)
def _parse_document(self, root): ns = {"cligen", self.XML_NAMESPACE} data = self.ParsedData() expected_root_tag = self._qualified_tag("cligen") if root.tag != expected_root_tag: raise self.CligenXmlError( "incorrect tag name of XML root element: {} (expected {})".format( root.tag, expected_root_tag)) for element in root: if self._is_qualified_tag(element, "argument"): argument = self._parse_argument(element) data.arguments.append(argument) elif self._is_qualified_tag(element, "options"): self._parse_options(element, data.options) if data.options.default_help_argument: help_argument = ArgumentParserSpec.Argument( keys=("-h", "--help"), type=ArgumentParserSpec.Argument.TYPE_BUILTIN_HELP, help_text="Print the help information then exit", ) data.arguments.append(help_argument) data.help_argument = help_argument return ArgumentParserSpec( arguments=tuple(data.arguments), help_argument=data.help_argument, )
def test_ParsedXML(self): path = self.sample_cligen_xml_path() parser = ArgumentSpecParser() actual = parser.parse_file(path) help_argument = ArgumentParserSpec.Argument( keys=("-h", "--help"), type=ArgumentParserSpec.Argument.TYPE_BUILTIN_HELP, help_text="Print the help information then exit", ) expected_arguments = ( ArgumentParserSpec.Argument( keys=("-i", "--input-file"), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text="The file from which to read", ), ArgumentParserSpec.Argument( keys=("-o", "--output-file"), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text="The file to which to write", ), help_argument, ) expected = ArgumentParserSpec( arguments=expected_arguments, help_argument=help_argument, ) self.assertEqual(actual, expected)
def sample_argspec(): arg1 = ArgumentParserSpec.Argument( keys=("-i", "--input-file"), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text="the file to read", ) arg2 = ArgumentParserSpec.Argument( keys=("-o", "--output-file"), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text="the file to write", ) return ArgumentParserSpec(arguments=(arg1, arg2), help_argument=None)
def test___init___KeywordArgs(self): arguments = object() help_argument = object() x = ArgumentParserSpec(arguments=arguments, help_argument=help_argument) self.assertIs(arguments, x.arguments) self.assertIs(help_argument, x.help_argument)
def test___init___PositionalArgs(self): keys = object() type = object() help_text = object() x = ArgumentParserSpec.Argument(keys, type, help_text) self.assertIs(keys, x.keys) self.assertIs(type, x.type) self.assertIs(help_text, x.help_text)
def new_Argument(self, keys=None, type=None, help_text=None): if keys is None: keys = ["-o", "--output-file"] if type is None: type = ArgumentParserSpec.Argument.TYPE_STRING_VALUE return ArgumentParserSpec.Argument( keys=keys, type=type, help_text=help_text, )
def test_argument_key_NotSpecified(self): self.assert_xml_parse_success( """<?xml version="1.0" ?> <cligen xmlns="http://schemas.cligen.io/arguments"> <argument> </argument> </cligen> """, arguments=[ ArgumentParserSpec.Argument( keys=(), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text=None, ) ], )
def _parse_argument(self, root): keys = [] help_text = None for element in root: if self._is_qualified_tag(element, "key"): key = self._element_text(element) keys.append(key) elif self._is_qualified_tag(element, "help"): help_text = self._element_text(element) return ArgumentParserSpec.Argument( keys=tuple(keys), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text=help_text, )
def test_argument_help(self): self.assert_xml_parse_success( """<?xml version="1.0" ?> <cligen xmlns="http://schemas.cligen.io/arguments"> <argument> <key>-q</key> <help>Keep quiet</help> </argument> </cligen> """, arguments=[ ArgumentParserSpec.Argument( keys=("-q", ), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text="Keep quiet", ) ], )
def test_argument_key_3Keys(self): self.assert_xml_parse_success( """<?xml version="1.0" ?> <cligen xmlns="http://schemas.cligen.io/arguments"> <argument> <key>-w</key> <key>--write</key> <key>--write-file</key> </argument> </cligen> """, arguments=[ ArgumentParserSpec.Argument( keys=("-w", "--write", "--write-file"), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text=None, ) ], )
def test_argument_key_trimming(self): self.assert_xml_parse_success( """<?xml version="1.0" ?> <cligen xmlns="http://schemas.cligen.io/arguments"> <argument> <key> -h</key> <key>--help </key> <key> --help-me </key> </argument> </cligen> """, arguments=[ ArgumentParserSpec.Argument( keys=("-h", "--help", "--help-me"), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text=None, ) ], )
def test_options_add_builtin_help_argument_True(self): self.assert_xml_parse_success( """<?xml version="1.0" ?> <cligen xmlns="http://schemas.cligen.io/arguments"> <options> <add-builtin-help-argument>true</add-builtin-help-argument> </options> <argument> <key>-n</key> <key>--name</key> </argument> </cligen> """, arguments=[ ArgumentParserSpec.Argument( keys=("-n", "--name"), type=ArgumentParserSpec.Argument.TYPE_STRING_VALUE, help_text=None, ) ], )
def test___init___PositionalArgs(self): arguments = object() help_argument = object() x = ArgumentParserSpec(arguments, help_argument) self.assertIs(arguments, x.arguments) self.assertIs(help_argument, x.help_argument)