Skip to content

Commit 6ca45e4

Browse files
Add argparse
1 parent 158fcf8 commit 6ca45e4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

_argparse.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from argparse import ArgumentParser
2+
3+
parser = ArgumentParser()
4+
parser.add_argument("-n", "--name", dest="name", type=str, help="select a name (required)")
5+
parser.add_argument("-t", "--times", dest="times", type=int, help="Several times you want to try?")
6+
7+
parser.add_argument("-o", "--on", action="store_true", dest="state")
8+
parser.add_argument("-f", "--off", action="store_false", dest="state")
9+
10+
args = parser.parse_args()
11+
12+
if not args.name:
13+
parser.error('name is not given')
14+
15+
if args.state:
16+
print('you turned the state on (-o | --on)')
17+
else:
18+
print('you turned the state off (-f | --off)')
19+
20+
print(args)

0 commit comments

Comments
 (0)