Skip to content

todo task clear command

How about removing all tasks? I.E clearing the project?

The idea is similar to the Implement the clear CLI Command

First, we need to update the Todoer controller:


class Todoer:


    def remove_all(self) -> CurrentTodo:
        """Remove all to-dos from the database."""
        write = self._db_handler.write_todos({f"{self.project_name}": []})
        return CurrentTodo({}, write.error)


The next thing is to create the file in the task package:

$ tree
└── TODO
    ├── task
    │   ├──
    │   ├──
    │   ├──
    │   ├──
    │   ├──
    │   └──
    ├── todo
    └── _todos

3 directories, 13 files

The actual implementation is similar to the original blog post, here we are intentionally using a decorator as a prompt:

from functools import wraps
from _todos import todo

def _prompt(func_: callable) -> callable:
    def wrapper(project_name: str):
        while True:
            choice = input("Delete all to-dos? [y/N]:")
            if 'y' == choice:
                return func_(project_name)
            elif 'N' == choice:
                print('Operation cancelled')
            print('Invalid choice. Try again')

    return wrapper

def clear(project_name: str) -> None:
    Deleting all tasks

        project_name (str): the project name

    Return: None
    todo_ = todo.get_todoer(project_name)

    print("All to-dos were removed")

Get the help of the clear command:

$ ./todo task clear -h
usage: todo task clear [-h] project_name

positional arguments:
  project_name  the project name

optional arguments:
  -h, --help    show this help message and exit

As you see, the actual code and also the DynaCLI implementation did not interfere with the _prompt decorator.

Let's test our clear command:

$ ./todo task list daily
ID. Is Done | Description
1 > morning walk
2 X night walk
3 X gym
4 X eat vegetables
$ ./todo task clear daily
Delete all to-dos? [y/N]:N
Operation canceled
./todo task clear daily
Delete all to-dos? [y/N]:sasd
Invalid choice. Try again
Delete all to-dos? [y/N]:y
All to-dos were removed
$ /todo task list daily
ID. Is Done | Description

Dead simple. DynaCLI just converted clear function to the CLI clear command(yes it works with decorated functions).