Skip to content

Commit

Permalink
Update arg parsing to better handle optinal journal name
Browse files Browse the repository at this point in the history
Fixes jrnl-org#520 -and parameter seems to only work for the default journal
  • Loading branch information
wren committed Jul 2, 2020
1 parent 27207b4 commit f111079
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion features/tagging.feature
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions jrnl/cli.py
Expand Up @@ -63,7 +63,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",
Expand Down Expand Up @@ -109,9 +108,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",
)

Expand Down Expand Up @@ -202,14 +202,17 @@ 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 = []

# Handle '-123' as a shortcut for '-n 123'
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):
Expand Down
54 changes: 36 additions & 18 deletions tests/test_parse_args.py
Expand Up @@ -78,21 +78,29 @@ 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(
excluded=["one", "two", "three"]
)


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():
Expand Down Expand Up @@ -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

0 comments on commit f111079

Please sign in to comment.