The
argparse module makes it easy to write user-friendly command-line interfaces.
Right after we are going to show basic examples of use of this parser.
- Let us start with a very simple example which does (almost) nothing:
SOURCE CODE
1
2
3
4
| import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
|
OUTPUT
1
2
3
4
5
| $ ./argparse1.py -h
usage: argparse1.py [-h]
optional arguments:
-h, --help show this help message and exit
|
Let us configure description, version and epilogue
SOURCE CODE
1
2
3
4
5
6
7
| import argparse
# prog = sys.argv[0], you could change if you want, prog="myprogram"
parser = argparse.ArgumentParser(description='Description',
version="%(prog)s 0.1",
epilog="Epilogue")
parser.parse_args()
|
OUTPUT
1
2
3
4
5
6
7
8
9
10
| $ ./argparse2.py -h
usage: argparse2.py [-h] [-v]
Description
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
Epilogue
|
- Let us introduce a positional argument
SOURCE CODE
1
2
3
4
5
| import argparse
parser = argparse.ArgumentParser()
parser.add_argument("argument")
parser.parse_args()
|
OUTPUT
1
2
3
4
5
6
7
8
9
10
11
| $ ./argparse3.py -h
usage: argparse3.py [-h] argument
positional arguments:
argument Information about argument
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
Epilogue
|
- Let us introduce optional arguments
SOURCE CODE
1
2
3
4
5
6
7
8
9
10
11
| import argparse
parser = argparse.ArgumentParser()
# A optional argument A with necessary value
parser.add_argument("-a", "--argument-A",
help="Information about argument with pattern [-a ARGUMENT]")
# A optional argument B without necessary value
parser.add_argument("-b", "--argument-B",
help="Information about b argument with pattern [-b] ",
action="store_true")
parser.parse_args()
|
OUTPUT
1
2
3
4
5
6
7
8
| $ ./argparse4.py -h
usage: argparse4.py [-h] [-a ARGUMENT_A] [-b]
optional arguments:
-h, --help show this help message and exit
-a ARGUMENT_A, --argument-A ARGUMENT_A
Information about argument with pattern [-a ARGUMENT]
-b, --argument-B Information about b argument with pattern [-b]
|
- Let us introduce subcommands
SOURCE CODE
1
2
3
4
5
6
7
8
9
10
11
| import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='sub-command help')
# create the parser for the "a" command
parser_a = subparsers.add_parser('a', help='a help')
# create the parser for the "b" command
parser_b = subparsers.add_parser('b', help='b help')
parser.parse_args()
|
OUTPUT
1
2
3
4
5
6
7
8
9
10
| $ ./argparse5.py -h
usage: argparse5.py [-h] {a,b} ...
positional arguments:
{a,b} sub-command help
a a help
b b help
optional arguments:
-h, --help show this help message and exit
|
- Let us introduce mutual exclusion of arguments
SOURCE CODE
1
2
3
4
5
6
7
8
9
10
11
12
| import argparse
parser = argparse.ArgumentParser()
group1 = parser.add_mutually_exclusive_group(required=True)
group1.add_argument("-a", "--argument-A", action="store_true")
group1.add_argument("-b", "--argument-B", action="store_false")
group2 = parser.add_mutually_exclusive_group(required=True)
group2.add_argument("-c", "--argument-C", help="c help")
group2.add_argument("-d", "--argument-D", help="d help")
parser.parse_args()
|
OUTPUT
1
2
3
4
5
6
7
8
9
10
11
| $ ./argparse6.py -h
usage: argparse6.py [-h] (-a | -b) (-c ARGUMENT_C | -d ARGUMENT_D)
optional arguments:
-h, --help show this help message and exit
-a, --argument-A
-b, --argument-B
-c ARGUMENT_C, --argument-C ARGUMENT_C
c help
-d ARGUMENT_D, --argument-D ARGUMENT_D
d help
|
- Finally, let us see how to execute action of optional argument, although positional argument is empt
SOURCE CODE (partial)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| import sys
import argparse
# Parse the target url
parser.add_argument ("url" ,
nargs='?',
help="target URL")
# Parse argument to show license
parser.add_argument ("-l",
"--license",
help="show license",
action="store_true")
# Print license
if args.license:
print st.WS_LICENSE
sys.exit(0)
|
The tricky is to use
nargs='?' for positional argument and
sys.exit(0)
OUTPUT
1
2
3
4
5
6
7
8
9
10
11
12
| $ ./argparse7.py -h
usage: argparse7.py [-l] [url]
Description
positional arguments:
url target URL
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-l, --license show license
|
References:
[1]
http://docs.python.org/dev/library/argparse.html
No hay comentarios:
Publicar un comentario