From cd123c2282344bcd5ce35ad56cb5b6774fdc92d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20Ob=C5=AFrka?= Date: Sun, 6 Mar 2016 00:36:00 +0100 Subject: Provide --lines command with small refactoring of necessary parts --- todoist | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/todoist b/todoist index d1ef7c7..96092ef 100755 --- a/todoist +++ b/todoist @@ -64,6 +64,9 @@ def arg_parser(): parser.add_argument("-i", "--indent", help="Set indentation of task (default is 1)", type=int) + parser.add_argument("-l", "--lines", + help="Use every line of stdin as one task", + action="store_true") parser.add_argument("msg", metavar="MSG", nargs='*', @@ -109,6 +112,17 @@ def find_candidate(name, storage): else: return candidates[0][1], None +def store_task(api, msg, kwargs_dict): + if not msg.strip(): + print("Warning: Empty message skipped", file=sys.stderr) + return + + status = api.add_item(msg, **kwargs_dict) + if "error" in status: + print("Store failed:", file=sys.stderr) + print(status["error"], file=sys.stderr) + sys.exit(1) + def main(): args = arg_parser() @@ -138,17 +152,11 @@ def main(): pprint(data, f) sys.exit(0) + ## Prepare data structures + kwargs = {} projects = build_project_list(data) collaborators = build_collaborators_list(data) - ## Prepare data - if not args.msg: - msg = sys.stdin.read().strip() - else: - msg = " ".join(args.msg) - - ## Prepare arguments - kwargs = {} ## Set date if not args.no_due_date: kwargs["date_string"] = args.date @@ -177,13 +185,21 @@ def main(): sys.exit(1) kwargs["responsible_uid"] = aid - ## Store task - status = api.add_item(msg, **kwargs) - if "error" in status: - print("Store failed:", file=sys.stderr) - print(status["error"], file=sys.stderr) - sys.exit(1) - + ## It is decided about storing tasks at this point + if args.msg: + msg = " ".join(args.msg) + store_task(api, msg, kwargs) + else: ## Read from stdin + if args.lines: + for line in sys.stdin: + if not line.strip(): + continue + store_task(api, line, kwargs) + else: + msg = sys.stdin.read().strip() + store_task(api, msg, kwargs) + + ## All operations done - commit and sync them api.commit() if __name__ == "__main__": -- cgit v1.2.3