From 655622b96f3bc7ce23f2b73c2b2a6c728630cc79 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Wed, 1 Jul 2020 14:47:05 -0700 Subject: [PATCH] Update arg parsing to better handle optinal journal name Fixes #520 -and parameter seems to only work for the default journal --- features/tagging.feature | 2 +- jrnl/cli.py | 9 ++++--- tests/test_parse_args.py | 54 ++++++++++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/features/tagging.feature b/features/tagging.feature index 22bed636a..c96273c5c 100644 --- a/features/tagging.feature +++ b/features/tagging.feature @@ -77,7 +77,7 @@ Feature: Tagging When we run "jrnl today: I think this will show up @thought" When we run "jrnl today: This should @never show up @thought" When we run "jrnl today: What a nice day for filtering @thought" - When we run "jrnl --tags -not @not @never" + When we run "jrnl --tags -not @not -not @never" Then the output should be """ @thought : 2 diff --git a/jrnl/cli.py b/jrnl/cli.py index 72ba5ea9c..0ca54d659 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -64,7 +64,6 @@ def parse_args(args=None): "Composing", 'To write an entry simply write it on the command line, e.g. "jrnl yesterday at 1pm: Went to the gym."', ) - composing.add_argument("text", metavar="", nargs="*") reading = parser.add_argument_group( "Reading", @@ -110,9 +109,10 @@ def parse_args(args=None): reading.add_argument( "-not", dest="excluded", - nargs="+", + nargs="?", default=[], metavar="E", + action="append", help="Exclude entries with these tags", ) @@ -203,6 +203,9 @@ def parse_args(args=None): help="Opens an interactive interface for deleting entries.", ) + # Everything else + composing.add_argument("text", metavar="", nargs="*") + if not args: args = [] @@ -210,7 +213,7 @@ def parse_args(args=None): num = re.compile(r"^-(\d+)$") args = [num.sub(r"-n \1", arg) for arg in args] - return parser.parse_args(args) + return parser.parse_intermixed_args(args) def guess_mode(args, config): diff --git a/tests/test_parse_args.py b/tests/test_parse_args.py index fcdc611ee..f9af643ef 100644 --- a/tests/test_parse_args.py +++ b/tests/test_parse_args.py @@ -78,7 +78,6 @@ def test_not_alone(): assert cli_as_dict("-not test") == expected_args(excluded=["test"]) -@pytest.mark.xfail def test_not_multiple_alone(): assert cli_as_dict("-not one -not two") == expected_args(excluded=["one", "two"]) assert cli_as_dict("-not one -not two -not three") == expected_args( @@ -86,13 +85,22 @@ def test_not_multiple_alone(): ) -def test_export_alone(): - assert cli_as_dict("--export json") == expected_args(export="json") +@pytest.mark.parametrize( + "cli", + ["two -not one -not three", "-not one two -not three", "-not one -not three two",], +) +def test_not_mixed(cli): + result = expected_args(excluded=["one", "three"], text=["two"]) + assert cli_as_dict(cli) == result + +def test_not_interspersed(): + result = expected_args(excluded=["one", "three"], text=["two", "two", "two"]) + assert cli_as_dict("two -not one two -not three two") == result -@pytest.mark.xfail -def test_export_defaults_to_jrnl(): - assert cli_as_dict("--export") == expected_args(import_="jrnl") + +def test_export_alone(): + assert cli_as_dict("--export json") == expected_args(export="json") def test_import_alone(): @@ -165,20 +173,30 @@ def test_version_alone(): # @see https://github.com/jrnl-org/jrnl/issues/520 -@pytest.mark.xfail -def test_and_ordering(): +@pytest.mark.parametrize( + "cli", + [ + "-and second @oldtag @newtag", + "second @oldtag @newtag -and", + "second -and @oldtag @newtag", + "second @oldtag -and @newtag", + ], +) +def test_and_ordering(cli): result = expected_args(strict=True, text=["second", "@oldtag", "@newtag"]) - assert result == cli_as_dict("-and second @oldtag @newtag") - assert result == cli_as_dict("second @oldtag @newtag -and") - assert result == cli_as_dict("second -and @oldtag @newtag") - assert result == cli_as_dict("second @oldtag -and @newtag") + assert cli_as_dict(cli) == result # @see https://github.com/jrnl-org/jrnl/issues/520 -@pytest.mark.xfail -def test_edit_ordering(): +@pytest.mark.parametrize( + "cli", + [ + "--edit second @oldtag @newtag", + "second @oldtag @newtag --edit", + "second --edit @oldtag @newtag", + "second @oldtag --edit @newtag", + ], +) +def test_edit_ordering(cli): result = expected_args(edit=True, text=["second", "@oldtag", "@newtag"]) - assert result == cli_as_dict("--edit second @oldtag @newtag") - assert result == cli_as_dict("second @oldtag @newtag --edit") - assert result == cli_as_dict("second --edit @oldtag @newtag") - assert result == cli_as_dict("second @oldtag --edit @newtag") + assert cli_as_dict(cli) == result