Commit a7d51550 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Simplify TagCommand by eliminating the subsubcommands.

parent 30c961bf
This diff is collapsed.
...@@ -26,21 +26,35 @@ class TagCommand(Command): ...@@ -26,21 +26,35 @@ class TagCommand(Command):
p_prompt=lambda a: None): p_prompt=lambda a: None):
super(TagCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt) super(TagCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.subsubcommand = None self.force = False
self.todo = None self.todo = None
self.tag = None self.tag = None
self.value = None self.value = None
self.values = [] self.values = []
def _process_flags(self):
flags, args = self.getopt("f")
for flag, value in flags:
if flag == "-f":
self.force = True
self.args = args
def _process_args(self):
self._process_flags()
try: try:
self.subsubcommand = self.argument(0) number = convert_todo_number(self.argument(0))
number = convert_todo_number(self.argument(1))
self.todo = self.todolist.todo(number) self.todo = self.todolist.todo(number)
self.tag = self.argument(2) self.tag = self.argument(1)
self.current_values = self.todo.tag_values(self.tag) self.current_values = self.todo.tag_values(self.tag)
self.value = self.argument(3)
except (InvalidCommandArgument, InvalidTodoNumberException, TodoList.InvalidTodoException): except (InvalidCommandArgument, InvalidTodoNumberException, TodoList.InvalidTodoException):
pass self.error("Invalid todo number.")
try:
self.value = self.argument(2)
except InvalidCommandArgument:
self.value = ""
def _print(self): def _print(self):
self.out(pretty_print(self.todo, [self.todolist.pp_number()])) self.out(pretty_print(self.todo, [self.todolist.pp_number()]))
...@@ -65,63 +79,41 @@ class TagCommand(Command): ...@@ -65,63 +79,41 @@ class TagCommand(Command):
return answer return answer
def _add(self): def _set_helper(self, p_old_value=""):
self._set(True)
def _set_helper(self, p_force, p_old_value=""):
old_src = self.todo.source() old_src = self.todo.source()
self.todo.set_tag(self.tag, self.value, p_force, p_old_value) self.todo.set_tag(self.tag, self.value, self.force, p_old_value)
if old_src != self.todo.source(): if old_src != self.todo.source():
self.todolist.set_dirty() self.todolist.set_dirty()
def _set(self, p_force_add=False): def _set(self):
if self.value == None: if len(self.current_values) > 1:
self.error("Missing value for tag.") answer = self._choose()
self.error(self.usage())
else:
if len(self.current_values) > 1:
answer = self._choose()
if answer == "all":
for value in self.current_values:
self._set_helper(False, value)
elif answer != None and self.value != self.current_values[answer]:
self._set_helper(False, self.current_values[answer])
else: # if not self.todo.has_tag(self.tag, self.value): if answer == "all":
self._set_helper(p_force_add) for value in self.current_values:
self._set_helper(value)
elif answer != None and self.value != self.current_values[answer]:
self._set_helper(self.current_values[answer])
self._print() else:
self._set_helper()
def _rm(self): self._print()
self.value = ""
self._set()
def execute(self): def execute(self):
if not super(TagCommand, self).execute(): if not super(TagCommand, self).execute():
return False return False
dispatch = { self._process_args()
"add": self._add,
"set": self._set, if self.todo and self.tag:
"del": self._rm, self._set()
"rm": self._rm,
}
if self.subsubcommand in dispatch and self.todo and self.tag:
dispatch[self.subsubcommand]()
elif self.subsubcommand not in dispatch:
self.error(self.usage())
elif not self.todo:
self.error("Invalid todo number.")
def usage(self): def usage(self):
return """Synopsis: return """Synopsis: tag <NUMBER> <tag> [<value>]"""
tag (add|set) <NUMBER> <tag> <value>
tag rm <NUMBER> <tag> [value]"""
def help(self): def help(self):
return """* add: Add a tag to the given todo. return """Sets the given tag to the given todo number with the given value. If
* set: Changes a tag of the given todo. the value is omitted, the tag is removed from the todo item.
* rm: Removes a tag from the given todo.""" """
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