Skip to content

Instantly share code, notes, and snippets.

@fayesafe
Created July 5, 2016 11:02
Show Gist options
  • Save fayesafe/1ed8ff0f3eaf44c4b177be63dc520162 to your computer and use it in GitHub Desktop.
Save fayesafe/1ed8ff0f3eaf44c4b177be63dc520162 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import os
def main(todo_file, argv):
if (argv.add):
add_task(todo_file, argv.add)
elif (argv.delete):
delete_task(todo_file, argv.delete)
else:
show_list(todo_file)
def add_task(todo_file, message):
with open(todo_file, 'a+') as todo:
todo.write('{:s}\n'.format(message[0]))
def show_list(todo_file):
try:
with open(todo_file, 'r') as todo:
index = 1
tasks = [x.rstrip() for x in todo.readlines()]
if (tasks):
for task in tasks:
print('{:d}:\t{:s}'.format(index, task))
index += 1
else:
print('No tasks in todo file ...')
except IOError as e:
print('No tasks in todo file ...')
def delete_task(todo_file, index):
try:
with open(todo_file, 'r+') as todo:
tasks = [x.rstrip() for x in todo.readlines()]
try:
deleted = tasks.pop(index-1)
print('Deleted: {:s}'.format(deleted))
todo.seek(0)
todo.truncate()
for task in tasks:
todo.write('{:s}\n'.format(task))
except IndexError as e:
print('Task non-existant ...')
except IOError as e:
print('No tasks in todo file ...')
if __name__ == '__main__':
todo_file = os.path.expanduser('~') + '/.todo'
argparser = argparse.ArgumentParser()
argparser.add_argument('-a', '--add', nargs='+',
help='add task to todo-list.')
argparser.add_argument('-d', '--delete', type=int,
nargs='?', help='delete item with index [i]')
argv = argparser.parse_args()
main(todo_file, argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment