Python 有两个内建的模块用于处理命令行参数:
一个是 getopt,getopt只能简单处理 命令行参数。
另一个是 optparse,是一个能够让程式设计人员轻松设计出简单明了、易于使用、符合标准的Unix命令列程式的Python模块。生成使用和帮助信息。
下面是一个简单的示例脚本optparse_exampl_1.py:
[root@localhost python]# vim optparse_exampl_1.py
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/env python
from
optparse
import
OptionParser
parser
=
OptionParser()
parser.add_option(
"-f"
,
"--file"
, dest
=
"filename"
,
help
=
"write report to FILE"
, metavar
=
"FILE"
)
parser.add_option(
"-q"
,
"--quiet"
,
action
=
"store_false"
, dest
=
"verbose"
, default
=
True
,
help
=
"don't print status messages to stdout"
)
(options, args)
=
parser.parse_args()
|
现在你可以在命令行进行如下输入:
1
2
3
4
5
|
<yourscript> --file=outfile -q
<yourscript> -f outfile --quiet
<yourscript> --quiet --file outfile
<yourscript> -q -foutfile
<yourscript> -qfoutfile
|
上面这些命令是相同效果的。除此之外, optparse 还为我们自动生成命令行的帮助信息:
1
2
|
<yourscript>
-
h
<yourscript>
-
-
help
|
optparse将打印脚本的选项和帮助信息:
1
2
3
4
5
6
7
|
[root@localhost python]
# ./optparse_exampl_1.py -h
Usage: optparse_exampl_1.py [options]
Options:
-
h,
-
-
help
show this
help
message
and
exit
-
f
FILE
,
-
-
file
=
FILE
write report to
FILE
-
q,
-
-
quiet don't
print
status messages to stdout
|
1
2
3
|
from
optparse
import
OptionParser
[...]
parser
=
OptionParser()
|
现在可以定义命令行选项,基本语法是:
1
2
|
parser.add_option(opt_str, ...,
attr
=
value, ...)
|
每种选项各有一个或多个选项的字符串,比如 -f 或 --file,通常每个选项将有一个短选项和一个长选项。例如:
1
|
parser.add_option(
"-f"
,
"--file"
, ...)
|
1
|
(options, args)
=
parser.parse_args()
|
1
2
3
4
5
|
parser.add_option(
"-f"
,
"--file"
,
action
=
"store"
,
type
=
"string"
, dest
=
"filename"
)
args
=
[
"-f"
,
"foo.txt"
]
(options, args)
=
parser.parse_args(args)
print
options.filename
|
1
|
parser.add_option(
"-n"
,
type
=
"int"
, dest
=
"num"
)
|
注意:这个选项没有长选项,长选项也是可选的,如果没有指定dest选项,将用命令行的参数名对options对象的值进行存取。store也有其他的两种形式: stort_true 和 store_false, 用于处理带命令行选项后面不带值的情况,例如: -v,-q等命令行参数。
1
2
|
parser.add_option(
"-v"
, action
=
"store_true"
, dest
=
"verbose"
)
parser.add_option(
"-q"
, action
=
"store_false"
, dest
=
"verbose"
)
|
这样的话,当解析到 ‘-v’,options.verbose 将被赋予 True 值,反之,解析到 ‘-q’,会被赋予 False 值。
其它的 actions 值还有:
store_const 、append 、count 、callback
默认值
parse_args() 方法提供了一个 default 参数用于设置默认值。如:
1
2
|
parser.add_option(
"-v"
, action
=
"store_true"
, dest
=
"verbose"
)
parser.add_option(
"-q"
, action
=
"store_false"
, dest
=
"verbose"
, default
=
True
)
|
又或者使用set_defaults例如:
1
2
3
|
parser.set_defaults(verbose
=
True
)
parser.add_option(...)
(options, args)
=
parser.parse_args()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
usage
=
"usage: %prog [options] arg1 arg2"
parser
=
OptionParser(usage
=
usage)
parser.add_option(
"-v"
,
"--verbose"
,
action
=
"store_true"
, dest
=
"verbose"
, default
=
True
,
help
=
"make lots of noise [default]"
)
parser.add_option(
"-q"
,
"--quiet"
,
action
=
"store_false"
, dest
=
"verbose"
,
help
=
"be vewwy quiet (I'm hunting wabbits)"
)
parser.add_option(
"-f"
,
"--filename"
,
metavar
=
"FILE"
,
help
=
"write output to FILE"
)
parser.add_option(
"-m"
,
"--mode"
,
default
=
"intermediate"
,
help
=
"interaction mode: novice, intermediate, "
"or expert [default: %default]"
)
|
当 optparse 解析到 -h 或者 –help 命令行参数时,会调用 parser.print_help() 打印程序的帮助信息:
1
2
3
4
5
6
7
8
9
10
|
Usage: <yourscript> [options] arg1 arg2
Options:
-
h,
-
-
help
show this
help
message
and
exit
-
v,
-
-
verbose make lots of noise [default]
-
q,
-
-
quiet be vewwy quiet (I'm hunting wabbits)
-
f
FILE
,
-
-
filename
=
FILE
write output to
FILE
-
m MODE,
-
-
mode
=
MODE interaction mode: novice, intermediate,
or
expert [default: intermediate]
|
1
|
usage
=
"usage: %prog [options] arg1 arg2"
|
1
|
-
m MODE,
-
-
mode
=
MODE
|
1
2
3
4
5
|
group
=
OptionGroup(parser,
"Dangerous Options"
,
"Caution: use these options at your own risk. "
"It is believed that some of them bite."
)
group.add_option(
"-g"
, action
=
"store_true"
,
help
=
"Group option."
)
parser.add_option_group(group)
|
输出如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Usage: <yourscript> [options] arg1 arg2
Options:
-
h,
-
-
help
show this
help
message
and
exit
-
v,
-
-
verbose make lots of noise [default]
-
q,
-
-
quiet be vewwy quiet (I'm hunting wabbits)
-
f
FILE
,
-
-
filename
=
FILE
write output to
FILE
-
m MODE,
-
-
mode
=
MODE interaction mode: novice, intermediate,
or
expert [default: intermediate]
Dangerous Options:
Caution: use these options at your own risk. It
is
believed that some
of them bite.
-
g Group option.
|
完整的列子:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
group
=
OptionGroup(parser,
"Dangerous Options"
,
"Caution: use these options at your own risk. "
"It is believed that some of them bite."
)
group.add_option(
"-g"
, action
=
"store_true"
,
help
=
"Group option."
)
parser.add_option_group(group)
group
=
OptionGroup(parser,
"Debug Options"
)
group.add_option(
"-d"
,
"--debug"
, action
=
"store_true"
,
help
=
"Print debug information"
)
group.add_option(
"-s"
,
"--sql"
, action
=
"store_true"
,
help
=
"Print all SQL statements executed"
)
group.add_option(
"-e"
, action
=
"store_true"
,
help
=
"Print every action done"
)
parser.add_option_group(group)
|