-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargsParse.py
48 lines (34 loc) · 1.2 KB
/
argsParse.py
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
41
42
43
44
45
46
47
48
# import argparse
# parser = argparse.ArgumentParser( description='Provides a personal greeting.')
# parser.add_argument(
# #-n: its short for name
# '_n', '--name', metavar="name",
# required=True, help="The name of the person to greet."
# )
# args = parser.parse_args()
# msg = f"Hello {args.name}!"
# print(msg)
# -- result:
# python3 argsParse.py
# Traceback (most recent call last):
# File "argsParse.py", line 6, in <module>
# parser.add_argument(
# File "/usr/lib/python3.8/argparse.py", line 1366, in add_argument
# kwargs = self._get_optional_kwargs(*args, **kwargs)
# File "/usr/lib/python3.8/argparse.py", line 1501, in _get_optional_kwargs
# raise ValueError(msg % args)
# ValueError: invalid option string '_n': must start with a character '-'
#---------
import argparse
parser = argparse.ArgumentParser(description='Provides a personal greeting.')
parser.add_argument(
'-n', "--name", metavar="name",
required=True, help="The name of the person to greet."
)
args = parser.parse_args()
msg = f"Hello {args.name}!"
print(msg)
# --- result
# python3 argsParse.py
# usage: argsParse.py [-h] -n name
# argsParse.py: error: the following arguments are required: -n/--name