Implement 'create' and 'add' modes
This commit is contained in:
parent
6915ea0368
commit
93655127db
21
tx
21
tx
@ -1,7 +1,14 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
|
|
||||||
|
def get_current_date():
|
||||||
|
return date.today().strftime("%Y-%m-%d")
|
||||||
|
|
||||||
|
def get_timefile_line(date, time, desc):
|
||||||
|
return date + " " + time + " " + desc + "\n"
|
||||||
|
|
||||||
# Parse command line arguments
|
# Parse command line arguments
|
||||||
parser = argparse.ArgumentParser(prog='time.txt', description="Description")
|
parser = argparse.ArgumentParser(prog='time.txt', description="Description")
|
||||||
@ -14,9 +21,12 @@ parser_a = subparsers.add_parser('total', help='a help')
|
|||||||
|
|
||||||
# create the parser for the "b" command
|
# create the parser for the "b" command
|
||||||
parser_b = subparsers.add_parser('add', help='b help')
|
parser_b = subparsers.add_parser('add', help='b help')
|
||||||
|
parser_b.add_argument("date", nargs='?', default=get_current_date())
|
||||||
parser_b.add_argument("time")
|
parser_b.add_argument("time")
|
||||||
parser_b.add_argument("description")
|
parser_b.add_argument("description")
|
||||||
|
|
||||||
|
parser_c = subparsers.add_parser('create', help='b help')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
filename = args.file if args.file else 'time.txt'
|
filename = args.file if args.file else 'time.txt'
|
||||||
@ -37,4 +47,11 @@ if not args.mode or args.mode == "total":
|
|||||||
|
|
||||||
elif args.mode == "add":
|
elif args.mode == "add":
|
||||||
with open(filename, 'a') as time_file:
|
with open(filename, 'a') as time_file:
|
||||||
time_file.write("A new line")
|
time_file.write(get_timefile_line(args.date, args.time, args.description))
|
||||||
|
|
||||||
|
elif args.mode == "create":
|
||||||
|
if (os.path.exists(filename)):
|
||||||
|
print("Error: File", filename, "already exists, can't create a new one!")
|
||||||
|
else:
|
||||||
|
with open(filename, 'w') as time_file:
|
||||||
|
time_file.write(get_timefile_line(get_current_date(), "00:01", "Setup time.txt"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user