diff --git a/fconv/__init__.py b/fconv/__init__.py index 4d73488..f8ff974 100644 --- a/fconv/__init__.py +++ b/fconv/__init__.py @@ -29,4 +29,10 @@ "yaml_explicit_start": "add explicit start marker(See also https://yaml.org/spec/1.2.2/#914-explicit-documents)", "yaml_explicit_end": "add explicit end marker(See also https://yaml.org/spec/1.2.2/#914-explicit-documents)", "yaml_indent": "YAML array elements and object members will be pretty-printed with that indent level. ", + # xml input + "xml_process_namespaces": "enable namespace support(See more info at https://github.com/martinblech/xmltodict#namespace-support)", + "xml_process_comments": "treat comments directives as an attribute", + # xml output + "xml_particle_document": "disable to add document root attribute", + "xml_disable_pretty": "disable XML document to be pretty-printed with indent", } diff --git a/fconv/__main__.py b/fconv/__main__.py index cc8028a..5e337f2 100644 --- a/fconv/__main__.py +++ b/fconv/__main__.py @@ -58,6 +58,26 @@ def _set_opts() -> Namespace: ) p.add_argument("--yaml-indent", help=HELP["yaml_indent"], type=int) + # xml intput + p.add_argument( + "--xml-process-namespaces", + help=HELP["xml_process_namespaces"], + action="store_true", + ) + p.add_argument( + "--xml-process-comments", help=HELP["xml_process_comments"], action="store_true" + ) + + # xml output + p.add_argument( + "--xml-particle-document", + help=HELP["xml_particle_document"], + action="store_true", + ) + p.add_argument( + "--xml-disable-pretty", help=HELP["xml_disable_pretty"], action="store_true" + ) + a = p.parse_args() return a @@ -104,6 +124,16 @@ def parse_args(a): out_opt["explicit_end"] = True if a.yaml_indent is not None: out_opt["indent"] = a.yaml_indent + # xml input + if a.xml_process_namespaces: + out_opt["process_namespaces"] = True + if a.xml_process_comments: + out_opt["process_comments"] = True + # xml output + if a.xml_particle_document: + out_opt["full_document"] = False + if a.xml_disable_pretty: + out_opt["pretty"] = False return in_opt, out_opt diff --git a/tests/test_cli.py b/tests/test_cli.py index d18338d..1ce28f8 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -6,7 +6,11 @@ from fconv import __prog__ from fconv.__main__ import main -from .fixtures import JSON_FILE_PATH, TOML_FILE_PATH, YAML_FILE_PATH +# fmt: off +from .fixtures import (JSON_FILE_PATH, TOML_FILE_PATH, XML_FILE_PATH, + YAML_FILE_PATH) + +# fmt: on @pytest.mark.parametrize( @@ -212,6 +216,51 @@ def test_cli_json_opt_callable(self, mocker, argv, expect_opt): [__prog__, "json", "yaml", "-i", JSON_FILE_PATH, "--yaml-indent", "1"], {"indent": 1}, ), + # XML input opt + ( + [ + __prog__, + "xml", + "yaml", + "-i", + XML_FILE_PATH, + "--xml-process-namespaces", + ], + {"process_namespaces": True}, + ), + ( + [ + __prog__, + "xml", + "yaml", + "-i", + XML_FILE_PATH, + "--xml-process-comments", + ], + {"process_comments": True}, + ), + ( + [ + __prog__, + "json", + "xml", + "-i", + JSON_FILE_PATH, + "--xml-particle-document", + ], + {"full_document": False}, + ), + ( + [ + __prog__, + "json", + "xml", + "-i", + JSON_FILE_PATH, + "--xml-disable-pretty", + ], + {"pretty": False}, + ), ], ) @pytest.mark.this