diff --git a/README.md b/README.md index ef8c40d..631258a 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Simple time/task logging on the command line. Plain-text log, no dependencies. ```bash tl add [-t|--time ] # start a task now, or at (14:30, 1h5m, 1h, 15m) -tl note # add a note to the current task +tl note [-t|--time ] # add a note to the current task, now or at tl help # overview of all commands tl version # show the version diff --git a/tests/test_commands.py b/tests/test_commands.py index 145fc75..65afff7 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -77,6 +77,12 @@ def test_missing_value(self): with contextlib.redirect_stderr(io.StringIO()): self.assertEqual(commands._get_time_flag(["-t"]), (None, None)) + def test_missing_value_uses_prefix(self): + err = io.StringIO() + with contextlib.redirect_stderr(err): + commands._get_time_flag(["-t"], "tl note") + self.assertTrue(err.getvalue().startswith("tl note: -t needs a value")) + class AddTest(CommandTestCase): def test_add_now(self): @@ -137,12 +143,35 @@ def test_note(self): self.assertEqual(out, "Note added: PR created (12:00)\n") self.assertEqual(self.lines(), ["2026-09-23 12:00\tnote\tPR created"]) - def test_time_flag_rejected(self): - for flag in ("-t", "--time"): - with self.subTest(flag=flag): - code, _, err = self.run_cmd(commands.note, [flag, "09:00", "x"]) + def test_note_with_clock_time(self): + code, out, _ = self.run_cmd(commands.note, ["-t", "09:30", "Decided"]) + self.assertEqual(code, 0) + self.assertEqual(out, "Note added: Decided (09:30)\n") + self.assertEqual(self.lines(), ["2026-09-23 09:30\tnote\tDecided"]) + + def test_note_with_duration(self): + self.run_cmd(commands.note, ["--time", "20m", "Called"]) + self.assertEqual(self.lines(), ["2026-09-23 11:40\tnote\tCalled"]) + + def test_bad_time_flag(self): + code, out, err = self.run_cmd(commands.note, ["-t", "nope", "x"]) + self.assertEqual(code, 1) + self.assertEqual(out, "") + self.assertIn("tl note: can't parse time flag 'nope'", err) + self.assertFalse(self.day_file().exists()) + + def test_flag_without_value(self): + code, _, err = self.run_cmd(commands.note, ["-t"]) + self.assertEqual(code, 1) + self.assertIn("tl note: -t needs a value", err) + + def test_flag_without_text(self): + for args in (["-t", "5m"], ["-t", "5m", " "]): + with self.subTest(args=args): + code, out, err = self.run_cmd(commands.note, args) self.assertEqual(code, 1) - self.assertIn("not allowed", err) + self.assertEqual(out, "") + self.assertIn("description is missing", err) self.assertFalse(self.day_file().exists()) def test_missing_text(self): diff --git a/timelog/commands.py b/timelog/commands.py index 390097d..b5aec5e 100644 --- a/timelog/commands.py +++ b/timelog/commands.py @@ -17,12 +17,12 @@ def _now() -> datetime: return datetime.now().replace(second=0, microsecond=0) -def _get_time_flag(args): +def _get_time_flag(args, prefix="tl add"): """Return (time_string_or_None, remaining_args), pulling out -t/--time and its value. The flag is recognized only as the first argument""" if args and args[0] in (TIME_FLAGS): if len(args) < 2: - print(f"tl add: {args[0]} needs a value, e.g. {args[0]} 14:30", file=sys.stderr) + print(f"{prefix}: {args[0]} needs a value, e.g. {args[0]} 14:30", file=sys.stderr) return None, None return args[1], args[2:] return None, args @@ -56,7 +56,7 @@ def add(args: List[str]) -> int: tl add -t 14:30 Meeting tl add --time 1h30m Long lunch """ - time_str, rest = _get_time_flag(args) + time_str, rest = _get_time_flag(args, "tl add") if time_str is None and rest is None: return 1 @@ -86,16 +86,32 @@ def add(args: List[str]) -> int: def note(args: List[str]) -> int: - """tl note : add a note.""" - if args and args[0] in (TIME_FLAGS): - print(render.flag_not_allowed("tl note", args[0]), file=sys.stderr) + """tl note [-t|--time ] : add a note. + + With the flag, value is either a time (12:00) or how long ago it happened (1h30m). + + Examples: + tl note PR created + tl note -t 14:30 Decided to postpone + tl note --time 20m Called the client + """ + time_str, rest = _get_time_flag(args, "tl note") + if time_str is None and rest is None: return 1 - entry = Entry(time=_now(), type="note", text=" ".join(args[0:])) + when = _now() + if time_str: + try: + when = _parse_time_flag(time_str, now=when) + except ValueError: + print(f"tl note: can't parse time flag '{time_str}'. Use HH:MM or e.g. 1h5m, 15m", file=sys.stderr) + return 1 + + entry = Entry(time=when, type="note", text=" ".join(rest)) if not entry.text: print( - render.missing_argument_error("tl note", "description", "tl note "), + render.missing_argument_error("tl note", "description", "tl note [-t