Paul's Python CoursePPC

Project 16 of 20, for Paul

Command-Line To-Do Tool

Paul's to-do list for the course code, on the Workshop terminal: add, list and done, kept in a file between runs, and polite when it's used wrongly.

About an hour and a half, in 13 steps.

Sign up free to open itLog in

Act 2 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • PaulMy to-dos are in comments all over the course code, and there were forty of them at the last count.
  • PaulEvery time Neil asks what's left to do, I have to go hunting through the files.
  • PaulI want a list on the terminal we can both run, to add a job, see the list and tick one off.
  • PaulIt should still be there tomorrow, and when it can't help it should say so, in words and in the exit code.

Write todo.py, a command-line to-do list kept in todo.json: a JSON list of tasks, each {"number": 1, "task": "rename x in grades.py", "done": false}. Use argparse, with three commands:

  • add TASK puts the task on the end, numbered one more than the highest so far (1 for the first), saves the list, and prints Added 4: fix the clock.
  • list prints every task as 1 [ ] rename x in grades.py, or [x] when it's done. list --open leaves out the done ones. An empty list prints Nothing on the list.
  • done NUMBER ticks that task off, saves the list, and prints Done: retrain the spam filter, or Already done: mark the Act I tests.

--file NAME, before the command, keeps the list in another file. A file that isn't there yet is an empty list. Two things are failures: say so on sys.stderr and exit with 1:

  • There's no task 9.
  • todo.json isn't a to-do list I can read., for a file that isn't JSON

A command it doesn't know, or a number that isn't one, is a usage error: argparse deals with those, with exit code 2.

What it prints
$ python todo.py add "fix the clock"
Added 4: fix the clock
$ python todo.py done 3
Done: retrain the spam filter
$ python todo.py list
1 [ ] rename x in grades.py
2 [x] mark the Act I tests
3 [x] retrain the spam filter
4 [ ] fix the clock

What you learn

What's in it

  1. Words after the program's name
  2. What was typed
  3. How it finished
  4. Stop everything
  5. Let argparse read it
  6. argparse reads it for you
  7. Commands of its own
  8. Done, number three
  9. How Paul would do it
  10. Fix Aaesha's version
  11. Make the to-do tool
  12. Ship it