Update arg parsing to better handle optinal journal name

Fixes #520 -and parameter seems to only work for the default journal
This commit is contained in:
Jonathan Wren 2020-07-01 14:47:05 -07:00
parent bf6a66b245
commit 0bc5f9d453
3 changed files with 43 additions and 22 deletions

View file

@ -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

View file

@ -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):

View file

@ -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,15 +85,24 @@ def test_not_multiple_alone():
)
@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
def test_export_alone():
assert cli_as_dict("--export json") == expected_args(export="json")
@pytest.mark.xfail
def test_export_defaults_to_jrnl():
assert cli_as_dict("--export") == expected_args(import_="jrnl")
def test_import_alone():
assert cli_as_dict("--import jrnl") == expected_args(import_="jrnl")
@ -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