Commit 47479b6d authored by Jacek Sowiński's avatar Jacek Sowiński

Add todos from stdin via `topydo add -f -`

Credit for idea goes to Bram Schoenmakers (@bram85).
parent fafe392d
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
from datetime import date from datetime import date
import re import re
from sys import stdin
from topydo.lib.Config import config from topydo.lib.Config import config
from topydo.lib.Command import Command from topydo.lib.Command import Command
...@@ -34,14 +35,14 @@ class AddCommand(Command): ...@@ -34,14 +35,14 @@ class AddCommand(Command):
p_args, p_todolist, p_out, p_err, p_prompt) p_args, p_todolist, p_out, p_err, p_prompt)
self.text = ' '.join(p_args) self.text = ' '.join(p_args)
self.todo = None self.todo = None
self.from_file = False self.from_file = None
def _process_flags(self): def _process_flags(self):
opts, args = self.getopt('f') opts, args = self.getopt('f:')
for opt, value in opts: for opt, value in opts:
if opt == '-f': if opt == '-f':
self.from_file = True self.from_file = value
self.args = args self.args = args
...@@ -95,8 +96,12 @@ class AddCommand(Command): ...@@ -95,8 +96,12 @@ class AddCommand(Command):
self.todo.set_creation_date(date.today()) self.todo.set_creation_date(date.today())
def get_todos_from_file(self, p_filename): def get_todos_from_file(self):
f = open(p_filename, 'r') if self.from_file == '-':
f = stdin
else:
f = open(self.from_file, 'r')
todos = f.read().decode('utf-8').splitlines() todos = f.read().decode('utf-8').splitlines()
return todos return todos
...@@ -117,7 +122,7 @@ class AddCommand(Command): ...@@ -117,7 +122,7 @@ class AddCommand(Command):
self._process_flags() self._process_flags()
if self.from_file: if self.from_file:
new_todos = self.get_todos_from_file(self.args[0]) new_todos = self.get_todos_from_file()
for todo in new_todos: for todo in new_todos:
self._add_todo(todo) self._add_todo(todo)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment