From 0204accd0538168b02ec532d1ad80805abcfa387 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 28 May 2026 13:17:29 +0200 Subject: [PATCH] Wire up -o for stories; remove -o from status - Extract _add_output_arg() helper; add it explicitly to summary, csv, and stories subparsers (removes it implicitly from status) - _cmd_stories now honours args.output, redirecting print_stories output to the given file (same pattern as _cmd_summary) - Update README with stories -o example --- README.md | 1 + src/timesheets/cli.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a4442bd..b0355d6 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ uv run timesheets csv -w --raw --joplin # full week, no aggregation ```sh uv run timesheets stories --joplin # today uv run timesheets stories -w --joplin # full week +uv run timesheets stories -w --joplin -o stories.md # write to file ``` ### status diff --git a/src/timesheets/cli.py b/src/timesheets/cli.py index 9dfb378..1d56381 100644 --- a/src/timesheets/cli.py +++ b/src/timesheets/cli.py @@ -83,6 +83,10 @@ def _add_shared_args(parser: argparse.ArgumentParser) -> None: ), default=None, ) + + +def _add_output_arg(parser: argparse.ArgumentParser) -> None: + """Add the -o/--output argument to a subparser.""" parser.add_argument( "-o", "--output", @@ -109,6 +113,7 @@ def build_parser() -> argparse.ArgumentParser: help="Print a human-readable summary of time spent per project.", ) _add_shared_args(summary_parser) + _add_output_arg(summary_parser) summary_parser.add_argument( "--weekly", "-w", @@ -132,6 +137,7 @@ def build_parser() -> argparse.ArgumentParser: help="Export timesheet entries as CSV.", ) _add_shared_args(csv_parser) + _add_output_arg(csv_parser) csv_parser.add_argument( "--raw", action="store_true", @@ -160,6 +166,7 @@ def build_parser() -> argparse.ArgumentParser: help="List stories worked on, grouped by project.", ) _add_shared_args(stories_parser) + _add_output_arg(stories_parser) stories_parser.add_argument( "--weekly", "-w", @@ -399,7 +406,17 @@ def _cmd_stories(args: argparse.Namespace, config: dict) -> None: return project_map = _resolve_project_map(args, config) - print_stories(rows, project_map) + if args.output: + with open(args.output, "w", encoding="utf-8") as f: + old_stdout = sys.stdout + sys.stdout = f + try: + print_stories(rows, project_map) + finally: + sys.stdout = old_stdout + print(f"Written to {args.output}", file=sys.stderr) + else: + print_stories(rows, project_map) def _cmd_csv(args: argparse.Namespace, config: dict) -> None: