-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
43 lines (33 loc) · 957 Bytes
/
update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import argparse
FILE = os.path.expanduser('~/reminders.txt')
def update(op, info):
with open(FILE, 'r') as f:
lines = f.readlines()
if op == 'add':
lines.append(info+'\n')
else:
del lines[int(info)]
with open(FILE, 'w') as f:
for line in lines:
f.write(line)
def update_args():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-a')
group.add_argument('-d',action='store_true')
parser.add_argument('number',type=int,help='position in list to add or delete')
args = parser.parse_args()
if args.number < 1:
raise argparse.ArgumentTypeError('Enter a number greater than 0')
with open(FILE,'r') as f:
lines = f.readlines()
if args.d:
del lines[args.number]
else:
lines.insert(args.number,args.a+'\n')
with open(FILE,'w') as f:
for line in lines:
f.write(line)
if __name__ == '__main__':
update()