karl 6915ea0368 Add subparses for modes
this allows for varying additional parameters based on mode.
2020-12-15 21:20:26 +01:00

41 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
from datetime import datetime, timedelta
import argparse
# Parse command line arguments
parser = argparse.ArgumentParser(prog='time.txt', description="Description")
subparsers = parser.add_subparsers(help='sub-command help', dest='mode')
parser.add_argument("-f", "--file")
# create the parser for the "a" command
parser_a = subparsers.add_parser('total', help='a help')
# create the parser for the "b" command
parser_b = subparsers.add_parser('add', help='b help')
parser_b.add_argument("time")
parser_b.add_argument("description")
args = parser.parse_args()
filename = args.file if args.file else 'time.txt'
if not args.mode or args.mode == "total":
with open(filename, 'r') as time_file:
total_time = timedelta()
for line in time_file:
date_time_description = line.split(" ", 2)
hours, minutes = date_time_description[1].split(':', 1)
delta = timedelta(hours=int(hours), minutes=int(minutes))
total_time += delta
print('Total time: ', total_time)
elif args.mode == "add":
with open(filename, 'a') as time_file:
time_file.write("A new line")