From b4d46ec17255aa186b41953ab36a6f26280a870f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Paulo=20Silva=20de=20Souza?= <77391175+joao-paulo-parity@users.noreply.github.com> Date: Fri, 1 Jul 2022 08:29:57 -0300 Subject: [PATCH 1/3] get and unset paths by multiple arguments --- tests/toml_cli/test_init.py | 11 +++++------ toml_cli/__init__.py | 26 +++++++++++++++----------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/tests/toml_cli/test_init.py b/tests/toml_cli/test_init.py index 73ccc68..2a1cc82 100644 --- a/tests/toml_cli/test_init.py +++ b/tests/toml_cli/test_init.py @@ -26,15 +26,15 @@ def test_get_value(tmp_path: pathlib.Path): assert result.exit_code == 0 assert "{'name': 'MyName', 'age': 12, 'education': {'name': 'University'}}" in result.stdout - result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person.education"]) + result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person", "education"]) assert result.exit_code == 0 assert "{'name': 'University'}" in result.stdout - result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person.education.name"]) + result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person", "education", "name"]) assert result.exit_code == 0 assert "University" in result.stdout - result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person.age"]) + result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person", "age"]) assert result.exit_code == 0 assert "12" in result.stdout @@ -109,12 +109,12 @@ def test_unset(tmp_path: pathlib.Path): """ ) - result = runner.invoke(app, ["unset", "--toml-path", str(test_toml_path), "person.education.name"]) + result = runner.invoke(app, ["unset", "--toml-path", str(test_toml_path), "person", "education", "name"]) assert result.exit_code == 0 assert "[person.education]" in test_toml_path.read_text() assert "University" not in test_toml_path.read_text() - result = runner.invoke(app, ["unset", "--toml-path", str(test_toml_path), "person.education"]) + result = runner.invoke(app, ["unset", "--toml-path", str(test_toml_path), "person", "education"]) assert result.exit_code == 0 assert "[persion.education]" not in test_toml_path.read_text() @@ -122,7 +122,6 @@ def test_unset(tmp_path: pathlib.Path): assert result.exit_code == 0 assert len(test_toml_path.read_text()) == 1 # just a newline - def test_no_command(): result = runner.invoke(app, []) assert result.exit_code == 0 diff --git a/toml_cli/__init__.py b/toml_cli/__init__.py index ee7e5bd..7736c08 100644 --- a/toml_cli/__init__.py +++ b/toml_cli/__init__.py @@ -1,21 +1,23 @@ import pathlib -from typing import Optional +from typing import Optional, List import tomlkit import tomlkit.exceptions import typer -app = typer.Typer() +app = typer.Typer(no_args_is_help=True) @app.command("get") -def get(key: Optional[str] = typer.Argument(None), toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml"))): +def get( + key: Optional[List[str]] = typer.Argument(None), + toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml")) +): """Get a value from a toml file""" toml_part = tomlkit.parse(toml_path.read_text()) - if key is not None: - for key_part in key.split("."): - toml_part = toml_part[key_part] + for key_part in key: + toml_part = toml_part[key_part] typer.echo(toml_part) @@ -66,20 +68,22 @@ def add_section( @app.command("unset") -def unset(key: str, toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml"))): +def unset( + key: Optional[List[str]] = typer.Argument(None), + toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml")) +): """Unset a value from a toml file""" toml_part = toml_file = tomlkit.parse(toml_path.read_text()) - for key_part in key.split(".")[:-1]: + for key_part in key[:-1]: try: toml_part = toml_part[key_part] except tomlkit.exceptions.NonExistentKey: typer.echo(f"Key {key} can not unset", err=True) - del toml_part[key.split(".")[-1]] + del toml_part[key[-1]] toml_path.write_text(tomlkit.dumps(toml_file)) - -def main(): +if __name__ == "__main__": app() From 783cab8432d111677142d058165fb2ddd2ea514a Mon Sep 17 00:00:00 2001 From: joao-paulo-parity Date: Fri, 1 Jul 2022 10:46:42 -0300 Subject: [PATCH 2/3] add back newline --- tests/toml_cli/test_init.py | 1 + toml_cli/__init__.py | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/toml_cli/test_init.py b/tests/toml_cli/test_init.py index 2a1cc82..82ba00b 100644 --- a/tests/toml_cli/test_init.py +++ b/tests/toml_cli/test_init.py @@ -122,6 +122,7 @@ def test_unset(tmp_path: pathlib.Path): assert result.exit_code == 0 assert len(test_toml_path.read_text()) == 1 # just a newline + def test_no_command(): result = runner.invoke(app, []) assert result.exit_code == 0 diff --git a/toml_cli/__init__.py b/toml_cli/__init__.py index 7736c08..4eaab91 100644 --- a/toml_cli/__init__.py +++ b/toml_cli/__init__.py @@ -85,5 +85,6 @@ def unset( toml_path.write_text(tomlkit.dumps(toml_file)) + if __name__ == "__main__": app() From 851ab20a1d5269ba270bb1c2c8fd9228884e7336 Mon Sep 17 00:00:00 2001 From: joao-paulo-parity Date: Fri, 1 Jul 2022 11:01:11 -0300 Subject: [PATCH 3/3] fix entrypoint --- toml_cli/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/toml_cli/__init__.py b/toml_cli/__init__.py index 4eaab91..ec3469b 100644 --- a/toml_cli/__init__.py +++ b/toml_cli/__init__.py @@ -86,5 +86,8 @@ def unset( toml_path.write_text(tomlkit.dumps(toml_file)) +def main(): + app() + if __name__ == "__main__": app()