From db11128beb43018f8045554840f1989de5dc3fb5 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 28 Nov 2025 19:13:15 +0100 Subject: [PATCH] ref: Extract sys module use from main func Main function instead returns the given error code and only when invoked as a cli script will we use the error code to exit with the corresponding code to the shell. --- topen.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/topen.py b/topen.py index 341a8fb..b829745 100755 --- a/topen.py +++ b/topen.py @@ -23,14 +23,14 @@ import subprocess import sys from dataclasses import asdict, dataclass from pathlib import Path -from typing import Any, Self +from typing import Any, Self, cast from tasklib import Task, TaskWarrior NON_EXISTENT_PATH = Path("%%%%I_DONT_EXIST_%%%%") -def main(cfg: "TConf | None" = None, io: "_IO | None" = None): +def main(cfg: "TConf | None" = None, io: "_IO | None" = None) -> int: """Runs the cli interface. First sets up the correct options, with overrides in the following order: @@ -42,6 +42,8 @@ def main(cfg: "TConf | None" = None, io: "_IO | None" = None): pointing to the file. If the task does not yet have a note annotation it also adds it automatically. + + Returns the status code as int, 0 for success, 1 for error. """ if not cfg: cfg = build_config() @@ -50,12 +52,14 @@ def main(cfg: "TConf | None" = None, io: "_IO | None" = None): if not cfg.task_id: _ = io.err("Please provide task ID as argument.\n") + return 1 - task = get_task(id=cfg.task_id, data_location=cfg.task_data) - uuid = task["uuid"] - if not uuid: - _ = io.err(f"Could not find task for ID: {cfg.task_id}.") - sys.exit(1) + try: + task = get_task(id=cfg.task_id, data_location=cfg.task_data) + uuid = cast(str, task["uuid"]) + except Task.DoesNotExist: + _ = io.err(f"Could not find task for ID: {cfg.task_id}.\n") + return 1 fpath = get_notes_file(uuid, notes_dir=cfg.notes_dir, notes_ext=cfg.notes_ext) @@ -68,8 +72,9 @@ def main(cfg: "TConf | None" = None, io: "_IO | None" = None): if fpath.exists(): add_annotation_if_missing(task, annotation_content=cfg.notes_annot) io.out(f"Added annotation: {cfg.notes_annot}") - return + return 0 io.out("No note file, doing nothing.") + return 0 def get_task(id: str | int, data_location: Path) -> Task: @@ -341,4 +346,5 @@ class _IO: if __name__ == "__main__": - main() + exit = main() + sys.exit(exit)