Skip to content

Commit

Permalink
Add -r flag to tag subcommand to enforce relative dates
Browse files Browse the repository at this point in the history
Relative dates were already interpreted for due and start tags, but you
can also enforce it for other tags by passing the -r flag to the tag
command.
  • Loading branch information
bram85 committed Jan 12, 2017
1 parent 5e626f6 commit 7ba9912
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
29 changes: 29 additions & 0 deletions test/test_tag_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ def test_set_tag12(self):
self.assertEqual(self.output, "| 3| Baz due:2014-10-20 foo:today\n")
self.assertEqual(self.errors, "")

@freeze_time('2017, 1, 12')
def test_set_tag13(self):
"""
Convert relative dates when forced to.
"""

command = TagCommand(["-r", "3", "foo", "today"], self.todolist,
self.out, self.error)

command.execute()

self.assertTrue(self.todolist.dirty)
self.assertEqual(self.output, "| 3| Baz due:2014-10-20 foo:2017-01-12\n")
self.assertEqual(self.errors, "")

def test_set_tag14(self):
"""
Leave the original value when an invalid relative date was given.
"""

command = TagCommand(["-r", "3", "foo", "bar"], self.todolist,
self.out, self.error)

command.execute()

self.assertTrue(self.todolist.dirty)
self.assertEqual(self.output, "| 3| Baz due:2014-10-20 foo:bar\n")
self.assertEqual(self.errors, "")

def test_rm_tag01(self):
command = TagCommand(["1", "due"], self.todolist, self.out, self.error)
command.execute()
Expand Down
14 changes: 11 additions & 3 deletions topydo/commands/TagCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ def __init__(self, p_args, p_todolist, #pragma: no branch

self.force = False
self.force_add = False
self.relative_date = False
self.todo = None
self.tag = None
self.value = None
self.values = []
self.current_values = []

def _process_flags(self):
flags, args = self.getopt("af")
flags, args = self.getopt("afr")
for flag, _ in flags:
if flag == "-a":
self.force_add = True
elif flag == "-f":
self.force = True
elif flag == "-r":
self.relative_date = True

self.args = args

Expand Down Expand Up @@ -93,7 +96,10 @@ def _choose(self):
return answer

def _convert_relative_dates(self):
if self.tag == config().tag_start() or self.tag == config().tag_due():
is_start_tag = self.tag == config().tag_start()
is_due_tag = self.tag == config().tag_due()

if self.relative_date or is_start_tag or is_due_tag:
real_date = relative_date_to_date(self.value)

if real_date:
Expand Down Expand Up @@ -143,5 +149,7 @@ def help(self):
-a : Do not change the current value of the TAG if it exists, but add a new
VALUE for the given TAG.
-f : Force setting/removing all values of the TAG. Prevents interaction with
the user.\
the user.
-r : Interpret the given value as a relative date and convert it to an absolute
date.\
"""

0 comments on commit 7ba9912

Please sign in to comment.