Commit 745685b2 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add force mode to TagCommand.

There are actually two force modes, one to force adding a tag and
one force mode that omits user interaction. The first kind is done
with -a, the second one with -f.

The meaning of -f changes in this commit, but now it's consistent
with the other subcommands.
parent 8f05447c
......@@ -48,7 +48,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertTrue(self.todolist.is_dirty())
def test_add_tag3(self):
command = TagCommand.TagCommand(["-f", "2", "due", "2014-10-19"], self.todolist, self.out, self.error)
command = TagCommand.TagCommand(["-a", "2", "due", "2014-10-19"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(self.todolist.todo(2).source(), "Bar due:2014-10-22 due:2014-10-19")
......@@ -112,6 +112,14 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, " 1. 2014-10-20\n 2. 2014-10-22\n 4 Fnord due:2014-10-20 due:2014-10-22\n")
self.assertEquals(self.errors, "")
def test_set_tag10(self):
command = TagCommand.TagCommand(["-f", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "99")
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, " 4 Fnord due:2014-10-20 due:2014-10-20\n")
self.assertEquals(self.errors, "")
def test_rm_tag1(self):
command = TagCommand.TagCommand(["1", "due"], self.todolist, self.out, self.error)
command.execute()
......@@ -176,6 +184,14 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Invalid todo number.\n")
def test_rm_tag10(self):
command = TagCommand.TagCommand(["-f", "4", "due"], self.todolist, self.out, self.error, lambda t: "A")
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, " 4 Fnord\n")
self.assertEquals(self.errors, "")
def test_help(self):
command = TagCommand.TagCommand(["help"], self.todolist, self.out, self.error)
command.execute()
......
......@@ -26,15 +26,18 @@ class TagCommand(Command):
super(TagCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.force = False
self.force_add = False
self.todo = None
self.tag = None
self.value = None
self.values = []
def _process_flags(self):
flags, args = self.getopt("f")
flags, args = self.getopt("af")
for flag, value in flags:
if flag == "-f":
if flag == "-a":
self.force_add = True
elif flag == "-f":
self.force = True
self.args = args
......@@ -61,15 +64,18 @@ class TagCommand(Command):
"""
Returns the chosen number of the tag value to process (or "all")
"""
for i, value in enumerate(self.current_values):
self.out("%2d. %s" % (i + 1, value))
answer = "all"
answer = self.prompt('Which value to remove? Enter number or "all": ')
if not self.force:
for i, value in enumerate(self.current_values):
self.out("%2d. %s" % (i + 1, value))
answer = self.prompt('Which value to remove? Enter number or "all": ')
if answer != "all":
try:
answer = int(answer) - 1
if answer < 0 or answer >= len(self.current_values):
answer = None
except ValueError:
......@@ -79,7 +85,7 @@ class TagCommand(Command):
def _set_helper(self, p_old_value=""):
old_src = self.todo.source()
self.todo.set_tag(self.tag, self.value, self.force, p_old_value)
self.todo.set_tag(self.tag, self.value, self.force_add, p_old_value)
if old_src != self.todo.source():
self.todolist.set_dirty()
......@@ -109,9 +115,12 @@ class TagCommand(Command):
self._set()
def usage(self):
return """Synopsis: tag <NUMBER> <tag> [<value>]"""
return """Synopsis: tag [-a] [-f] <NUMBER> <tag> [<value>]"""
def help(self):
return """Sets the given tag to the given todo number with the given value. If
the value is omitted, the tag is removed from the todo item.
-a : Do not change the current value of the tag if it exists, but add a new value.
-f : Force setting/removing all values of the tag. Prevents interaction with the user.
"""
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment