Add subparses for modes
this allows for varying additional parameters based on mode.
This commit is contained in:
parent
f4a2735da0
commit
6915ea0368
18
tx
18
tx
@ -4,20 +4,24 @@ from datetime import datetime, timedelta
|
|||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
# Parse command line arguments
|
# Parse command line arguments
|
||||||
parser = argparse.ArgumentParser(description="Description")
|
parser = argparse.ArgumentParser(prog='time.txt', description="Description")
|
||||||
|
subparsers = parser.add_subparsers(help='sub-command help', dest='mode')
|
||||||
|
|
||||||
# add arguments to the parser
|
|
||||||
parser.add_argument("-f", "--file")
|
parser.add_argument("-f", "--file")
|
||||||
parser.add_argument("mode",
|
|
||||||
help="display the square of a given number",
|
# create the parser for the "a" command
|
||||||
nargs='?', default="total",
|
parser_a = subparsers.add_parser('total', help='a help')
|
||||||
choices=["total", "add"])
|
|
||||||
|
# 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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
filename = args.file if args.file else 'time.txt'
|
filename = args.file if args.file else 'time.txt'
|
||||||
|
|
||||||
if args.mode == "total":
|
if not args.mode or args.mode == "total":
|
||||||
with open(filename, 'r') as time_file:
|
with open(filename, 'r') as time_file:
|
||||||
total_time = timedelta()
|
total_time = timedelta()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user