forked from Rustam-Z/python-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparse_example.py
More file actions
40 lines (30 loc) Β· 1.15 KB
/
Copy pathargparse_example.py
File metadata and controls
40 lines (30 loc) Β· 1.15 KB
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
"""
How to run: python argparse_example.py --name Rustam --age 20 100 200 --values 1 2 3
1. --name is a required arg (flag)
2. x, y are positional arguments
3. --age is an optional argument
4. Multiple Input Arguments
5. Mutually Exclusive Arguments, depending on one argument, you want to restrict the use of another.
6. Subparser
https://towardsdatascience.com/a-simple-guide-to-command-line-arguments-with-argparse-6824c30ab1c3
"""
import argparse
# Create the parser
parser = argparse.ArgumentParser()
# Add an argument
parser.add_argument('--name', type=str, required=True)
parser.add_argument('--age', type=int)
parser.add_argument('x', type=int, help='The first value to multiply')
parser.add_argument('y', type=int, help='The second value to multiply')
parser.add_argument('--values', type=int, nargs=3) # Multiple Input Arguments, nargs='+
# Parse the argument
args = parser.parse_args()
sum_ = sum(args.values)
# Print "Hello" + the user input argument
print('Checking...', args.name)
if args.age:
print(args.name, 'is', args.age, 'years old.')
else:
print('Hello,', args.name + '!')
print('Multiplication:', args.x * args.y)
print('Sum:', sum_)