This repository has been archived by the owner on Feb 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter_class.py
95 lines (83 loc) · 2.29 KB
/
formatter_class.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
>>> parser0.print_help() # doctest: +ELLIPSIS
usage: PROG [-h]
...
this description was indented weird but that is okay
...
optional arguments:
-h, --help show this help message and exit
...
likewise for this epilog whose whitespace will be cleaned up and whose words
will be wrapped across a couple lines
>>> parser1.print_help() # doctest: +ELLIPSIS
usage: PROG [-h]
...
Please do not mess up this text!
--------------------------------
I have indented it
exactly the way
I want it
...
optional arguments:
-h, --help show this help message and exit
>>> parser2.print_help() # doctest: +ELLIPSIS
usage: PROG [-h] [--foo FOO] [bar [bar ...]]
...
positional arguments:
bar BAR! (default: [1, 2, 3])
...
optional arguments:
-h, --help show this help message and exit
--foo FOO FOO! (default: 42)
>>> parser3.print_help() # doctest: +ELLIPSIS
usage: PROG [-h] [--foo int] float
...
positional arguments:
float
...
optional arguments:
-h, --help show this help message and exit
--foo int
"""
import argparse
import textwrap
# remove whitespaces #
######################
parser0 = argparse.ArgumentParser(
prog='PROG',
description='''this description
was indented weird
but that is okay''',
epilog='''
likewise for this epilog whose whitespace will
be cleaned up and whose words will be wrapped
across a couple lines'''
)
# preserve formatting #
#######################
parser1 = argparse.ArgumentParser(
prog='PROG',
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''\
Please do not mess up this text!
--------------------------------
I have indented it
exactly the way
I want it
'''))
# show default values #
#######################
parser2 = argparse.ArgumentParser(
prog='PROG',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser2.add_argument('--foo', type=int, default=42, help='FOO!')
parser2.add_argument('bar', nargs='*', default=[1, 2, 3], help='BAR!')
# show argument types #
#######################
parser3 = argparse.ArgumentParser(
prog='PROG',
formatter_class=argparse.MetavarTypeHelpFormatter
)
parser3.add_argument('--foo', type=int)
parser3.add_argument('bar', type=float)