Linux ip-148-66-134-25.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els11.x86_64 #1 SMP Sun Nov 3 09:06:59 UTC 2024 x86_64
Apache
: 148.66.134.25 | : 18.118.0.48
66 Domain
8.0.30
amvm
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
BLACK DEFEND!
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
49.27
KB
-rw-r--r--
__builtin__.html
10.26
KB
-rw-r--r--
__future__.html
13.79
KB
-rw-r--r--
__main__.html
7.05
KB
-rw-r--r--
_winreg.html
59.21
KB
-rw-r--r--
abc.html
23.9
KB
-rw-r--r--
aepack.html
13.16
KB
-rw-r--r--
aetools.html
14.91
KB
-rw-r--r--
aetypes.html
18.88
KB
-rw-r--r--
aifc.html
22.4
KB
-rw-r--r--
al.html
17.34
KB
-rw-r--r--
allos.html
33.72
KB
-rw-r--r--
anydbm.html
16.33
KB
-rw-r--r--
archiving.html
9.26
KB
-rw-r--r--
argparse.html
237.62
KB
-rw-r--r--
array.html
29.29
KB
-rw-r--r--
ast.html
34.98
KB
-rw-r--r--
asynchat.html
31.43
KB
-rw-r--r--
asyncore.html
36.51
KB
-rw-r--r--
atexit.html
16.8
KB
-rw-r--r--
audioop.html
31.36
KB
-rw-r--r--
autogil.html
8.19
KB
-rw-r--r--
base64.html
19.67
KB
-rw-r--r--
basehttpserver.html
34.04
KB
-rw-r--r--
bastion.html
11.04
KB
-rw-r--r--
bdb.html
36.68
KB
-rw-r--r--
binascii.html
20.67
KB
-rw-r--r--
binhex.html
10.58
KB
-rw-r--r--
bisect.html
23.24
KB
-rw-r--r--
bsddb.html
26.43
KB
-rw-r--r--
bz2.html
26.08
KB
-rw-r--r--
calendar.html
37.79
KB
-rw-r--r--
carbon.html
48.94
KB
-rw-r--r--
cd.html
27.96
KB
-rw-r--r--
cgi.html
49.92
KB
-rw-r--r--
cgihttpserver.html
13.1
KB
-rw-r--r--
cgitb.html
11.41
KB
-rw-r--r--
chunk.html
14.66
KB
-rw-r--r--
cmath.html
25.63
KB
-rw-r--r--
cmd.html
26.09
KB
-rw-r--r--
code.html
24.58
KB
-rw-r--r--
codecs.html
100.64
KB
-rw-r--r--
codeop.html
14.84
KB
-rw-r--r--
collections.html
133.96
KB
-rw-r--r--
colorpicker.html
7.52
KB
-rw-r--r--
colorsys.html
11.04
KB
-rw-r--r--
commands.html
14.36
KB
-rw-r--r--
compileall.html
16.83
KB
-rw-r--r--
compiler.html
67.75
KB
-rw-r--r--
configparser.html
62.13
KB
-rw-r--r--
constants.html
12.83
KB
-rw-r--r--
contextlib.html
19.39
KB
-rw-r--r--
cookie.html
39.07
KB
-rw-r--r--
cookielib.html
83.82
KB
-rw-r--r--
copy.html
12.19
KB
-rw-r--r--
copy_reg.html
13.76
KB
-rw-r--r--
crypt.html
10.04
KB
-rw-r--r--
crypto.html
7.59
KB
-rw-r--r--
csv.html
67.37
KB
-rw-r--r--
ctypes.html
238.78
KB
-rw-r--r--
curses.ascii.html
22.29
KB
-rw-r--r--
curses.html
146.63
KB
-rw-r--r--
curses.panel.html
14.39
KB
-rw-r--r--
custominterp.html
7.62
KB
-rw-r--r--
datatypes.html
16.84
KB
-rw-r--r--
datetime.html
226.59
KB
-rw-r--r--
dbhash.html
15.48
KB
-rw-r--r--
dbm.html
12.07
KB
-rw-r--r--
debug.html
10.15
KB
-rw-r--r--
decimal.html
194.44
KB
-rw-r--r--
development.html
14.17
KB
-rw-r--r--
difflib.html
84.83
KB
-rw-r--r--
dircache.html
11.41
KB
-rw-r--r--
dis.html
69.95
KB
-rw-r--r--
distutils.html
8.05
KB
-rw-r--r--
dl.html
16.33
KB
-rw-r--r--
doctest.html
165.54
KB
-rw-r--r--
docxmlrpcserver.html
16.43
KB
-rw-r--r--
dumbdbm.html
14.02
KB
-rw-r--r--
dummy_thread.html
9.43
KB
-rw-r--r--
dummy_threading.html
8.37
KB
-rw-r--r--
easydialogs.html
30.55
KB
-rw-r--r--
email-examples.html
45.65
KB
-rw-r--r--
email.charset.html
26.8
KB
-rw-r--r--
email.encoders.html
11.86
KB
-rw-r--r--
email.errors.html
15.77
KB
-rw-r--r--
email.generator.html
20.77
KB
-rw-r--r--
email.header.html
26.92
KB
-rw-r--r--
email.html
44.24
KB
-rw-r--r--
email.iterators.html
11.52
KB
-rw-r--r--
email.message.html
63.16
KB
-rw-r--r--
email.mime.html
27.93
KB
-rw-r--r--
email.parser.html
30.45
KB
-rw-r--r--
email.util.html
24.46
KB
-rw-r--r--
errno.html
37.99
KB
-rw-r--r--
exceptions.html
56.13
KB
-rw-r--r--
fcntl.html
22.67
KB
-rw-r--r--
filecmp.html
22.3
KB
-rw-r--r--
fileformats.html
9.14
KB
-rw-r--r--
fileinput.html
24.28
KB
-rw-r--r--
filesys.html
10.2
KB
-rw-r--r--
fl.html
49.92
KB
-rw-r--r--
fm.html
11.91
KB
-rw-r--r--
fnmatch.html
14.58
KB
-rw-r--r--
formatter.html
34.06
KB
-rw-r--r--
fpectl.html
16.01
KB
-rw-r--r--
fpformat.html
10.59
KB
-rw-r--r--
fractions.html
22.61
KB
-rw-r--r--
framework.html
33.34
KB
-rw-r--r--
frameworks.html
7.14
KB
-rw-r--r--
ftplib.html
43.99
KB
-rw-r--r--
functions.html
183.14
KB
-rw-r--r--
functools.html
27.17
KB
-rw-r--r--
future_builtins.html
13.04
KB
-rw-r--r--
gc.html
25.75
KB
-rw-r--r--
gdbm.html
15.96
KB
-rw-r--r--
gensuitemodule.html
11.51
KB
-rw-r--r--
getopt.html
23.66
KB
-rw-r--r--
getpass.html
10.65
KB
-rw-r--r--
gettext.html
78.76
KB
-rw-r--r--
gl.html
22.09
KB
-rw-r--r--
glob.html
13.26
KB
-rw-r--r--
grp.html
10.49
KB
-rw-r--r--
gzip.html
18.99
KB
-rw-r--r--
hashlib.html
18.2
KB
-rw-r--r--
heapq.html
31.61
KB
-rw-r--r--
hmac.html
10.46
KB
-rw-r--r--
hotshot.html
18.65
KB
-rw-r--r--
htmllib.html
25.32
KB
-rw-r--r--
htmlparser.html
39.11
KB
-rw-r--r--
httplib.html
62.95
KB
-rw-r--r--
i18n.html
9.52
KB
-rw-r--r--
ic.html
17.17
KB
-rw-r--r--
idle.html
20.9
KB
-rw-r--r--
imageop.html
14.76
KB
-rw-r--r--
imaplib.html
51.99
KB
-rw-r--r--
imgfile.html
11.71
KB
-rw-r--r--
imghdr.html
11.3
KB
-rw-r--r--
imp.html
34.34
KB
-rw-r--r--
importlib.html
8.26
KB
-rw-r--r--
imputil.html
31.81
KB
-rw-r--r--
index.html
72.78
KB
-rw-r--r--
inspect.html
50.71
KB
-rw-r--r--
internet.html
24.87
KB
-rw-r--r--
intro.html
8.93
KB
-rw-r--r--
io.html
98.13
KB
-rw-r--r--
ipc.html
13.41
KB
-rw-r--r--
itertools.html
115.91
KB
-rw-r--r--
jpeg.html
12.74
KB
-rw-r--r--
json.html
67.04
KB
-rw-r--r--
keyword.html
7.68
KB
-rw-r--r--
language.html
11.03
KB
-rw-r--r--
linecache.html
10.59
KB
-rw-r--r--
locale.html
55.14
KB
-rw-r--r--
logging.config.html
63.36
KB
-rw-r--r--
logging.handlers.html
69.64
KB
-rw-r--r--
logging.html
95.64
KB
-rw-r--r--
mac.html
21.79
KB
-rw-r--r--
macos.html
14.76
KB
-rw-r--r--
macosa.html
12.96
KB
-rw-r--r--
macostools.html
15.52
KB
-rw-r--r--
macpath.html
7.76
KB
-rw-r--r--
mailbox.html
156.75
KB
-rw-r--r--
mailcap.html
13.21
KB
-rw-r--r--
markup.html
18.77
KB
-rw-r--r--
marshal.html
17.98
KB
-rw-r--r--
math.html
39.24
KB
-rw-r--r--
md5.html
13.97
KB
-rw-r--r--
mhlib.html
21.54
KB
-rw-r--r--
mimetools.html
19.25
KB
-rw-r--r--
mimetypes.html
28.39
KB
-rw-r--r--
mimewriter.html
15.02
KB
-rw-r--r--
mimify.html
13.36
KB
-rw-r--r--
miniaeframe.html
12.2
KB
-rw-r--r--
misc.html
6.87
KB
-rw-r--r--
mm.html
9.03
KB
-rw-r--r--
mmap.html
28.36
KB
-rw-r--r--
modulefinder.html
15.31
KB
-rw-r--r--
modules.html
8.46
KB
-rw-r--r--
msilib.html
52.43
KB
-rw-r--r--
msvcrt.html
19.37
KB
-rw-r--r--
multifile.html
24.3
KB
-rw-r--r--
multiprocessing.html
365.71
KB
-rw-r--r--
mutex.html
11.23
KB
-rw-r--r--
netdata.html
16.98
KB
-rw-r--r--
netrc.html
12.3
KB
-rw-r--r--
new.html
12.12
KB
-rw-r--r--
nis.html
10.64
KB
-rw-r--r--
nntplib.html
41.92
KB
-rw-r--r--
numbers.html
37.75
KB
-rw-r--r--
numeric.html
13.55
KB
-rw-r--r--
operator.html
82
KB
-rw-r--r--
optparse.html
222.56
KB
-rw-r--r--
os.html
214.25
KB
-rw-r--r--
os.path.html
38.34
KB
-rw-r--r--
ossaudiodev.html
41.5
KB
-rw-r--r--
othergui.html
9.08
KB
-rw-r--r--
parser.html
39.36
KB
-rw-r--r--
pdb.html
33.96
KB
-rw-r--r--
persistence.html
14.87
KB
-rw-r--r--
pickle.html
102.27
KB
-rw-r--r--
pickletools.html
10.63
KB
-rw-r--r--
pipes.html
18.01
KB
-rw-r--r--
pkgutil.html
25.11
KB
-rw-r--r--
platform.html
28.37
KB
-rw-r--r--
plistlib.html
17.03
KB
-rw-r--r--
popen2.html
25.43
KB
-rw-r--r--
poplib.html
22.32
KB
-rw-r--r--
posix.html
14.41
KB
-rw-r--r--
posixfile.html
19.76
KB
-rw-r--r--
pprint.html
29.92
KB
-rw-r--r--
profile.html
63.56
KB
-rw-r--r--
pty.html
9.48
KB
-rw-r--r--
pwd.html
11.43
KB
-rw-r--r--
py_compile.html
11.12
KB
-rw-r--r--
pyclbr.html
14.71
KB
-rw-r--r--
pydoc.html
11.48
KB
-rw-r--r--
pyexpat.html
71.53
KB
-rw-r--r--
python.html
12.27
KB
-rw-r--r--
queue.html
24.22
KB
-rw-r--r--
quopri.html
11.9
KB
-rw-r--r--
random.html
37.83
KB
-rw-r--r--
re.html
134.74
KB
-rw-r--r--
readline.html
28.24
KB
-rw-r--r--
repr.html
20.43
KB
-rw-r--r--
resource.html
26.48
KB
-rw-r--r--
restricted.html
11.65
KB
-rw-r--r--
rexec.html
37.41
KB
-rw-r--r--
rfc822.html
42.22
KB
-rw-r--r--
rlcompleter.html
13.51
KB
-rw-r--r--
robotparser.html
12.27
KB
-rw-r--r--
runpy.html
19.34
KB
-rw-r--r--
sched.html
18.54
KB
-rw-r--r--
scrolledtext.html
9.32
KB
-rw-r--r--
select.html
39.67
KB
-rw-r--r--
sets.html
36.92
KB
-rw-r--r--
sgi.html
9.71
KB
-rw-r--r--
sgmllib.html
30.77
KB
-rw-r--r--
sha.html
12.09
KB
-rw-r--r--
shelve.html
27.02
KB
-rw-r--r--
shlex.html
32.1
KB
-rw-r--r--
shutil.html
40.22
KB
-rw-r--r--
signal.html
31.14
KB
-rw-r--r--
simplehttpserver.html
18.41
KB
-rw-r--r--
simplexmlrpcserver.html
31.39
KB
-rw-r--r--
site.html
23.64
KB
-rw-r--r--
smtpd.html
12.46
KB
-rw-r--r--
smtplib.html
42.13
KB
-rw-r--r--
sndhdr.html
10.02
KB
-rw-r--r--
socket.html
106.34
KB
-rw-r--r--
socketserver.html
59.83
KB
-rw-r--r--
someos.html
15.11
KB
-rw-r--r--
spwd.html
10.33
KB
-rw-r--r--
sqlite3.html
139.5
KB
-rw-r--r--
ssl.html
65.62
KB
-rw-r--r--
stat.html
32.31
KB
-rw-r--r--
statvfs.html
10.6
KB
-rw-r--r--
stdtypes.html
260.4
KB
-rw-r--r--
string.html
106.65
KB
-rw-r--r--
stringio.html
18.81
KB
-rw-r--r--
stringprep.html
16.13
KB
-rw-r--r--
strings.html
14.93
KB
-rw-r--r--
struct.html
40.88
KB
-rw-r--r--
subprocess.html
84.91
KB
-rw-r--r--
sun.html
6.84
KB
-rw-r--r--
sunau.html
27.1
KB
-rw-r--r--
sunaudio.html
17.79
KB
-rw-r--r--
symbol.html
7.66
KB
-rw-r--r--
symtable.html
22.94
KB
-rw-r--r--
sys.html
98.7
KB
-rw-r--r--
sysconfig.html
23.84
KB
-rw-r--r--
syslog.html
17.92
KB
-rw-r--r--
tabnanny.html
10.63
KB
-rw-r--r--
tarfile.html
78.68
KB
-rw-r--r--
telnetlib.html
25.48
KB
-rw-r--r--
tempfile.html
29.42
KB
-rw-r--r--
termios.html
16.01
KB
-rw-r--r--
test.html
52.62
KB
-rw-r--r--
textwrap.html
27.25
KB
-rw-r--r--
thread.html
20.47
KB
-rw-r--r--
threading.html
76.69
KB
-rw-r--r--
time.html
56.93
KB
-rw-r--r--
timeit.html
36.27
KB
-rw-r--r--
tix.html
46.96
KB
-rw-r--r--
tk.html
23.64
KB
-rw-r--r--
tkinter.html
67.67
KB
-rw-r--r--
token.html
19.62
KB
-rw-r--r--
tokenize.html
18.45
KB
-rw-r--r--
trace.html
25.54
KB
-rw-r--r--
traceback.html
33.44
KB
-rw-r--r--
ttk.html
101.75
KB
-rw-r--r--
tty.html
9.06
KB
-rw-r--r--
turtle.html
211.74
KB
-rw-r--r--
types.html
27.59
KB
-rw-r--r--
undoc.html
23.16
KB
-rw-r--r--
unicodedata.html
18.55
KB
-rw-r--r--
unittest.html
202.85
KB
-rw-r--r--
unix.html
10.55
KB
-rw-r--r--
urllib.html
58.68
KB
-rw-r--r--
urllib2.html
100.58
KB
-rw-r--r--
urlparse.html
40.41
KB
-rw-r--r--
user.html
11.83
KB
-rw-r--r--
userdict.html
29.73
KB
-rw-r--r--
uu.html
11.03
KB
-rw-r--r--
uuid.html
28.19
KB
-rw-r--r--
warnings.html
46.6
KB
-rw-r--r--
wave.html
22.22
KB
-rw-r--r--
weakref.html
36.52
KB
-rw-r--r--
webbrowser.html
23.07
KB
-rw-r--r--
whichdb.html
8.85
KB
-rw-r--r--
windows.html
9.33
KB
-rw-r--r--
winsound.html
18.75
KB
-rw-r--r--
wsgiref.html
81.04
KB
-rw-r--r--
xdrlib.html
29.94
KB
-rw-r--r--
xml.dom.html
89.04
KB
-rw-r--r--
xml.dom.minidom.html
40.42
KB
-rw-r--r--
xml.dom.pulldom.html
12.71
KB
-rw-r--r--
xml.etree.elementtree.html
93.22
KB
-rw-r--r--
xml.html
16.49
KB
-rw-r--r--
xml.sax.handler.html
38.63
KB
-rw-r--r--
xml.sax.html
20.22
KB
-rw-r--r--
xml.sax.reader.html
39.09
KB
-rw-r--r--
xml.sax.utils.html
14.26
KB
-rw-r--r--
xmlrpclib.html
60.79
KB
-rw-r--r--
zipfile.html
53.14
KB
-rw-r--r--
zipimport.html
20.42
KB
-rw-r--r--
zlib.html
25.46
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : optparse.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>15.5. optparse — Parser for command line options — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="15. Generic Operating System Services" href="allos.html" /> <link rel="next" title="15.6. getopt — C-style parser for command line options" href="getopt.html" /> <link rel="prev" title="15.4. argparse — Parser for command-line options, arguments and sub-commands" href="argparse.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="getopt.html" title="15.6. getopt — C-style parser for command line options" accesskey="N">next</a> |</li> <li class="right" > <a href="argparse.html" title="15.4. argparse — Parser for command-line options, arguments and sub-commands" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="allos.html" accesskey="U">15. Generic Operating System Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-optparse"> <span id="optparse-parser-for-command-line-options"></span><h1>15.5. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> — Parser for command line options<a class="headerlink" href="#module-optparse" title="Permalink to this headline">¶</a></h1> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> <p class="deprecated"> <span class="versionmodified">Deprecated since version 2.7: </span>The <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> module is deprecated and will not be developed further; development will continue with the <a class="reference internal" href="argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> module.</p> <p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Lib/optparse.py">Lib/optparse.py</a></p> <hr class="docutils" /> <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> is a more convenient, flexible, and powerful library for parsing command-line options than the old <a class="reference internal" href="getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><tt class="xref py py-mod docutils literal"><span class="pre">getopt</span></tt></a> module. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> uses a more declarative style of command-line parsing: you create an instance of <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a>, populate it with options, and parse the command line. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> allows users to specify options in the conventional GNU/POSIX syntax, and additionally generates usage and help messages for you.</p> <p>Here’s an example of using <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> in a simple script:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">()</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-f"</span><span class="p">,</span> <span class="s">"--file"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"filename"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"write report to FILE"</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">"FILE"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-q"</span><span class="p">,</span> <span class="s">"--quiet"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_false"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"don't print status messages to stdout"</span><span class="p">)</span> <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> </pre></div> </div> <p>With these few lines of code, users of your script can now do the “usual thing” on the command-line, for example:</p> <div class="highlight-python"><pre><yourscript> --file=outfile -q</pre> </div> <p>As it parses the command line, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> sets attributes of the <tt class="docutils literal"><span class="pre">options</span></tt> object returned by <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt> based on user-supplied command-line values. When <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt> returns from parsing this command line, <tt class="docutils literal"><span class="pre">options.filename</span></tt> will be <tt class="docutils literal"><span class="pre">"outfile"</span></tt> and <tt class="docutils literal"><span class="pre">options.verbose</span></tt> will be <tt class="docutils literal"><span class="pre">False</span></tt>. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> supports both long and short options, allows short options to be merged together, and allows options to be associated with their arguments in a variety of ways. Thus, the following command lines are all equivalent to the above example:</p> <div class="highlight-python"><pre><yourscript> -f outfile --quiet <yourscript> --quiet --file outfile <yourscript> -q -foutfile <yourscript> -qfoutfile</pre> </div> <p>Additionally, users can run one of</p> <div class="highlight-python"><pre><yourscript> -h <yourscript> --help</pre> </div> <p>and <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> will print out a brief summary of your script’s options:</p> <div class="highlight-text"><div class="highlight"><pre>Usage: <yourscript> [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 </pre></div> </div> <p>where the value of <em>yourscript</em> is determined at runtime (normally from <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt>).</p> <div class="section" id="background"> <span id="optparse-background"></span><h2>15.5.1. Background<a class="headerlink" href="#background" title="Permalink to this headline">¶</a></h2> <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> was explicitly designed to encourage the creation of programs with straightforward, conventional command-line interfaces. To that end, it supports only the most common command-line syntax and semantics conventionally used under Unix. If you are unfamiliar with these conventions, read this section to acquaint yourself with them.</p> <div class="section" id="terminology"> <span id="optparse-terminology"></span><h3>15.5.1.1. Terminology<a class="headerlink" href="#terminology" title="Permalink to this headline">¶</a></h3> <dl class="docutils"> <dt>argument</dt> <dd><p class="first">a string entered on the command-line, and passed by the shell to <tt class="docutils literal"><span class="pre">execl()</span></tt> or <tt class="docutils literal"><span class="pre">execv()</span></tt>. In Python, arguments are elements of <tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt> (<tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> is the name of the program being executed). Unix shells also use the term “word”.</p> <p class="last">It is occasionally desirable to substitute an argument list other than <tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>, so you should read “argument” as “an element of <tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>, or of some other list provided as a substitute for <tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>”.</p> </dd> <dt>option</dt> <dd><p class="first">an argument used to supply extra information to guide or customize the execution of a program. There are many different syntaxes for options; the traditional Unix syntax is a hyphen (“-”) followed by a single letter, e.g. <tt class="docutils literal"><span class="pre">-x</span></tt> or <tt class="docutils literal"><span class="pre">-F</span></tt>. Also, traditional Unix syntax allows multiple options to be merged into a single argument, e.g. <tt class="docutils literal"><span class="pre">-x</span> <span class="pre">-F</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">-xF</span></tt>. The GNU project introduced <tt class="docutils literal"><span class="pre">--</span></tt> followed by a series of hyphen-separated words, e.g. <tt class="docutils literal"><span class="pre">--file</span></tt> or <tt class="docutils literal"><span class="pre">--dry-run</span></tt>. These are the only two option syntaxes provided by <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>.</p> <p>Some other option syntaxes that the world has seen include:</p> <ul class="simple"> <li>a hyphen followed by a few letters, e.g. <tt class="docutils literal"><span class="pre">-pf</span></tt> (this is <em>not</em> the same as multiple options merged into a single argument)</li> <li>a hyphen followed by a whole word, e.g. <tt class="docutils literal"><span class="pre">-file</span></tt> (this is technically equivalent to the previous syntax, but they aren’t usually seen in the same program)</li> <li>a plus sign followed by a single letter, or a few letters, or a word, e.g. <tt class="docutils literal"><span class="pre">+f</span></tt>, <tt class="docutils literal"><span class="pre">+rgb</span></tt></li> <li>a slash followed by a letter, or a few letters, or a word, e.g. <tt class="docutils literal"><span class="pre">/f</span></tt>, <tt class="docutils literal"><span class="pre">/file</span></tt></li> </ul> <p class="last">These option syntaxes are not supported by <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>, and they never will be. This is deliberate: the first three are non-standard on any environment, and the last only makes sense if you’re exclusively targeting VMS, MS-DOS, and/or Windows.</p> </dd> <dt>option argument</dt> <dd><p class="first">an argument that follows an option, is closely associated with that option, and is consumed from the argument list when that option is. With <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>, option arguments may either be in a separate argument from their option:</p> <div class="highlight-text"><div class="highlight"><pre>-f foo --file foo </pre></div> </div> <p>or included in the same argument:</p> <div class="highlight-text"><div class="highlight"><pre>-ffoo --file=foo </pre></div> </div> <p class="last">Typically, a given option either takes an argument or it doesn’t. Lots of people want an “optional option arguments” feature, meaning that some options will take an argument if they see it, and won’t if they don’t. This is somewhat controversial, because it makes parsing ambiguous: if <tt class="docutils literal"><span class="pre">-a</span></tt> takes an optional argument and <tt class="docutils literal"><span class="pre">-b</span></tt> is another option entirely, how do we interpret <tt class="docutils literal"><span class="pre">-ab</span></tt>? Because of this ambiguity, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> does not support this feature.</p> </dd> <dt>positional argument</dt> <dd>something leftover in the argument list after options have been parsed, i.e. after options and their arguments have been parsed and removed from the argument list.</dd> <dt>required option</dt> <dd>an option that must be supplied on the command-line; note that the phrase “required option” is self-contradictory in English. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> doesn’t prevent you from implementing required options, but doesn’t give you much help at it either.</dd> </dl> <p>For example, consider this hypothetical command-line:</p> <div class="highlight-python"><pre>prog -v --report report.txt foo bar</pre> </div> <p><tt class="docutils literal"><span class="pre">-v</span></tt> and <tt class="docutils literal"><span class="pre">--report</span></tt> are both options. Assuming that <tt class="docutils literal"><span class="pre">--report</span></tt> takes one argument, <tt class="docutils literal"><span class="pre">report.txt</span></tt> is an option argument. <tt class="docutils literal"><span class="pre">foo</span></tt> and <tt class="docutils literal"><span class="pre">bar</span></tt> are positional arguments.</p> </div> <div class="section" id="what-are-options-for"> <span id="optparse-what-options-for"></span><h3>15.5.1.2. What are options for?<a class="headerlink" href="#what-are-options-for" title="Permalink to this headline">¶</a></h3> <p>Options are used to provide extra information to tune or customize the execution of a program. In case it wasn’t clear, options are usually <em>optional</em>. A program should be able to run just fine with no options whatsoever. (Pick a random program from the Unix or GNU toolsets. Can it run without any options at all and still make sense? The main exceptions are <tt class="docutils literal"><span class="pre">find</span></tt>, <tt class="docutils literal"><span class="pre">tar</span></tt>, and <tt class="docutils literal"><span class="pre">dd</span></tt>—all of which are mutant oddballs that have been rightly criticized for their non-standard syntax and confusing interfaces.)</p> <p>Lots of people want their programs to have “required options”. Think about it. If it’s required, then it’s <em>not optional</em>! If there is a piece of information that your program absolutely requires in order to run successfully, that’s what positional arguments are for.</p> <p>As an example of good command-line interface design, consider the humble <tt class="docutils literal"><span class="pre">cp</span></tt> utility, for copying files. It doesn’t make much sense to try to copy files without supplying a destination and at least one source. Hence, <tt class="docutils literal"><span class="pre">cp</span></tt> fails if you run it with no arguments. However, it has a flexible, useful syntax that does not require any options at all:</p> <div class="highlight-python"><pre>cp SOURCE DEST cp SOURCE ... DEST-DIR</pre> </div> <p>You can get pretty far with just that. Most <tt class="docutils literal"><span class="pre">cp</span></tt> implementations provide a bunch of options to tweak exactly how the files are copied: you can preserve mode and modification time, avoid following symlinks, ask before clobbering existing files, etc. But none of this distracts from the core mission of <tt class="docutils literal"><span class="pre">cp</span></tt>, which is to copy either one file to another, or several files to another directory.</p> </div> <div class="section" id="what-are-positional-arguments-for"> <span id="optparse-what-positional-arguments-for"></span><h3>15.5.1.3. What are positional arguments for?<a class="headerlink" href="#what-are-positional-arguments-for" title="Permalink to this headline">¶</a></h3> <p>Positional arguments are for those pieces of information that your program absolutely, positively requires to run.</p> <p>A good user interface should have as few absolute requirements as possible. If your program requires 17 distinct pieces of information in order to run successfully, it doesn’t much matter <em>how</em> you get that information from the user—most people will give up and walk away before they successfully run the program. This applies whether the user interface is a command-line, a configuration file, or a GUI: if you make that many demands on your users, most of them will simply give up.</p> <p>In short, try to minimize the amount of information that users are absolutely required to supply—use sensible defaults whenever possible. Of course, you also want to make your programs reasonably flexible. That’s what options are for. Again, it doesn’t matter if they are entries in a config file, widgets in the “Preferences” dialog of a GUI, or command-line options—the more options you implement, the more flexible your program is, and the more complicated its implementation becomes. Too much flexibility has drawbacks as well, of course; too many options can overwhelm users and make your code much harder to maintain.</p> </div> </div> <div class="section" id="tutorial"> <span id="optparse-tutorial"></span><h2>15.5.2. Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h2> <p>While <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> is quite flexible and powerful, it’s also straightforward to use in most cases. This section covers the code patterns that are common to any <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>-based program.</p> <p>First, you need to import the OptionParser class; then, early in the main program, create an OptionParser instance:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">()</span> </pre></div> </div> <p>Then you can start defining options. The basic syntax is:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="n">opt_str</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="n">value</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span> </pre></div> </div> <p>Each option has one or more option strings, such as <tt class="docutils literal"><span class="pre">-f</span></tt> or <tt class="docutils literal"><span class="pre">--file</span></tt>, and several option attributes that tell <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> what to expect and what to do when it encounters that option on the command line.</p> <p>Typically, each option will have one short option string and one long option string, e.g.:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-f"</span><span class="p">,</span> <span class="s">"--file"</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span> </pre></div> </div> <p>You’re free to define as many short option strings and as many long option strings as you like (including zero), as long as there is at least one option string overall.</p> <p>The option strings passed to <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.add_option()</span></tt></a> are effectively labels for the option defined by that call. For brevity, we will frequently refer to <em>encountering an option</em> on the command line; in reality, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> encounters <em>option strings</em> and looks up options from them.</p> <p>Once all of your options are defined, instruct <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> to parse your program’s command line:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> </pre></div> </div> <p>(If you like, you can pass a custom argument list to <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt>, but that’s rarely necessary: by default it uses <tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>.)</p> <p><tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt> returns two values:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">options</span></tt>, an object containing values for all of your options—e.g. if <tt class="docutils literal"><span class="pre">--file</span></tt> takes a single string argument, then <tt class="docutils literal"><span class="pre">options.file</span></tt> will be the filename supplied by the user, or <tt class="docutils literal"><span class="pre">None</span></tt> if the user did not supply that option</li> <li><tt class="docutils literal"><span class="pre">args</span></tt>, the list of positional arguments leftover after parsing options</li> </ul> <p>This tutorial section only covers the four most important option attributes: <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a>, <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> (destination), and <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a>. Of these, <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a> is the most fundamental.</p> <div class="section" id="understanding-option-actions"> <span id="optparse-understanding-option-actions"></span><h3>15.5.2.1. Understanding option actions<a class="headerlink" href="#understanding-option-actions" title="Permalink to this headline">¶</a></h3> <p>Actions tell <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> what to do when it encounters an option on the command line. There is a fixed set of actions hard-coded into <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>; adding new actions is an advanced topic covered in section <a class="reference internal" href="#optparse-extending-optparse"><em>Extending optparse</em></a>. Most actions tell <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> to store a value in some variable—for example, take a string from the command line and store it in an attribute of <tt class="docutils literal"><span class="pre">options</span></tt>.</p> <p>If you don’t specify an option action, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> defaults to <tt class="docutils literal"><span class="pre">store</span></tt>.</p> </div> <div class="section" id="the-store-action"> <span id="optparse-store-action"></span><h3>15.5.2.2. The store action<a class="headerlink" href="#the-store-action" title="Permalink to this headline">¶</a></h3> <p>The most common option action is <tt class="docutils literal"><span class="pre">store</span></tt>, which tells <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> to take the next argument (or the remainder of the current argument), ensure that it is of the correct type, and store it to your chosen destination.</p> <p>For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-f"</span><span class="p">,</span> <span class="s">"--file"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">"string"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"filename"</span><span class="p">)</span> </pre></div> </div> <p>Now let’s make up a fake command line and ask <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> to parse it:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="s">"-f"</span><span class="p">,</span> <span class="s">"foo.txt"</span><span class="p">]</span> <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> </pre></div> </div> <p>When <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> sees the option string <tt class="docutils literal"><span class="pre">-f</span></tt>, it consumes the next argument, <tt class="docutils literal"><span class="pre">foo.txt</span></tt>, and stores it in <tt class="docutils literal"><span class="pre">options.filename</span></tt>. So, after this call to <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt>, <tt class="docutils literal"><span class="pre">options.filename</span></tt> is <tt class="docutils literal"><span class="pre">"foo.txt"</span></tt>.</p> <p>Some other option types supported by <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> are <tt class="docutils literal"><span class="pre">int</span></tt> and <tt class="docutils literal"><span class="pre">float</span></tt>. Here’s an option that expects an integer argument:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-n"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">"int"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"num"</span><span class="p">)</span> </pre></div> </div> <p>Note that this option has no long option string, which is perfectly acceptable. Also, there’s no explicit action, since the default is <tt class="docutils literal"><span class="pre">store</span></tt>.</p> <p>Let’s parse another fake command-line. This time, we’ll jam the option argument right up against the option: since <tt class="docutils literal"><span class="pre">-n42</span></tt> (one argument) is equivalent to <tt class="docutils literal"><span class="pre">-n</span> <span class="pre">42</span></tt> (two arguments), the code</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">"-n42"</span><span class="p">])</span> <span class="k">print</span> <span class="n">options</span><span class="o">.</span><span class="n">num</span> </pre></div> </div> <p>will print <tt class="docutils literal"><span class="pre">42</span></tt>.</p> <p>If you don’t specify a type, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> assumes <tt class="docutils literal"><span class="pre">string</span></tt>. Combined with the fact that the default action is <tt class="docutils literal"><span class="pre">store</span></tt>, that means our first example can be a lot shorter:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-f"</span><span class="p">,</span> <span class="s">"--file"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"filename"</span><span class="p">)</span> </pre></div> </div> <p>If you don’t supply a destination, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> figures out a sensible default from the option strings: if the first long option string is <tt class="docutils literal"><span class="pre">--foo-bar</span></tt>, then the default destination is <tt class="docutils literal"><span class="pre">foo_bar</span></tt>. If there are no long option strings, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> looks at the first short option string: the default destination for <tt class="docutils literal"><span class="pre">-f</span></tt> is <tt class="docutils literal"><span class="pre">f</span></tt>.</p> <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> also includes built-in <tt class="docutils literal"><span class="pre">long</span></tt> and <tt class="docutils literal"><span class="pre">complex</span></tt> types. Adding types is covered in section <a class="reference internal" href="#optparse-extending-optparse"><em>Extending optparse</em></a>.</p> </div> <div class="section" id="handling-boolean-flag-options"> <span id="optparse-handling-boolean-options"></span><h3>15.5.2.3. Handling boolean (flag) options<a class="headerlink" href="#handling-boolean-flag-options" title="Permalink to this headline">¶</a></h3> <p>Flag options—set a variable to true or false when a particular option is seen —are quite common. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> supports them with two separate actions, <tt class="docutils literal"><span class="pre">store_true</span></tt> and <tt class="docutils literal"><span class="pre">store_false</span></tt>. For example, you might have a <tt class="docutils literal"><span class="pre">verbose</span></tt> flag that is turned on with <tt class="docutils literal"><span class="pre">-v</span></tt> and off with <tt class="docutils literal"><span class="pre">-q</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-v"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-q"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_false"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">)</span> </pre></div> </div> <p>Here we have two different options with the same destination, which is perfectly OK. (It just means you have to be a bit careful when setting default values— see below.)</p> <p>When <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> encounters <tt class="docutils literal"><span class="pre">-v</span></tt> on the command line, it sets <tt class="docutils literal"><span class="pre">options.verbose</span></tt> to <tt class="docutils literal"><span class="pre">True</span></tt>; when it encounters <tt class="docutils literal"><span class="pre">-q</span></tt>, <tt class="docutils literal"><span class="pre">options.verbose</span></tt> is set to <tt class="docutils literal"><span class="pre">False</span></tt>.</p> </div> <div class="section" id="other-actions"> <span id="optparse-other-actions"></span><h3>15.5.2.4. Other actions<a class="headerlink" href="#other-actions" title="Permalink to this headline">¶</a></h3> <p>Some other actions supported by <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> are:</p> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">"store_const"</span></tt></dt> <dd>store a constant value</dd> <dt><tt class="docutils literal"><span class="pre">"append"</span></tt></dt> <dd>append this option’s argument to a list</dd> <dt><tt class="docutils literal"><span class="pre">"count"</span></tt></dt> <dd>increment a counter by one</dd> <dt><tt class="docutils literal"><span class="pre">"callback"</span></tt></dt> <dd>call a specified function</dd> </dl> <p>These are covered in section <a class="reference internal" href="#optparse-reference-guide"><em>Reference Guide</em></a>, Reference Guide and section <a class="reference internal" href="#optparse-option-callbacks"><em>Option Callbacks</em></a>.</p> </div> <div class="section" id="default-values"> <span id="optparse-default-values"></span><h3>15.5.2.5. Default values<a class="headerlink" href="#default-values" title="Permalink to this headline">¶</a></h3> <p>All of the above examples involve setting some variable (the “destination”) when certain command-line options are seen. What happens if those options are never seen? Since we didn’t supply any defaults, they are all set to <tt class="docutils literal"><span class="pre">None</span></tt>. This is usually fine, but sometimes you want more control. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> lets you supply a default value for each destination, which is assigned before the command line is parsed.</p> <p>First, consider the verbose/quiet example. If we want <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> to set <tt class="docutils literal"><span class="pre">verbose</span></tt> to <tt class="docutils literal"><span class="pre">True</span></tt> unless <tt class="docutils literal"><span class="pre">-q</span></tt> is seen, then we can do this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-v"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-q"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_false"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">)</span> </pre></div> </div> <p>Since default values apply to the <em>destination</em> rather than to any particular option, and these two options happen to have the same destination, this is exactly equivalent:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-v"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-q"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_false"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> </pre></div> </div> <p>Consider this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-v"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-q"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_false"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> </pre></div> </div> <p>Again, the default value for <tt class="docutils literal"><span class="pre">verbose</span></tt> will be <tt class="docutils literal"><span class="pre">True</span></tt>: the last default value supplied for any particular destination is the one that counts.</p> <p>A clearer way to specify default values is the <tt class="xref py py-meth docutils literal"><span class="pre">set_defaults()</span></tt> method of OptionParser, which you can call at any time before calling <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">set_defaults</span><span class="p">(</span><span class="n">verbose</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="o">...</span><span class="p">)</span> <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> </pre></div> </div> <p>As before, the last value specified for a given option destination is the one that counts. For clarity, try to use one method or the other of setting default values, not both.</p> </div> <div class="section" id="generating-help"> <span id="optparse-generating-help"></span><h3>15.5.2.6. Generating help<a class="headerlink" href="#generating-help" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>‘s ability to generate help and usage text automatically is useful for creating user-friendly command-line interfaces. All you have to do is supply a <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> value for each option, and optionally a short usage message for your whole program. Here’s an OptionParser populated with user-friendly (documented) options:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">usage</span> <span class="o">=</span> <span class="s">"usage: %prog [options] arg1 arg2"</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">usage</span><span class="o">=</span><span class="n">usage</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-v"</span><span class="p">,</span> <span class="s">"--verbose"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"make lots of noise [default]"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-q"</span><span class="p">,</span> <span class="s">"--quiet"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_false"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"be vewwy quiet (I'm hunting wabbits)"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-f"</span><span class="p">,</span> <span class="s">"--filename"</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">"FILE"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"write output to FILE"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-m"</span><span class="p">,</span> <span class="s">"--mode"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s">"intermediate"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"interaction mode: novice, intermediate, "</span> <span class="s">"or expert [default: </span><span class="si">%d</span><span class="s">efault]"</span><span class="p">)</span> </pre></div> </div> <p>If <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> encounters either <tt class="docutils literal"><span class="pre">-h</span></tt> or <tt class="docutils literal"><span class="pre">--help</span></tt> on the command-line, or if you just call <tt class="xref py py-meth docutils literal"><span class="pre">parser.print_help()</span></tt>, it prints the following to standard output:</p> <div class="highlight-text"><div class="highlight"><pre>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] </pre></div> </div> <p>(If the help output is triggered by a help option, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> exits after printing the help text.)</p> <p>There’s a lot going on here to help <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> generate the best possible help message:</p> <ul> <li><p class="first">the script defines its own usage message:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">usage</span> <span class="o">=</span> <span class="s">"usage: %prog [options] arg1 arg2"</span> </pre></div> </div> <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> expands <tt class="docutils literal"><span class="pre">%prog</span></tt> in the usage string to the name of the current program, i.e. <tt class="docutils literal"><span class="pre">os.path.basename(sys.argv[0])</span></tt>. The expanded string is then printed before the detailed option help.</p> <p>If you don’t supply a usage string, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> uses a bland but sensible default: <tt class="docutils literal"><span class="pre">"Usage:</span> <span class="pre">%prog</span> <span class="pre">[options]"</span></tt>, which is fine if your script doesn’t take any positional arguments.</p> </li> <li><p class="first">every option defines a help string, and doesn’t worry about line-wrapping— <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> takes care of wrapping lines and making the help output look good.</p> </li> <li><p class="first">options that take a value indicate this fact in their automatically-generated help message, e.g. for the “mode” option:</p> <div class="highlight-python"><pre>-m MODE, --mode=MODE</pre> </div> <p>Here, “MODE” is called the meta-variable: it stands for the argument that the user is expected to supply to <tt class="docutils literal"><span class="pre">-m</span></tt>/<tt class="docutils literal"><span class="pre">--mode</span></tt>. By default, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> converts the destination variable name to uppercase and uses that for the meta-variable. Sometimes, that’s not what you want—for example, the <tt class="docutils literal"><span class="pre">--filename</span></tt> option explicitly sets <tt class="docutils literal"><span class="pre">metavar="FILE"</span></tt>, resulting in this automatically-generated option description:</p> <div class="highlight-python"><pre>-f FILE, --filename=FILE</pre> </div> <p>This is important for more than just saving space, though: the manually written help text uses the meta-variable <tt class="docutils literal"><span class="pre">FILE</span></tt> to clue the user in that there’s a connection between the semi-formal syntax <tt class="docutils literal"><span class="pre">-f</span> <span class="pre">FILE</span></tt> and the informal semantic description “write output to FILE”. This is a simple but effective way to make your help text a lot clearer and more useful for end users.</p> </li> </ul> <p class="versionadded"> <span class="versionmodified">New in version 2.4: </span>Options that have a default value can include <tt class="docutils literal"><span class="pre">%default</span></tt> in the help string—<a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> will replace it with <a class="reference internal" href="functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> of the option’s default value. If an option has no default value (or the default value is <tt class="docutils literal"><span class="pre">None</span></tt>), <tt class="docutils literal"><span class="pre">%default</span></tt> expands to <tt class="docutils literal"><span class="pre">none</span></tt>.</p> <div class="section" id="grouping-options"> <h4>15.5.2.6.1. Grouping Options<a class="headerlink" href="#grouping-options" title="Permalink to this headline">¶</a></h4> <p>When dealing with many options, it is convenient to group these options for better help output. An <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a> can contain several option groups, each of which can contain several options.</p> <p>An option group is obtained using the class <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><tt class="xref py py-class docutils literal"><span class="pre">OptionGroup</span></tt></a>:</p> <dl class="class"> <dt id="optparse.OptionGroup"> <em class="property">class </em><tt class="descclassname">optparse.</tt><tt class="descname">OptionGroup</tt><big>(</big><em>parser</em>, <em>title</em>, <em>description=None</em><big>)</big><a class="headerlink" href="#optparse.OptionGroup" title="Permalink to this definition">¶</a></dt> <dd><p>where</p> <ul class="simple"> <li>parser is the <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a> instance the group will be insterted in to</li> <li>title is the group title</li> <li>description, optional, is a long description of the group</li> </ul> </dd></dl> <p><a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><tt class="xref py py-class docutils literal"><span class="pre">OptionGroup</span></tt></a> inherits from <tt class="xref py py-class docutils literal"><span class="pre">OptionContainer</span></tt> (like <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a>) and so the <tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt> method can be used to add an option to the group.</p> <p>Once all the options are declared, using the <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a> method <tt class="xref py py-meth docutils literal"><span class="pre">add_option_group()</span></tt> the group is added to the previously defined parser.</p> <p>Continuing with the parser defined in the previous section, adding an <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><tt class="xref py py-class docutils literal"><span class="pre">OptionGroup</span></tt></a> to a parser is easy:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">group</span> <span class="o">=</span> <span class="n">OptionGroup</span><span class="p">(</span><span class="n">parser</span><span class="p">,</span> <span class="s">"Dangerous Options"</span><span class="p">,</span> <span class="s">"Caution: use these options at your own risk. "</span> <span class="s">"It is believed that some of them bite."</span><span class="p">)</span> <span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-g"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"Group option."</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option_group</span><span class="p">(</span><span class="n">group</span><span class="p">)</span> </pre></div> </div> <p>This would result in the following help output:</p> <div class="highlight-text"><div class="highlight"><pre>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. </pre></div> </div> <p>A bit more complete example might involve using more than one group: still extending the previous example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">group</span> <span class="o">=</span> <span class="n">OptionGroup</span><span class="p">(</span><span class="n">parser</span><span class="p">,</span> <span class="s">"Dangerous Options"</span><span class="p">,</span> <span class="s">"Caution: use these options at your own risk. "</span> <span class="s">"It is believed that some of them bite."</span><span class="p">)</span> <span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-g"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"Group option."</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option_group</span><span class="p">(</span><span class="n">group</span><span class="p">)</span> <span class="n">group</span> <span class="o">=</span> <span class="n">OptionGroup</span><span class="p">(</span><span class="n">parser</span><span class="p">,</span> <span class="s">"Debug Options"</span><span class="p">)</span> <span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-d"</span><span class="p">,</span> <span class="s">"--debug"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"Print debug information"</span><span class="p">)</span> <span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-s"</span><span class="p">,</span> <span class="s">"--sql"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"Print all SQL statements executed"</span><span class="p">)</span> <span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-e"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"Print every action done"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option_group</span><span class="p">(</span><span class="n">group</span><span class="p">)</span> </pre></div> </div> <p>that results in the following output:</p> <div class="highlight-text"><div class="highlight"><pre>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. Debug Options: -d, --debug Print debug information -s, --sql Print all SQL statements executed -e Print every action done </pre></div> </div> <p>Another interesting method, in particular when working programmatically with option groups is:</p> <dl class="method"> <dt id="optparse.OptionParser.get_option_group"> <tt class="descclassname">OptionParser.</tt><tt class="descname">get_option_group</tt><big>(</big><em>opt_str</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.get_option_group" title="Permalink to this definition">¶</a></dt> <dd><p>Return the <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><tt class="xref py py-class docutils literal"><span class="pre">OptionGroup</span></tt></a> to which the short or long option string <em>opt_str</em> (e.g. <tt class="docutils literal"><span class="pre">'-o'</span></tt> or <tt class="docutils literal"><span class="pre">'--option'</span></tt>) belongs. If there’s no such <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><tt class="xref py py-class docutils literal"><span class="pre">OptionGroup</span></tt></a>, return <tt class="docutils literal"><span class="pre">None</span></tt>.</p> </dd></dl> </div> </div> <div class="section" id="printing-a-version-string"> <span id="optparse-printing-version-string"></span><h3>15.5.2.7. Printing a version string<a class="headerlink" href="#printing-a-version-string" title="Permalink to this headline">¶</a></h3> <p>Similar to the brief usage string, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> can also print a version string for your program. You have to supply the string as the <tt class="docutils literal"><span class="pre">version</span></tt> argument to OptionParser:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">usage</span><span class="o">=</span><span class="s">"%prog [-f] [-q]"</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="s">"%prog 1.0"</span><span class="p">)</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">%prog</span></tt> is expanded just like it is in <tt class="docutils literal"><span class="pre">usage</span></tt>. Apart from that, <tt class="docutils literal"><span class="pre">version</span></tt> can contain anything you like. When you supply it, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> automatically adds a <tt class="docutils literal"><span class="pre">--version</span></tt> option to your parser. If it encounters this option on the command line, it expands your <tt class="docutils literal"><span class="pre">version</span></tt> string (by replacing <tt class="docutils literal"><span class="pre">%prog</span></tt>), prints it to stdout, and exits.</p> <p>For example, if your script is called <tt class="docutils literal"><span class="pre">/usr/bin/foo</span></tt>:</p> <div class="highlight-python"><pre>$ /usr/bin/foo --version foo 1.0</pre> </div> <p>The following two methods can be used to print and get the <tt class="docutils literal"><span class="pre">version</span></tt> string:</p> <dl class="method"> <dt id="optparse.OptionParser.print_version"> <tt class="descclassname">OptionParser.</tt><tt class="descname">print_version</tt><big>(</big><em>file=None</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.print_version" title="Permalink to this definition">¶</a></dt> <dd><p>Print the version message for the current program (<tt class="docutils literal"><span class="pre">self.version</span></tt>) to <em>file</em> (default stdout). As with <a class="reference internal" href="#optparse.OptionParser.print_usage" title="optparse.OptionParser.print_usage"><tt class="xref py py-meth docutils literal"><span class="pre">print_usage()</span></tt></a>, any occurrence of <tt class="docutils literal"><span class="pre">%prog</span></tt> in <tt class="docutils literal"><span class="pre">self.version</span></tt> is replaced with the name of the current program. Does nothing if <tt class="docutils literal"><span class="pre">self.version</span></tt> is empty or undefined.</p> </dd></dl> <dl class="method"> <dt id="optparse.OptionParser.get_version"> <tt class="descclassname">OptionParser.</tt><tt class="descname">get_version</tt><big>(</big><big>)</big><a class="headerlink" href="#optparse.OptionParser.get_version" title="Permalink to this definition">¶</a></dt> <dd><p>Same as <a class="reference internal" href="#optparse.OptionParser.print_version" title="optparse.OptionParser.print_version"><tt class="xref py py-meth docutils literal"><span class="pre">print_version()</span></tt></a> but returns the version string instead of printing it.</p> </dd></dl> </div> <div class="section" id="how-optparse-handles-errors"> <span id="optparse-how-optparse-handles-errors"></span><h3>15.5.2.8. How <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> handles errors<a class="headerlink" href="#how-optparse-handles-errors" title="Permalink to this headline">¶</a></h3> <p>There are two broad classes of errors that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> has to worry about: programmer errors and user errors. Programmer errors are usually erroneous calls to <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-func docutils literal"><span class="pre">OptionParser.add_option()</span></tt></a>, e.g. invalid option strings, unknown option attributes, missing option attributes, etc. These are dealt with in the usual way: raise an exception (either <tt class="xref py py-exc docutils literal"><span class="pre">optparse.OptionError</span></tt> or <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>) and let the program crash.</p> <p>Handling user errors is much more important, since they are guaranteed to happen no matter how stable your code is. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> can automatically detect some user errors, such as bad option arguments (passing <tt class="docutils literal"><span class="pre">-n</span> <span class="pre">4x</span></tt> where <tt class="docutils literal"><span class="pre">-n</span></tt> takes an integer argument), missing arguments (<tt class="docutils literal"><span class="pre">-n</span></tt> at the end of the command line, where <tt class="docutils literal"><span class="pre">-n</span></tt> takes an argument of any type). Also, you can call <tt class="xref py py-func docutils literal"><span class="pre">OptionParser.error()</span></tt> to signal an application-defined error condition:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">a</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">b</span><span class="p">:</span> <span class="n">parser</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">"options -a and -b are mutually exclusive"</span><span class="p">)</span> </pre></div> </div> <p>In either case, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> handles the error the same way: it prints the program’s usage message and an error message to standard error and exits with error status 2.</p> <p>Consider the first example above, where the user passes <tt class="docutils literal"><span class="pre">4x</span></tt> to an option that takes an integer:</p> <div class="highlight-python"><pre>$ /usr/bin/foo -n 4x Usage: foo [options] foo: error: option -n: invalid integer value: '4x'</pre> </div> <p>Or, where the user fails to pass a value at all:</p> <div class="highlight-python"><pre>$ /usr/bin/foo -n Usage: foo [options] foo: error: -n option requires an argument</pre> </div> <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>-generated error messages take care always to mention the option involved in the error; be sure to do the same when calling <tt class="xref py py-func docutils literal"><span class="pre">OptionParser.error()</span></tt> from your application code.</p> <p>If <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>‘s default error-handling behaviour does not suit your needs, you’ll need to subclass OptionParser and override its <tt class="xref py py-meth docutils literal"><span class="pre">exit()</span></tt> and/or <tt class="xref py py-meth docutils literal"><span class="pre">error()</span></tt> methods.</p> </div> <div class="section" id="putting-it-all-together"> <span id="optparse-putting-it-all-together"></span><h3>15.5.2.9. Putting it all together<a class="headerlink" href="#putting-it-all-together" title="Permalink to this headline">¶</a></h3> <p>Here’s what <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>-based scripts usually look like:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span> <span class="n">usage</span> <span class="o">=</span> <span class="s">"usage: %prog [options] arg"</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">usage</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-f"</span><span class="p">,</span> <span class="s">"--file"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"filename"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"read data from FILENAME"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-v"</span><span class="p">,</span> <span class="s">"--verbose"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-q"</span><span class="p">,</span> <span class="s">"--quiet"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_false"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">)</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span> <span class="n">parser</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">"incorrect number of arguments"</span><span class="p">)</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">verbose</span><span class="p">:</span> <span class="k">print</span> <span class="s">"reading </span><span class="si">%s</span><span class="s">..."</span> <span class="o">%</span> <span class="n">options</span><span class="o">.</span><span class="n">filename</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span> <span class="n">main</span><span class="p">()</span> </pre></div> </div> </div> </div> <div class="section" id="reference-guide"> <span id="optparse-reference-guide"></span><h2>15.5.3. Reference Guide<a class="headerlink" href="#reference-guide" title="Permalink to this headline">¶</a></h2> <div class="section" id="creating-the-parser"> <span id="optparse-creating-parser"></span><h3>15.5.3.1. Creating the parser<a class="headerlink" href="#creating-the-parser" title="Permalink to this headline">¶</a></h3> <p>The first step in using <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> is to create an OptionParser instance.</p> <dl class="class"> <dt id="optparse.OptionParser"> <em class="property">class </em><tt class="descclassname">optparse.</tt><tt class="descname">OptionParser</tt><big>(</big><em>...</em><big>)</big><a class="headerlink" href="#optparse.OptionParser" title="Permalink to this definition">¶</a></dt> <dd><p>The OptionParser constructor has no required arguments, but a number of optional keyword arguments. You should always pass them as keyword arguments, i.e. do not rely on the order in which the arguments are declared.</p> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">usage</span></tt> (default: <tt class="docutils literal"><span class="pre">"%prog</span> <span class="pre">[options]"</span></tt>)</dt> <dd>The usage summary to print when your program is run incorrectly or with a help option. When <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> prints the usage string, it expands <tt class="docutils literal"><span class="pre">%prog</span></tt> to <tt class="docutils literal"><span class="pre">os.path.basename(sys.argv[0])</span></tt> (or to <tt class="docutils literal"><span class="pre">prog</span></tt> if you passed that keyword argument). To suppress a usage message, pass the special value <tt class="xref py py-data docutils literal"><span class="pre">optparse.SUPPRESS_USAGE</span></tt>.</dd> <dt><tt class="docutils literal"><span class="pre">option_list</span></tt> (default: <tt class="docutils literal"><span class="pre">[]</span></tt>)</dt> <dd>A list of Option objects to populate the parser with. The options in <tt class="docutils literal"><span class="pre">option_list</span></tt> are added after any options in <tt class="docutils literal"><span class="pre">standard_option_list</span></tt> (a class attribute that may be set by OptionParser subclasses), but before any version or help options. Deprecated; use <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt></a> after creating the parser instead.</dd> <dt><tt class="docutils literal"><span class="pre">option_class</span></tt> (default: optparse.Option)</dt> <dd>Class to use when adding options to the parser in <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt></a>.</dd> <dt><tt class="docutils literal"><span class="pre">version</span></tt> (default: <tt class="docutils literal"><span class="pre">None</span></tt>)</dt> <dd>A version string to print when the user supplies a version option. If you supply a true value for <tt class="docutils literal"><span class="pre">version</span></tt>, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> automatically adds a version option with the single option string <tt class="docutils literal"><span class="pre">--version</span></tt>. The substring <tt class="docutils literal"><span class="pre">%prog</span></tt> is expanded the same as for <tt class="docutils literal"><span class="pre">usage</span></tt>.</dd> <dt><tt class="docutils literal"><span class="pre">conflict_handler</span></tt> (default: <tt class="docutils literal"><span class="pre">"error"</span></tt>)</dt> <dd>Specifies what to do when options with conflicting option strings are added to the parser; see section <a class="reference internal" href="#optparse-conflicts-between-options"><em>Conflicts between options</em></a>.</dd> <dt><tt class="docutils literal"><span class="pre">description</span></tt> (default: <tt class="docutils literal"><span class="pre">None</span></tt>)</dt> <dd>A paragraph of text giving a brief overview of your program. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> reformats this paragraph to fit the current terminal width and prints it when the user requests help (after <tt class="docutils literal"><span class="pre">usage</span></tt>, but before the list of options).</dd> <dt><tt class="docutils literal"><span class="pre">formatter</span></tt> (default: a new <tt class="xref py py-class docutils literal"><span class="pre">IndentedHelpFormatter</span></tt>)</dt> <dd>An instance of optparse.HelpFormatter that will be used for printing help text. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> provides two concrete classes for this purpose: IndentedHelpFormatter and TitledHelpFormatter.</dd> <dt><tt class="docutils literal"><span class="pre">add_help_option</span></tt> (default: <tt class="docutils literal"><span class="pre">True</span></tt>)</dt> <dd>If true, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> will add a help option (with option strings <tt class="docutils literal"><span class="pre">-h</span></tt> and <tt class="docutils literal"><span class="pre">--help</span></tt>) to the parser.</dd> <dt><tt class="docutils literal"><span class="pre">prog</span></tt></dt> <dd>The string to use when expanding <tt class="docutils literal"><span class="pre">%prog</span></tt> in <tt class="docutils literal"><span class="pre">usage</span></tt> and <tt class="docutils literal"><span class="pre">version</span></tt> instead of <tt class="docutils literal"><span class="pre">os.path.basename(sys.argv[0])</span></tt>.</dd> <dt><tt class="docutils literal"><span class="pre">epilog</span></tt> (default: <tt class="docutils literal"><span class="pre">None</span></tt>)</dt> <dd>A paragraph of help text to print after the option help.</dd> </dl> </dd></dl> </div> <div class="section" id="populating-the-parser"> <span id="optparse-populating-parser"></span><h3>15.5.3.2. Populating the parser<a class="headerlink" href="#populating-the-parser" title="Permalink to this headline">¶</a></h3> <p>There are several ways to populate the parser with options. The preferred way is by using <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.add_option()</span></tt></a>, as shown in section <a class="reference internal" href="#optparse-tutorial"><em>Tutorial</em></a>. <tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt> can be called in one of two ways:</p> <ul class="simple"> <li>pass it an Option instance (as returned by <tt class="xref py py-func docutils literal"><span class="pre">make_option()</span></tt>)</li> <li>pass it any combination of positional and keyword arguments that are acceptable to <tt class="xref py py-func docutils literal"><span class="pre">make_option()</span></tt> (i.e., to the Option constructor), and it will create the Option instance for you</li> </ul> <p>The other alternative is to pass a list of pre-constructed Option instances to the OptionParser constructor, as in:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">option_list</span> <span class="o">=</span> <span class="p">[</span> <span class="n">make_option</span><span class="p">(</span><span class="s">"-f"</span><span class="p">,</span> <span class="s">"--filename"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">"string"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"filename"</span><span class="p">),</span> <span class="n">make_option</span><span class="p">(</span><span class="s">"-q"</span><span class="p">,</span> <span class="s">"--quiet"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_false"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">),</span> <span class="p">]</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">option_list</span><span class="o">=</span><span class="n">option_list</span><span class="p">)</span> </pre></div> </div> <p>(<tt class="xref py py-func docutils literal"><span class="pre">make_option()</span></tt> is a factory function for creating Option instances; currently it is an alias for the Option constructor. A future version of <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> may split Option into several classes, and <tt class="xref py py-func docutils literal"><span class="pre">make_option()</span></tt> will pick the right class to instantiate. Do not instantiate Option directly.)</p> </div> <div class="section" id="defining-options"> <span id="optparse-defining-options"></span><h3>15.5.3.3. Defining options<a class="headerlink" href="#defining-options" title="Permalink to this headline">¶</a></h3> <p>Each Option instance represents a set of synonymous command-line option strings, e.g. <tt class="docutils literal"><span class="pre">-f</span></tt> and <tt class="docutils literal"><span class="pre">--file</span></tt>. You can specify any number of short or long option strings, but you must specify at least one overall option string.</p> <p>The canonical way to create an <tt class="xref py py-class docutils literal"><span class="pre">Option</span></tt> instance is with the <tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt> method of <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a>.</p> <dl class="method"> <dt id="optparse.OptionParser.add_option"> <tt class="descclassname">OptionParser.</tt><tt class="descname">add_option</tt><big>(</big><em>option</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.add_option" title="Permalink to this definition">¶</a></dt> <dt> <tt class="descclassname">OptionParser.</tt><tt class="descname">add_option</tt><big>(</big><em>*opt_str</em>, <em>attr=value</em>, <em>...</em><big>)</big></dt> <dd><p>To define an option with only a short option string:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-f"</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="n">value</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span> </pre></div> </div> <p>And to define an option with only a long option string:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--foo"</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="n">value</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span> </pre></div> </div> <p>The keyword arguments define attributes of the new Option object. The most important option attribute is <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a>, and it largely determines which other attributes are relevant or required. If you pass irrelevant option attributes, or fail to pass required ones, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> raises an <tt class="xref py py-exc docutils literal"><span class="pre">OptionError</span></tt> exception explaining your mistake.</p> <p>An option’s <em>action</em> determines what <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> does when it encounters this option on the command-line. The standard option actions hard-coded into <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> are:</p> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">"store"</span></tt></dt> <dd>store this option’s argument (default)</dd> <dt><tt class="docutils literal"><span class="pre">"store_const"</span></tt></dt> <dd>store a constant value</dd> <dt><tt class="docutils literal"><span class="pre">"store_true"</span></tt></dt> <dd>store a true value</dd> <dt><tt class="docutils literal"><span class="pre">"store_false"</span></tt></dt> <dd>store a false value</dd> <dt><tt class="docutils literal"><span class="pre">"append"</span></tt></dt> <dd>append this option’s argument to a list</dd> <dt><tt class="docutils literal"><span class="pre">"append_const"</span></tt></dt> <dd>append a constant value to a list</dd> <dt><tt class="docutils literal"><span class="pre">"count"</span></tt></dt> <dd>increment a counter by one</dd> <dt><tt class="docutils literal"><span class="pre">"callback"</span></tt></dt> <dd>call a specified function</dd> <dt><tt class="docutils literal"><span class="pre">"help"</span></tt></dt> <dd>print a usage message including all options and the documentation for them</dd> </dl> <p>(If you don’t supply an action, the default is <tt class="docutils literal"><span class="pre">"store"</span></tt>. For this action, you may also supply <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> and <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> option attributes; see <a class="reference internal" href="#optparse-standard-option-actions"><em>Standard option actions</em></a>.)</p> </dd></dl> <p>As you can see, most actions involve storing or updating a value somewhere. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> always creates a special object for this, conventionally called <tt class="docutils literal"><span class="pre">options</span></tt> (it happens to be an instance of <tt class="xref py py-class docutils literal"><span class="pre">optparse.Values</span></tt>). Option arguments (and various other values) are stored as attributes of this object, according to the <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> (destination) option attribute.</p> <p>For example, when you call</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> </pre></div> </div> <p>one of the first things <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> does is create the <tt class="docutils literal"><span class="pre">options</span></tt> object:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">options</span> <span class="o">=</span> <span class="n">Values</span><span class="p">()</span> </pre></div> </div> <p>If one of the options in this parser is defined with</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-f"</span><span class="p">,</span> <span class="s">"--file"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">"string"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"filename"</span><span class="p">)</span> </pre></div> </div> <p>and the command-line being parsed includes any of the following:</p> <div class="highlight-python"><pre>-ffoo -f foo --file=foo --file foo</pre> </div> <p>then <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>, on seeing this option, will do the equivalent of</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="s">"foo"</span> </pre></div> </div> <p>The <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> and <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> option attributes are almost as important as <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a>, but <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a> is the only one that makes sense for <em>all</em> options.</p> </div> <div class="section" id="option-attributes"> <span id="optparse-option-attributes"></span><h3>15.5.3.4. Option attributes<a class="headerlink" href="#option-attributes" title="Permalink to this headline">¶</a></h3> <p>The following option attributes may be passed as keyword arguments to <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.add_option()</span></tt></a>. If you pass an option attribute that is not relevant to a particular option, or fail to pass a required option attribute, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> raises <tt class="xref py py-exc docutils literal"><span class="pre">OptionError</span></tt>.</p> <dl class="attribute"> <dt id="optparse.Option.action"> <tt class="descclassname">Option.</tt><tt class="descname">action</tt><a class="headerlink" href="#optparse.Option.action" title="Permalink to this definition">¶</a></dt> <dd><p>(default: <tt class="docutils literal"><span class="pre">"store"</span></tt>)</p> <p>Determines <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>‘s behaviour when this option is seen on the command line; the available options are documented <a class="reference internal" href="#optparse-standard-option-actions"><em>here</em></a>.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.type"> <tt class="descclassname">Option.</tt><tt class="descname">type</tt><a class="headerlink" href="#optparse.Option.type" title="Permalink to this definition">¶</a></dt> <dd><p>(default: <tt class="docutils literal"><span class="pre">"string"</span></tt>)</p> <p>The argument type expected by this option (e.g., <tt class="docutils literal"><span class="pre">"string"</span></tt> or <tt class="docutils literal"><span class="pre">"int"</span></tt>); the available option types are documented <a class="reference internal" href="#optparse-standard-option-types"><em>here</em></a>.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.dest"> <tt class="descclassname">Option.</tt><tt class="descname">dest</tt><a class="headerlink" href="#optparse.Option.dest" title="Permalink to this definition">¶</a></dt> <dd><p>(default: derived from option strings)</p> <p>If the option’s action implies writing or modifying a value somewhere, this tells <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> where to write it: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> names an attribute of the <tt class="docutils literal"><span class="pre">options</span></tt> object that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> builds as it parses the command line.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.default"> <tt class="descclassname">Option.</tt><tt class="descname">default</tt><a class="headerlink" href="#optparse.Option.default" title="Permalink to this definition">¶</a></dt> <dd><p>The value to use for this option’s destination if the option is not seen on the command line. See also <a class="reference internal" href="#optparse.OptionParser.set_defaults" title="optparse.OptionParser.set_defaults"><tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.set_defaults()</span></tt></a>.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.nargs"> <tt class="descclassname">Option.</tt><tt class="descname">nargs</tt><a class="headerlink" href="#optparse.Option.nargs" title="Permalink to this definition">¶</a></dt> <dd><p>(default: 1)</p> <p>How many arguments of type <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> should be consumed when this option is seen. If > 1, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> will store a tuple of values to <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.const"> <tt class="descclassname">Option.</tt><tt class="descname">const</tt><a class="headerlink" href="#optparse.Option.const" title="Permalink to this definition">¶</a></dt> <dd><p>For actions that store a constant value, the constant value to store.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.choices"> <tt class="descclassname">Option.</tt><tt class="descname">choices</tt><a class="headerlink" href="#optparse.Option.choices" title="Permalink to this definition">¶</a></dt> <dd><p>For options of type <tt class="docutils literal"><span class="pre">"choice"</span></tt>, the list of strings the user may choose from.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.callback"> <tt class="descclassname">Option.</tt><tt class="descname">callback</tt><a class="headerlink" href="#optparse.Option.callback" title="Permalink to this definition">¶</a></dt> <dd><p>For options with action <tt class="docutils literal"><span class="pre">"callback"</span></tt>, the callable to call when this option is seen. See section <a class="reference internal" href="#optparse-option-callbacks"><em>Option Callbacks</em></a> for detail on the arguments passed to the callable.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.callback_args"> <tt class="descclassname">Option.</tt><tt class="descname">callback_args</tt><a class="headerlink" href="#optparse.Option.callback_args" title="Permalink to this definition">¶</a></dt> <dt id="optparse.Option.callback_kwargs"> <tt class="descclassname">Option.</tt><tt class="descname">callback_kwargs</tt><a class="headerlink" href="#optparse.Option.callback_kwargs" title="Permalink to this definition">¶</a></dt> <dd><p>Additional positional and keyword arguments to pass to <tt class="docutils literal"><span class="pre">callback</span></tt> after the four standard callback arguments.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.help"> <tt class="descclassname">Option.</tt><tt class="descname">help</tt><a class="headerlink" href="#optparse.Option.help" title="Permalink to this definition">¶</a></dt> <dd><p>Help text to print for this option when listing all available options after the user supplies a <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> option (such as <tt class="docutils literal"><span class="pre">--help</span></tt>). If no help text is supplied, the option will be listed without help text. To hide this option, use the special value <tt class="xref py py-data docutils literal"><span class="pre">optparse.SUPPRESS_HELP</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.metavar"> <tt class="descclassname">Option.</tt><tt class="descname">metavar</tt><a class="headerlink" href="#optparse.Option.metavar" title="Permalink to this definition">¶</a></dt> <dd><p>(default: derived from option strings)</p> <p>Stand-in for the option argument(s) to use when printing help text. See section <a class="reference internal" href="#optparse-tutorial"><em>Tutorial</em></a> for an example.</p> </dd></dl> </div> <div class="section" id="standard-option-actions"> <span id="optparse-standard-option-actions"></span><h3>15.5.3.5. Standard option actions<a class="headerlink" href="#standard-option-actions" title="Permalink to this headline">¶</a></h3> <p>The various option actions all have slightly different requirements and effects. Most actions have several relevant option attributes which you may specify to guide <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>‘s behaviour; a few have required attributes, which you must specify for any option using that action.</p> <ul> <li><p class="first"><tt class="docutils literal"><span class="pre">"store"</span></tt> [relevant: <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>, <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a>, <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a>]</p> <p>The option must be followed by an argument, which is converted to a value according to <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> and stored in <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>. If <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a> > 1, multiple arguments will be consumed from the command line; all will be converted according to <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> and stored to <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> as a tuple. See the <a class="reference internal" href="#optparse-standard-option-types"><em>Standard option types</em></a> section.</p> <p>If <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a> is supplied (a list or tuple of strings), the type defaults to <tt class="docutils literal"><span class="pre">"choice"</span></tt>.</p> <p>If <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> is not supplied, it defaults to <tt class="docutils literal"><span class="pre">"string"</span></tt>.</p> <p>If <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> is not supplied, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> derives a destination from the first long option string (e.g., <tt class="docutils literal"><span class="pre">--foo-bar</span></tt> implies <tt class="docutils literal"><span class="pre">foo_bar</span></tt>). If there are no long option strings, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> derives a destination from the first short option string (e.g., <tt class="docutils literal"><span class="pre">-f</span></tt> implies <tt class="docutils literal"><span class="pre">f</span></tt>).</p> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-f"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-p"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">"float"</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"point"</span><span class="p">)</span> </pre></div> </div> <p>As it parses the command line</p> <div class="highlight-python"><pre>-f foo.txt -p 1 -3.5 4 -fbar.txt</pre> </div> <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> will set</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">f</span> <span class="o">=</span> <span class="s">"foo.txt"</span> <span class="n">options</span><span class="o">.</span><span class="n">point</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.5</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">)</span> <span class="n">options</span><span class="o">.</span><span class="n">f</span> <span class="o">=</span> <span class="s">"bar.txt"</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">"store_const"</span></tt> [required: <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><tt class="xref py py-attr docutils literal"><span class="pre">const</span></tt></a>; relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>]</p> <p>The value <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><tt class="xref py py-attr docutils literal"><span class="pre">const</span></tt></a> is stored in <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.</p> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-q"</span><span class="p">,</span> <span class="s">"--quiet"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_const"</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-v"</span><span class="p">,</span> <span class="s">"--verbose"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_const"</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--noisy"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_const"</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">)</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">--noisy</span></tt> is seen, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> will set</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="mi">2</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">"store_true"</span></tt> [relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>]</p> <p>A special case of <tt class="docutils literal"><span class="pre">"store_const"</span></tt> that stores a true value to <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">"store_false"</span></tt> [relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>]</p> <p>Like <tt class="docutils literal"><span class="pre">"store_true"</span></tt>, but stores a false value.</p> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--clobber"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"clobber"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--no-clobber"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_false"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"clobber"</span><span class="p">)</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">"append"</span></tt> [relevant: <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>, <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a>, <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a>]</p> <p>The option must be followed by an argument, which is appended to the list in <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>. If no default value for <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> is supplied, an empty list is automatically created when <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> first encounters this option on the command-line. If <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a> > 1, multiple arguments are consumed, and a tuple of length <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a> is appended to <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>.</p> <p>The defaults for <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> and <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> are the same as for the <tt class="docutils literal"><span class="pre">"store"</span></tt> action.</p> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-t"</span><span class="p">,</span> <span class="s">"--tracks"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"append"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">"int"</span><span class="p">)</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">-t3</span></tt> is seen on the command-line, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> does the equivalent of:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">tracks</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">options</span><span class="o">.</span><span class="n">tracks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="s">"3"</span><span class="p">))</span> </pre></div> </div> <p>If, a little later on, <tt class="docutils literal"><span class="pre">--tracks=4</span></tt> is seen, it does:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">tracks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="s">"4"</span><span class="p">))</span> </pre></div> </div> <p>The <tt class="docutils literal"><span class="pre">append</span></tt> action calls the <tt class="docutils literal"><span class="pre">append</span></tt> method on the current value of the option. This means that any default value specified must have an <tt class="docutils literal"><span class="pre">append</span></tt> method. It also means that if the default value is non-empty, the default elements will be present in the parsed value for the option, with any values from the command line appended after those default values:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--files"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"append"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="p">[</span><span class="s">'~/.mypkg/defaults'</span><span class="p">])</span> <span class="gp">>>> </span><span class="n">opts</span><span class="p">,</span> <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">'--files'</span><span class="p">,</span> <span class="s">'overrides.mypkg'</span><span class="p">])</span> <span class="gp">>>> </span><span class="n">opts</span><span class="o">.</span><span class="n">files</span> <span class="go">['~/.mypkg/defaults', 'overrides.mypkg']</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">"append_const"</span></tt> [required: <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><tt class="xref py py-attr docutils literal"><span class="pre">const</span></tt></a>; relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>]</p> <p>Like <tt class="docutils literal"><span class="pre">"store_const"</span></tt>, but the value <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><tt class="xref py py-attr docutils literal"><span class="pre">const</span></tt></a> is appended to <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>; as with <tt class="docutils literal"><span class="pre">"append"</span></tt>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> defaults to <tt class="docutils literal"><span class="pre">None</span></tt>, and an empty list is automatically created the first time the option is encountered.</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">"count"</span></tt> [relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>]</p> <p>Increment the integer stored at <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a>. If no default value is supplied, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> is set to zero before being incremented the first time.</p> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-v"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"count"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbosity"</span><span class="p">)</span> </pre></div> </div> <p>The first time <tt class="docutils literal"><span class="pre">-v</span></tt> is seen on the command line, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> does the equivalent of:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">verbosity</span> <span class="o">=</span> <span class="mi">0</span> <span class="n">options</span><span class="o">.</span><span class="n">verbosity</span> <span class="o">+=</span> <span class="mi">1</span> </pre></div> </div> <p>Every subsequent occurrence of <tt class="docutils literal"><span class="pre">-v</span></tt> results in</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="o">.</span><span class="n">verbosity</span> <span class="o">+=</span> <span class="mi">1</span> </pre></div> </div> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">"callback"</span></tt> [required: <a class="reference internal" href="#optparse.Option.callback" title="optparse.Option.callback"><tt class="xref py py-attr docutils literal"><span class="pre">callback</span></tt></a>; relevant: <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>, <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a>, <a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><tt class="xref py py-attr docutils literal"><span class="pre">callback_args</span></tt></a>, <a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><tt class="xref py py-attr docutils literal"><span class="pre">callback_kwargs</span></tt></a>]</p> <p>Call the function specified by <a class="reference internal" href="#optparse.Option.callback" title="optparse.Option.callback"><tt class="xref py py-attr docutils literal"><span class="pre">callback</span></tt></a>, which is called as</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">func</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> </pre></div> </div> <p>See section <a class="reference internal" href="#optparse-option-callbacks"><em>Option Callbacks</em></a> for more detail.</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">"help"</span></tt></p> <p>Prints a complete help message for all the options in the current option parser. The help message is constructed from the <tt class="docutils literal"><span class="pre">usage</span></tt> string passed to OptionParser’s constructor and the <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> string passed to every option.</p> <p>If no <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> string is supplied for an option, it will still be listed in the help message. To omit an option entirely, use the special value <tt class="xref py py-data docutils literal"><span class="pre">optparse.SUPPRESS_HELP</span></tt>.</p> <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> automatically adds a <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> option to all OptionParsers, so you do not normally need to create one.</p> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span><span class="p">,</span> <span class="n">SUPPRESS_HELP</span> <span class="c"># usually, a help option is added automatically, but that can</span> <span class="c"># be suppressed using the add_help_option argument</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">add_help_option</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-h"</span><span class="p">,</span> <span class="s">"--help"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"help"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-v"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"verbose"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"Be moderately verbose"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--file"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"filename"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"Input file to read data from"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--secret"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="n">SUPPRESS_HELP</span><span class="p">)</span> </pre></div> </div> <p>If <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> sees either <tt class="docutils literal"><span class="pre">-h</span></tt> or <tt class="docutils literal"><span class="pre">--help</span></tt> on the command line, it will print something like the following help message to stdout (assuming <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> is <tt class="docutils literal"><span class="pre">"foo.py"</span></tt>):</p> <div class="highlight-text"><div class="highlight"><pre>Usage: foo.py [options] Options: -h, --help Show this help message and exit -v Be moderately verbose --file=FILENAME Input file to read data from </pre></div> </div> <p>After printing the help message, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> terminates your process with <tt class="docutils literal"><span class="pre">sys.exit(0)</span></tt>.</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">"version"</span></tt></p> <p>Prints the version number supplied to the OptionParser to stdout and exits. The version number is actually formatted and printed by the <tt class="docutils literal"><span class="pre">print_version()</span></tt> method of OptionParser. Generally only relevant if the <tt class="docutils literal"><span class="pre">version</span></tt> argument is supplied to the OptionParser constructor. As with <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><tt class="xref py py-attr docutils literal"><span class="pre">help</span></tt></a> options, you will rarely create <tt class="docutils literal"><span class="pre">version</span></tt> options, since <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> automatically adds them when needed.</p> </li> </ul> </div> <div class="section" id="standard-option-types"> <span id="optparse-standard-option-types"></span><h3>15.5.3.6. Standard option types<a class="headerlink" href="#standard-option-types" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> has six built-in option types: <tt class="docutils literal"><span class="pre">"string"</span></tt>, <tt class="docutils literal"><span class="pre">"int"</span></tt>, <tt class="docutils literal"><span class="pre">"long"</span></tt>, <tt class="docutils literal"><span class="pre">"choice"</span></tt>, <tt class="docutils literal"><span class="pre">"float"</span></tt> and <tt class="docutils literal"><span class="pre">"complex"</span></tt>. If you need to add new option types, see section <a class="reference internal" href="#optparse-extending-optparse"><em>Extending optparse</em></a>.</p> <p>Arguments to string options are not checked or converted in any way: the text on the command line is stored in the destination (or passed to the callback) as-is.</p> <p>Integer arguments (type <tt class="docutils literal"><span class="pre">"int"</span></tt> or <tt class="docutils literal"><span class="pre">"long"</span></tt>) are parsed as follows:</p> <ul class="simple"> <li>if the number starts with <tt class="docutils literal"><span class="pre">0x</span></tt>, it is parsed as a hexadecimal number</li> <li>if the number starts with <tt class="docutils literal"><span class="pre">0</span></tt>, it is parsed as an octal number</li> <li>if the number starts with <tt class="docutils literal"><span class="pre">0b</span></tt>, it is parsed as a binary number</li> <li>otherwise, the number is parsed as a decimal number</li> </ul> <p>The conversion is done by calling either <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a> or <a class="reference internal" href="functions.html#long" title="long"><tt class="xref py py-func docutils literal"><span class="pre">long()</span></tt></a> with the appropriate base (2, 8, 10, or 16). If this fails, so will <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>, although with a more useful error message.</p> <p><tt class="docutils literal"><span class="pre">"float"</span></tt> and <tt class="docutils literal"><span class="pre">"complex"</span></tt> option arguments are converted directly with <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-func docutils literal"><span class="pre">float()</span></tt></a> and <a class="reference internal" href="functions.html#complex" title="complex"><tt class="xref py py-func docutils literal"><span class="pre">complex()</span></tt></a>, with similar error-handling.</p> <p><tt class="docutils literal"><span class="pre">"choice"</span></tt> options are a subtype of <tt class="docutils literal"><span class="pre">"string"</span></tt> options. The <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a> option attribute (a sequence of strings) defines the set of allowed option arguments. <tt class="xref py py-func docutils literal"><span class="pre">optparse.check_choice()</span></tt> compares user-supplied option arguments against this master list and raises <tt class="xref py py-exc docutils literal"><span class="pre">OptionValueError</span></tt> if an invalid string is given.</p> </div> <div class="section" id="parsing-arguments"> <span id="optparse-parsing-arguments"></span><h3>15.5.3.7. Parsing arguments<a class="headerlink" href="#parsing-arguments" title="Permalink to this headline">¶</a></h3> <p>The whole point of creating and populating an OptionParser is to call its <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt> method:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">args</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span> </pre></div> </div> <p>where the input parameters are</p> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">args</span></tt></dt> <dd>the list of arguments to process (default: <tt class="docutils literal"><span class="pre">sys.argv[1:]</span></tt>)</dd> <dt><tt class="docutils literal"><span class="pre">values</span></tt></dt> <dd>a <tt class="xref py py-class docutils literal"><span class="pre">optparse.Values</span></tt> object to store option arguments in (default: a new instance of <tt class="xref py py-class docutils literal"><span class="pre">Values</span></tt>) – if you give an existing object, the option defaults will not be initialized on it</dd> </dl> <p>and the return values are</p> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">options</span></tt></dt> <dd>the same object that was passed in as <tt class="docutils literal"><span class="pre">values</span></tt>, or the optparse.Values instance created by <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a></dd> <dt><tt class="docutils literal"><span class="pre">args</span></tt></dt> <dd>the leftover positional arguments after all options have been processed</dd> </dl> <p>The most common usage is to supply neither keyword argument. If you supply <tt class="docutils literal"><span class="pre">values</span></tt>, it will be modified with repeated <a class="reference internal" href="functions.html#setattr" title="setattr"><tt class="xref py py-func docutils literal"><span class="pre">setattr()</span></tt></a> calls (roughly one for every option argument stored to an option destination) and returned by <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt>.</p> <p>If <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt> encounters any errors in the argument list, it calls the OptionParser’s <tt class="xref py py-meth docutils literal"><span class="pre">error()</span></tt> method with an appropriate end-user error message. This ultimately terminates your process with an exit status of 2 (the traditional Unix exit status for command-line errors).</p> </div> <div class="section" id="querying-and-manipulating-your-option-parser"> <span id="optparse-querying-manipulating-option-parser"></span><h3>15.5.3.8. Querying and manipulating your option parser<a class="headerlink" href="#querying-and-manipulating-your-option-parser" title="Permalink to this headline">¶</a></h3> <p>The default behavior of the option parser can be customized slightly, and you can also poke around your option parser and see what’s there. OptionParser provides several methods to help you out:</p> <dl class="method"> <dt id="optparse.OptionParser.disable_interspersed_args"> <tt class="descclassname">OptionParser.</tt><tt class="descname">disable_interspersed_args</tt><big>(</big><big>)</big><a class="headerlink" href="#optparse.OptionParser.disable_interspersed_args" title="Permalink to this definition">¶</a></dt> <dd><p>Set parsing to stop on the first non-option. For example, if <tt class="docutils literal"><span class="pre">-a</span></tt> and <tt class="docutils literal"><span class="pre">-b</span></tt> are both simple options that take no arguments, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> normally accepts this syntax:</p> <div class="highlight-python"><pre>prog -a arg1 -b arg2</pre> </div> <p>and treats it as equivalent to</p> <div class="highlight-python"><pre>prog -a -b arg1 arg2</pre> </div> <p>To disable this feature, call <a class="reference internal" href="#optparse.OptionParser.disable_interspersed_args" title="optparse.OptionParser.disable_interspersed_args"><tt class="xref py py-meth docutils literal"><span class="pre">disable_interspersed_args()</span></tt></a>. This restores traditional Unix syntax, where option parsing stops with the first non-option argument.</p> <p>Use this if you have a command processor which runs another command which has options of its own and you want to make sure these options don’t get confused. For example, each command might have a different set of options.</p> </dd></dl> <dl class="method"> <dt id="optparse.OptionParser.enable_interspersed_args"> <tt class="descclassname">OptionParser.</tt><tt class="descname">enable_interspersed_args</tt><big>(</big><big>)</big><a class="headerlink" href="#optparse.OptionParser.enable_interspersed_args" title="Permalink to this definition">¶</a></dt> <dd><p>Set parsing to not stop on the first non-option, allowing interspersing switches with command arguments. This is the default behavior.</p> </dd></dl> <dl class="method"> <dt id="optparse.OptionParser.get_option"> <tt class="descclassname">OptionParser.</tt><tt class="descname">get_option</tt><big>(</big><em>opt_str</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.get_option" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the Option instance with the option string <em>opt_str</em>, or <tt class="docutils literal"><span class="pre">None</span></tt> if no options have that option string.</p> </dd></dl> <dl class="method"> <dt id="optparse.OptionParser.has_option"> <tt class="descclassname">OptionParser.</tt><tt class="descname">has_option</tt><big>(</big><em>opt_str</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.has_option" title="Permalink to this definition">¶</a></dt> <dd><p>Return true if the OptionParser has an option with option string <em>opt_str</em> (e.g., <tt class="docutils literal"><span class="pre">-q</span></tt> or <tt class="docutils literal"><span class="pre">--verbose</span></tt>).</p> </dd></dl> <dl class="method"> <dt id="optparse.OptionParser.remove_option"> <tt class="descclassname">OptionParser.</tt><tt class="descname">remove_option</tt><big>(</big><em>opt_str</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.remove_option" title="Permalink to this definition">¶</a></dt> <dd><p>If the <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a> has an option corresponding to <em>opt_str</em>, that option is removed. If that option provided any other option strings, all of those option strings become invalid. If <em>opt_str</em> does not occur in any option belonging to this <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a>, raises <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>.</p> </dd></dl> </div> <div class="section" id="conflicts-between-options"> <span id="optparse-conflicts-between-options"></span><h3>15.5.3.9. Conflicts between options<a class="headerlink" href="#conflicts-between-options" title="Permalink to this headline">¶</a></h3> <p>If you’re not careful, it’s easy to define options with conflicting option strings:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-n"</span><span class="p">,</span> <span class="s">"--dry-run"</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-n"</span><span class="p">,</span> <span class="s">"--noisy"</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span> </pre></div> </div> <p>(This is particularly true if you’ve defined your own OptionParser subclass with some standard options.)</p> <p>Every time you add an option, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> checks for conflicts with existing options. If it finds any, it invokes the current conflict-handling mechanism. You can set the conflict-handling mechanism either in the constructor:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">conflict_handler</span><span class="o">=</span><span class="n">handler</span><span class="p">)</span> </pre></div> </div> <p>or with a separate call:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">set_conflict_handler</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span> </pre></div> </div> <p>The available conflict handlers are:</p> <blockquote> <div><dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">"error"</span></tt> (default)</dt> <dd>assume option conflicts are a programming error and raise <tt class="xref py py-exc docutils literal"><span class="pre">OptionConflictError</span></tt></dd> <dt><tt class="docutils literal"><span class="pre">"resolve"</span></tt></dt> <dd>resolve option conflicts intelligently (see below)</dd> </dl> </div></blockquote> <p>As an example, let’s define an <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a> that resolves conflicts intelligently and add conflicting options to it:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">conflict_handler</span><span class="o">=</span><span class="s">"resolve"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-n"</span><span class="p">,</span> <span class="s">"--dry-run"</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"do no harm"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-n"</span><span class="p">,</span> <span class="s">"--noisy"</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"be noisy"</span><span class="p">)</span> </pre></div> </div> <p>At this point, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> detects that a previously-added option is already using the <tt class="docutils literal"><span class="pre">-n</span></tt> option string. Since <tt class="docutils literal"><span class="pre">conflict_handler</span></tt> is <tt class="docutils literal"><span class="pre">"resolve"</span></tt>, it resolves the situation by removing <tt class="docutils literal"><span class="pre">-n</span></tt> from the earlier option’s list of option strings. Now <tt class="docutils literal"><span class="pre">--dry-run</span></tt> is the only way for the user to activate that option. If the user asks for help, the help message will reflect that:</p> <div class="highlight-python"><pre>Options: --dry-run do no harm [...] -n, --noisy be noisy</pre> </div> <p>It’s possible to whittle away the option strings for a previously-added option until there are none left, and the user has no way of invoking that option from the command-line. In that case, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> removes that option completely, so it doesn’t show up in help text or anywhere else. Carrying on with our existing OptionParser:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--dry-run"</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"new dry-run option"</span><span class="p">)</span> </pre></div> </div> <p>At this point, the original <tt class="docutils literal"><span class="pre">-n</span></tt>/<tt class="docutils literal"><span class="pre">--dry-run</span></tt> option is no longer accessible, so <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> removes it, leaving this help text:</p> <div class="highlight-python"><pre>Options: [...] -n, --noisy be noisy --dry-run new dry-run option</pre> </div> </div> <div class="section" id="cleanup"> <span id="optparse-cleanup"></span><h3>15.5.3.10. Cleanup<a class="headerlink" href="#cleanup" title="Permalink to this headline">¶</a></h3> <p>OptionParser instances have several cyclic references. This should not be a problem for Python’s garbage collector, but you may wish to break the cyclic references explicitly by calling <tt class="xref py py-meth docutils literal"><span class="pre">destroy()</span></tt> on your OptionParser once you are done with it. This is particularly useful in long-running applications where large object graphs are reachable from your OptionParser.</p> </div> <div class="section" id="other-methods"> <span id="optparse-other-methods"></span><h3>15.5.3.11. Other methods<a class="headerlink" href="#other-methods" title="Permalink to this headline">¶</a></h3> <p>OptionParser supports several other public methods:</p> <dl class="method"> <dt id="optparse.OptionParser.set_usage"> <tt class="descclassname">OptionParser.</tt><tt class="descname">set_usage</tt><big>(</big><em>usage</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.set_usage" title="Permalink to this definition">¶</a></dt> <dd><p>Set the usage string according to the rules described above for the <tt class="docutils literal"><span class="pre">usage</span></tt> constructor keyword argument. Passing <tt class="docutils literal"><span class="pre">None</span></tt> sets the default usage string; use <tt class="xref py py-data docutils literal"><span class="pre">optparse.SUPPRESS_USAGE</span></tt> to suppress a usage message.</p> </dd></dl> <dl class="method"> <dt id="optparse.OptionParser.print_usage"> <tt class="descclassname">OptionParser.</tt><tt class="descname">print_usage</tt><big>(</big><em>file=None</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.print_usage" title="Permalink to this definition">¶</a></dt> <dd><p>Print the usage message for the current program (<tt class="docutils literal"><span class="pre">self.usage</span></tt>) to <em>file</em> (default stdout). Any occurrence of the string <tt class="docutils literal"><span class="pre">%prog</span></tt> in <tt class="docutils literal"><span class="pre">self.usage</span></tt> is replaced with the name of the current program. Does nothing if <tt class="docutils literal"><span class="pre">self.usage</span></tt> is empty or not defined.</p> </dd></dl> <dl class="method"> <dt id="optparse.OptionParser.get_usage"> <tt class="descclassname">OptionParser.</tt><tt class="descname">get_usage</tt><big>(</big><big>)</big><a class="headerlink" href="#optparse.OptionParser.get_usage" title="Permalink to this definition">¶</a></dt> <dd><p>Same as <a class="reference internal" href="#optparse.OptionParser.print_usage" title="optparse.OptionParser.print_usage"><tt class="xref py py-meth docutils literal"><span class="pre">print_usage()</span></tt></a> but returns the usage string instead of printing it.</p> </dd></dl> <dl class="method"> <dt id="optparse.OptionParser.set_defaults"> <tt class="descclassname">OptionParser.</tt><tt class="descname">set_defaults</tt><big>(</big><em>dest=value</em>, <em>...</em><big>)</big><a class="headerlink" href="#optparse.OptionParser.set_defaults" title="Permalink to this definition">¶</a></dt> <dd><p>Set default values for several option destinations at once. Using <a class="reference internal" href="#optparse.OptionParser.set_defaults" title="optparse.OptionParser.set_defaults"><tt class="xref py py-meth docutils literal"><span class="pre">set_defaults()</span></tt></a> is the preferred way to set default values for options, since multiple options can share the same destination. For example, if several “mode” options all set the same destination, any one of them can set the default, and the last one wins:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--advanced"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_const"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"mode"</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s">"advanced"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s">"novice"</span><span class="p">)</span> <span class="c"># overridden below</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--novice"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_const"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"mode"</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s">"novice"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s">"advanced"</span><span class="p">)</span> <span class="c"># overrides above setting</span> </pre></div> </div> <p>To avoid this confusion, use <a class="reference internal" href="#optparse.OptionParser.set_defaults" title="optparse.OptionParser.set_defaults"><tt class="xref py py-meth docutils literal"><span class="pre">set_defaults()</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">set_defaults</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s">"advanced"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--advanced"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_const"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"mode"</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s">"advanced"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--novice"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_const"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"mode"</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s">"novice"</span><span class="p">)</span> </pre></div> </div> </dd></dl> </div> </div> <div class="section" id="option-callbacks"> <span id="optparse-option-callbacks"></span><h2>15.5.4. Option Callbacks<a class="headerlink" href="#option-callbacks" title="Permalink to this headline">¶</a></h2> <p>When <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>‘s built-in actions and types aren’t quite enough for your needs, you have two choices: extend <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> or define a callback option. Extending <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> is more general, but overkill for a lot of simple cases. Quite often a simple callback is all you need.</p> <p>There are two steps to defining a callback option:</p> <ul class="simple"> <li>define the option itself using the <tt class="docutils literal"><span class="pre">"callback"</span></tt> action</li> <li>write the callback; this is a function (or method) that takes at least four arguments, as described below</li> </ul> <div class="section" id="defining-a-callback-option"> <span id="optparse-defining-callback-option"></span><h3>15.5.4.1. Defining a callback option<a class="headerlink" href="#defining-a-callback-option" title="Permalink to this headline">¶</a></h3> <p>As always, the easiest way to define a callback option is by using the <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.add_option()</span></tt></a> method. Apart from <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><tt class="xref py py-attr docutils literal"><span class="pre">action</span></tt></a>, the only option attribute you must specify is <tt class="docutils literal"><span class="pre">callback</span></tt>, the function to call:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-c"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"callback"</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">my_callback</span><span class="p">)</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">callback</span></tt> is a function (or other callable object), so you must have already defined <tt class="docutils literal"><span class="pre">my_callback()</span></tt> when you create this callback option. In this simple case, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> doesn’t even know if <tt class="docutils literal"><span class="pre">-c</span></tt> takes any arguments, which usually means that the option takes no arguments—the mere presence of <tt class="docutils literal"><span class="pre">-c</span></tt> on the command-line is all it needs to know. In some circumstances, though, you might want your callback to consume an arbitrary number of command-line arguments. This is where writing callbacks gets tricky; it’s covered later in this section.</p> <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> always passes four particular arguments to your callback, and it will only pass additional arguments if you specify them via <a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><tt class="xref py py-attr docutils literal"><span class="pre">callback_args</span></tt></a> and <a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><tt class="xref py py-attr docutils literal"><span class="pre">callback_kwargs</span></tt></a>. Thus, the minimal callback function signature is:</p> <div class="highlight-python"><pre>def my_callback(option, opt, value, parser):</pre> </div> <p>The four arguments to a callback are described below.</p> <p>There are several other option attributes that you can supply when you define a callback option:</p> <dl class="docutils"> <dt><a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a></dt> <dd>has its usual meaning: as with the <tt class="docutils literal"><span class="pre">"store"</span></tt> or <tt class="docutils literal"><span class="pre">"append"</span></tt> actions, it instructs <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> to consume one argument and convert it to <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>. Rather than storing the converted value(s) anywhere, though, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> passes it to your callback function.</dd> <dt><a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a></dt> <dd>also has its usual meaning: if it is supplied and > 1, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> will consume <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a> arguments, each of which must be convertible to <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>. It then passes a tuple of converted values to your callback.</dd> <dt><a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><tt class="xref py py-attr docutils literal"><span class="pre">callback_args</span></tt></a></dt> <dd>a tuple of extra positional arguments to pass to the callback</dd> <dt><a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><tt class="xref py py-attr docutils literal"><span class="pre">callback_kwargs</span></tt></a></dt> <dd>a dictionary of extra keyword arguments to pass to the callback</dd> </dl> </div> <div class="section" id="how-callbacks-are-called"> <span id="optparse-how-callbacks-called"></span><h3>15.5.4.2. How callbacks are called<a class="headerlink" href="#how-callbacks-are-called" title="Permalink to this headline">¶</a></h3> <p>All callbacks are called as follows:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">func</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> </pre></div> </div> <p>where</p> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">option</span></tt></dt> <dd>is the Option instance that’s calling the callback</dd> <dt><tt class="docutils literal"><span class="pre">opt_str</span></tt></dt> <dd>is the option string seen on the command-line that’s triggering the callback. (If an abbreviated long option was used, <tt class="docutils literal"><span class="pre">opt_str</span></tt> will be the full, canonical option string—e.g. if the user puts <tt class="docutils literal"><span class="pre">--foo</span></tt> on the command-line as an abbreviation for <tt class="docutils literal"><span class="pre">--foobar</span></tt>, then <tt class="docutils literal"><span class="pre">opt_str</span></tt> will be <tt class="docutils literal"><span class="pre">"--foobar"</span></tt>.)</dd> <dt><tt class="docutils literal"><span class="pre">value</span></tt></dt> <dd>is the argument to this option seen on the command-line. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> will only expect an argument if <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> is set; the type of <tt class="docutils literal"><span class="pre">value</span></tt> will be the type implied by the option’s type. If <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> for this option is <tt class="docutils literal"><span class="pre">None</span></tt> (no argument expected), then <tt class="docutils literal"><span class="pre">value</span></tt> will be <tt class="docutils literal"><span class="pre">None</span></tt>. If <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a> > 1, <tt class="docutils literal"><span class="pre">value</span></tt> will be a tuple of values of the appropriate type.</dd> <dt><tt class="docutils literal"><span class="pre">parser</span></tt></dt> <dd><p class="first">is the OptionParser instance driving the whole thing, mainly useful because you can access some other interesting data through its instance attributes:</p> <dl class="last docutils"> <dt><tt class="docutils literal"><span class="pre">parser.largs</span></tt></dt> <dd>the current list of leftover arguments, ie. arguments that have been consumed but are neither options nor option arguments. Feel free to modify <tt class="docutils literal"><span class="pre">parser.largs</span></tt>, e.g. by adding more arguments to it. (This list will become <tt class="docutils literal"><span class="pre">args</span></tt>, the second return value of <tt class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></tt>.)</dd> <dt><tt class="docutils literal"><span class="pre">parser.rargs</span></tt></dt> <dd>the current list of remaining arguments, ie. with <tt class="docutils literal"><span class="pre">opt_str</span></tt> and <tt class="docutils literal"><span class="pre">value</span></tt> (if applicable) removed, and only the arguments following them still there. Feel free to modify <tt class="docutils literal"><span class="pre">parser.rargs</span></tt>, e.g. by consuming more arguments.</dd> <dt><tt class="docutils literal"><span class="pre">parser.values</span></tt></dt> <dd>the object where option values are by default stored (an instance of optparse.OptionValues). This lets callbacks use the same mechanism as the rest of <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> for storing option values; you don’t need to mess around with globals or closures. You can also access or modify the value(s) of any options already encountered on the command-line.</dd> </dl> </dd> <dt><tt class="docutils literal"><span class="pre">args</span></tt></dt> <dd>is a tuple of arbitrary positional arguments supplied via the <a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><tt class="xref py py-attr docutils literal"><span class="pre">callback_args</span></tt></a> option attribute.</dd> <dt><tt class="docutils literal"><span class="pre">kwargs</span></tt></dt> <dd>is a dictionary of arbitrary keyword arguments supplied via <a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><tt class="xref py py-attr docutils literal"><span class="pre">callback_kwargs</span></tt></a>.</dd> </dl> </div> <div class="section" id="raising-errors-in-a-callback"> <span id="optparse-raising-errors-in-callback"></span><h3>15.5.4.3. Raising errors in a callback<a class="headerlink" href="#raising-errors-in-a-callback" title="Permalink to this headline">¶</a></h3> <p>The callback function should raise <tt class="xref py py-exc docutils literal"><span class="pre">OptionValueError</span></tt> if there are any problems with the option or its argument(s). <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> catches this and terminates the program, printing the error message you supply to stderr. Your message should be clear, concise, accurate, and mention the option at fault. Otherwise, the user will have a hard time figuring out what he did wrong.</p> </div> <div class="section" id="callback-example-1-trivial-callback"> <span id="optparse-callback-example-1"></span><h3>15.5.4.4. Callback example 1: trivial callback<a class="headerlink" href="#callback-example-1-trivial-callback" title="Permalink to this headline">¶</a></h3> <p>Here’s an example of a callback option that takes no arguments, and simply records that the option was seen:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">record_foo_seen</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span> <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">saw_foo</span> <span class="o">=</span> <span class="bp">True</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--foo"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"callback"</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">record_foo_seen</span><span class="p">)</span> </pre></div> </div> <p>Of course, you could do that with the <tt class="docutils literal"><span class="pre">"store_true"</span></tt> action.</p> </div> <div class="section" id="callback-example-2-check-option-order"> <span id="optparse-callback-example-2"></span><h3>15.5.4.5. Callback example 2: check option order<a class="headerlink" href="#callback-example-2-check-option-order" title="Permalink to this headline">¶</a></h3> <p>Here’s a slightly more interesting example: record the fact that <tt class="docutils literal"><span class="pre">-a</span></tt> is seen, but blow up if it comes after <tt class="docutils literal"><span class="pre">-b</span></tt> in the command-line.</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">check_order</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span> <span class="k">if</span> <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">b</span><span class="p">:</span> <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span><span class="s">"can't use -a after -b"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-a"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"callback"</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_order</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-b"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"b"</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="callback-example-3-check-option-order-generalized"> <span id="optparse-callback-example-3"></span><h3>15.5.4.6. Callback example 3: check option order (generalized)<a class="headerlink" href="#callback-example-3-check-option-order-generalized" title="Permalink to this headline">¶</a></h3> <p>If you want to re-use this callback for several similar options (set a flag, but blow up if <tt class="docutils literal"><span class="pre">-b</span></tt> has already been seen), it needs a bit of work: the error message and the flag that it sets must be generalized.</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">check_order</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span> <span class="k">if</span> <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">b</span><span class="p">:</span> <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span><span class="s">"can't use </span><span class="si">%s</span><span class="s"> after -b"</span> <span class="o">%</span> <span class="n">opt_str</span><span class="p">)</span> <span class="nb">setattr</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-a"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"callback"</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_order</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">'a'</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-b"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"b"</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-c"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"callback"</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_order</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">'c'</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="callback-example-4-check-arbitrary-condition"> <span id="optparse-callback-example-4"></span><h3>15.5.4.7. Callback example 4: check arbitrary condition<a class="headerlink" href="#callback-example-4-check-arbitrary-condition" title="Permalink to this headline">¶</a></h3> <p>Of course, you could put any condition in there—you’re not limited to checking the values of already-defined options. For example, if you have options that should not be called when the moon is full, all you have to do is this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">check_moon</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span> <span class="k">if</span> <span class="n">is_moon_full</span><span class="p">():</span> <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span><span class="s">"</span><span class="si">%s</span><span class="s"> option invalid when moon is full"</span> <span class="o">%</span> <span class="n">opt_str</span><span class="p">)</span> <span class="nb">setattr</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--foo"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"callback"</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_moon</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"foo"</span><span class="p">)</span> </pre></div> </div> <p>(The definition of <tt class="docutils literal"><span class="pre">is_moon_full()</span></tt> is left as an exercise for the reader.)</p> </div> <div class="section" id="callback-example-5-fixed-arguments"> <span id="optparse-callback-example-5"></span><h3>15.5.4.8. Callback example 5: fixed arguments<a class="headerlink" href="#callback-example-5-fixed-arguments" title="Permalink to this headline">¶</a></h3> <p>Things get slightly more interesting when you define callback options that take a fixed number of arguments. Specifying that a callback option takes arguments is similar to defining a <tt class="docutils literal"><span class="pre">"store"</span></tt> or <tt class="docutils literal"><span class="pre">"append"</span></tt> option: if you define <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a>, then the option takes one argument that must be convertible to that type; if you further define <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a>, then the option takes <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><tt class="xref py py-attr docutils literal"><span class="pre">nargs</span></tt></a> arguments.</p> <p>Here’s an example that just emulates the standard <tt class="docutils literal"><span class="pre">"store"</span></tt> action:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">store_value</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span> <span class="nb">setattr</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"--foo"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"callback"</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">store_value</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">"int"</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"foo"</span><span class="p">)</span> </pre></div> </div> <p>Note that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> takes care of consuming 3 arguments and converting them to integers for you; all you have to do is store them. (Or whatever; obviously you don’t need a callback for this example.)</p> </div> <div class="section" id="callback-example-6-variable-arguments"> <span id="optparse-callback-example-6"></span><h3>15.5.4.9. Callback example 6: variable arguments<a class="headerlink" href="#callback-example-6-variable-arguments" title="Permalink to this headline">¶</a></h3> <p>Things get hairy when you want an option to take a variable number of arguments. For this case, you must write a callback, as <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> doesn’t provide any built-in capabilities for it. And you have to deal with certain intricacies of conventional Unix command-line parsing that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> normally handles for you. In particular, callbacks should implement the conventional rules for bare <tt class="docutils literal"><span class="pre">--</span></tt> and <tt class="docutils literal"><span class="pre">-</span></tt> arguments:</p> <ul class="simple"> <li>either <tt class="docutils literal"><span class="pre">--</span></tt> or <tt class="docutils literal"><span class="pre">-</span></tt> can be option arguments</li> <li>bare <tt class="docutils literal"><span class="pre">--</span></tt> (if not the argument to some option): halt command-line processing and discard the <tt class="docutils literal"><span class="pre">--</span></tt></li> <li>bare <tt class="docutils literal"><span class="pre">-</span></tt> (if not the argument to some option): halt command-line processing but keep the <tt class="docutils literal"><span class="pre">-</span></tt> (append it to <tt class="docutils literal"><span class="pre">parser.largs</span></tt>)</li> </ul> <p>If you want an option that takes a variable number of arguments, there are several subtle, tricky issues to worry about. The exact implementation you choose will be based on which trade-offs you’re willing to make for your application (which is why <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> doesn’t support this sort of thing directly).</p> <p>Nevertheless, here’s a stab at a callback for an option with variable arguments:</p> <div class="highlight-python"><pre> def vararg_callback(option, opt_str, value, parser): assert value is None value = [] def floatable(str): try: float(str) return True except ValueError: return False for arg in parser.rargs: # stop on --foo like options if arg[:2] == "--" and len(arg) > 2: break # stop on -a, but not on -3 or -3.0 if arg[:1] == "-" and len(arg) > 1 and not floatable(arg): break value.append(arg) del parser.rargs[:len(value)] setattr(parser.values, option.dest, value) [...] parser.add_option("-c", "--callback", dest="vararg_attr", action="callback", callback=vararg_callback)</pre> </div> </div> </div> <div class="section" id="extending-optparse"> <span id="optparse-extending-optparse"></span><h2>15.5.5. Extending <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a><a class="headerlink" href="#extending-optparse" title="Permalink to this headline">¶</a></h2> <p>Since the two major controlling factors in how <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> interprets command-line options are the action and type of each option, the most likely direction of extension is to add new actions and new types.</p> <div class="section" id="adding-new-types"> <span id="optparse-adding-new-types"></span><h3>15.5.5.1. Adding new types<a class="headerlink" href="#adding-new-types" title="Permalink to this headline">¶</a></h3> <p>To add new types, you need to define your own subclass of <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>‘s <tt class="xref py py-class docutils literal"><span class="pre">Option</span></tt> class. This class has a couple of attributes that define <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>‘s types: <a class="reference internal" href="#optparse.Option.TYPES" title="optparse.Option.TYPES"><tt class="xref py py-attr docutils literal"><span class="pre">TYPES</span></tt></a> and <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><tt class="xref py py-attr docutils literal"><span class="pre">TYPE_CHECKER</span></tt></a>.</p> <dl class="attribute"> <dt id="optparse.Option.TYPES"> <tt class="descclassname">Option.</tt><tt class="descname">TYPES</tt><a class="headerlink" href="#optparse.Option.TYPES" title="Permalink to this definition">¶</a></dt> <dd><p>A tuple of type names; in your subclass, simply define a new tuple <a class="reference internal" href="#optparse.Option.TYPES" title="optparse.Option.TYPES"><tt class="xref py py-attr docutils literal"><span class="pre">TYPES</span></tt></a> that builds on the standard one.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.TYPE_CHECKER"> <tt class="descclassname">Option.</tt><tt class="descname">TYPE_CHECKER</tt><a class="headerlink" href="#optparse.Option.TYPE_CHECKER" title="Permalink to this definition">¶</a></dt> <dd><p>A dictionary mapping type names to type-checking functions. A type-checking function has the following signature:</p> <div class="highlight-python"><pre>def check_mytype(option, opt, value)</pre> </div> <p>where <tt class="docutils literal"><span class="pre">option</span></tt> is an <tt class="xref py py-class docutils literal"><span class="pre">Option</span></tt> instance, <tt class="docutils literal"><span class="pre">opt</span></tt> is an option string (e.g., <tt class="docutils literal"><span class="pre">-f</span></tt>), and <tt class="docutils literal"><span class="pre">value</span></tt> is the string from the command line that must be checked and converted to your desired type. <tt class="docutils literal"><span class="pre">check_mytype()</span></tt> should return an object of the hypothetical type <tt class="docutils literal"><span class="pre">mytype</span></tt>. The value returned by a type-checking function will wind up in the OptionValues instance returned by <tt class="xref py py-meth docutils literal"><span class="pre">OptionParser.parse_args()</span></tt>, or be passed to a callback as the <tt class="docutils literal"><span class="pre">value</span></tt> parameter.</p> <p>Your type-checking function should raise <tt class="xref py py-exc docutils literal"><span class="pre">OptionValueError</span></tt> if it encounters any problems. <tt class="xref py py-exc docutils literal"><span class="pre">OptionValueError</span></tt> takes a single string argument, which is passed as-is to <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><tt class="xref py py-class docutils literal"><span class="pre">OptionParser</span></tt></a>‘s <tt class="xref py py-meth docutils literal"><span class="pre">error()</span></tt> method, which in turn prepends the program name and the string <tt class="docutils literal"><span class="pre">"error:"</span></tt> and prints everything to stderr before terminating the process.</p> </dd></dl> <p>Here’s a silly example that demonstrates adding a <tt class="docutils literal"><span class="pre">"complex"</span></tt> option type to parse Python-style complex numbers on the command line. (This is even sillier than it used to be, because <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> 1.3 added built-in support for complex numbers, but never mind.)</p> <p>First, the necessary imports:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">copy</span> <span class="kn">import</span> <span class="n">copy</span> <span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">Option</span><span class="p">,</span> <span class="n">OptionValueError</span> </pre></div> </div> <p>You need to define your type-checker first, since it’s referred to later (in the <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><tt class="xref py py-attr docutils literal"><span class="pre">TYPE_CHECKER</span></tt></a> class attribute of your Option subclass):</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">check_complex</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="k">try</span><span class="p">:</span> <span class="k">return</span> <span class="nb">complex</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span> <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span> <span class="s">"option </span><span class="si">%s</span><span class="s">: invalid complex value: </span><span class="si">%r</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span> </pre></div> </div> <p>Finally, the Option subclass:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyOption</span> <span class="p">(</span><span class="n">Option</span><span class="p">):</span> <span class="n">TYPES</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">TYPES</span> <span class="o">+</span> <span class="p">(</span><span class="s">"complex"</span><span class="p">,)</span> <span class="n">TYPE_CHECKER</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">Option</span><span class="o">.</span><span class="n">TYPE_CHECKER</span><span class="p">)</span> <span class="n">TYPE_CHECKER</span><span class="p">[</span><span class="s">"complex"</span><span class="p">]</span> <span class="o">=</span> <span class="n">check_complex</span> </pre></div> </div> <p>(If we didn’t make a <a class="reference internal" href="copy.html#module-copy" title="copy: Shallow and deep copy operations."><tt class="xref py py-func docutils literal"><span class="pre">copy()</span></tt></a> of <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><tt class="xref py py-attr docutils literal"><span class="pre">Option.TYPE_CHECKER</span></tt></a>, we would end up modifying the <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><tt class="xref py py-attr docutils literal"><span class="pre">TYPE_CHECKER</span></tt></a> attribute of <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>‘s Option class. This being Python, nothing stops you from doing that except good manners and common sense.)</p> <p>That’s it! Now you can write a script that uses the new option type just like any other <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a>-based script, except you have to instruct your OptionParser to use MyOption instead of Option:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">option_class</span><span class="o">=</span><span class="n">MyOption</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-c"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">"complex"</span><span class="p">)</span> </pre></div> </div> <p>Alternately, you can build your own option list and pass it to OptionParser; if you don’t use <tt class="xref py py-meth docutils literal"><span class="pre">add_option()</span></tt> in the above way, you don’t need to tell OptionParser which option class to use:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">option_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">MyOption</span><span class="p">(</span><span class="s">"-c"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">"complex"</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">"c"</span><span class="p">)]</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">option_list</span><span class="o">=</span><span class="n">option_list</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="adding-new-actions"> <span id="optparse-adding-new-actions"></span><h3>15.5.5.2. Adding new actions<a class="headerlink" href="#adding-new-actions" title="Permalink to this headline">¶</a></h3> <p>Adding new actions is a bit trickier, because you have to understand that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> has a couple of classifications for actions:</p> <dl class="docutils"> <dt>“store” actions</dt> <dd>actions that result in <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> storing a value to an attribute of the current OptionValues instance; these options require a <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><tt class="xref py py-attr docutils literal"><span class="pre">dest</span></tt></a> attribute to be supplied to the Option constructor.</dd> <dt>“typed” actions</dt> <dd>actions that take a value from the command line and expect it to be of a certain type; or rather, a string that can be converted to a certain type. These options require a <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> attribute to the Option constructor.</dd> </dl> <p>These are overlapping sets: some default “store” actions are <tt class="docutils literal"><span class="pre">"store"</span></tt>, <tt class="docutils literal"><span class="pre">"store_const"</span></tt>, <tt class="docutils literal"><span class="pre">"append"</span></tt>, and <tt class="docutils literal"><span class="pre">"count"</span></tt>, while the default “typed” actions are <tt class="docutils literal"><span class="pre">"store"</span></tt>, <tt class="docutils literal"><span class="pre">"append"</span></tt>, and <tt class="docutils literal"><span class="pre">"callback"</span></tt>.</p> <p>When you add an action, you need to categorize it by listing it in at least one of the following class attributes of Option (all are lists of strings):</p> <dl class="attribute"> <dt id="optparse.Option.ACTIONS"> <tt class="descclassname">Option.</tt><tt class="descname">ACTIONS</tt><a class="headerlink" href="#optparse.Option.ACTIONS" title="Permalink to this definition">¶</a></dt> <dd><p>All actions must be listed in ACTIONS.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.STORE_ACTIONS"> <tt class="descclassname">Option.</tt><tt class="descname">STORE_ACTIONS</tt><a class="headerlink" href="#optparse.Option.STORE_ACTIONS" title="Permalink to this definition">¶</a></dt> <dd><p>“store” actions are additionally listed here.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.TYPED_ACTIONS"> <tt class="descclassname">Option.</tt><tt class="descname">TYPED_ACTIONS</tt><a class="headerlink" href="#optparse.Option.TYPED_ACTIONS" title="Permalink to this definition">¶</a></dt> <dd><p>“typed” actions are additionally listed here.</p> </dd></dl> <dl class="attribute"> <dt id="optparse.Option.ALWAYS_TYPED_ACTIONS"> <tt class="descclassname">Option.</tt><tt class="descname">ALWAYS_TYPED_ACTIONS</tt><a class="headerlink" href="#optparse.Option.ALWAYS_TYPED_ACTIONS" title="Permalink to this definition">¶</a></dt> <dd><p>Actions that always take a type (i.e. whose options always take a value) are additionally listed here. The only effect of this is that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> assigns the default type, <tt class="docutils literal"><span class="pre">"string"</span></tt>, to options with no explicit type whose action is listed in <a class="reference internal" href="#optparse.Option.ALWAYS_TYPED_ACTIONS" title="optparse.Option.ALWAYS_TYPED_ACTIONS"><tt class="xref py py-attr docutils literal"><span class="pre">ALWAYS_TYPED_ACTIONS</span></tt></a>.</p> </dd></dl> <p>In order to actually implement your new action, you must override Option’s <tt class="xref py py-meth docutils literal"><span class="pre">take_action()</span></tt> method and add a case that recognizes your action.</p> <p>For example, let’s add an <tt class="docutils literal"><span class="pre">"extend"</span></tt> action. This is similar to the standard <tt class="docutils literal"><span class="pre">"append"</span></tt> action, but instead of taking a single value from the command-line and appending it to an existing list, <tt class="docutils literal"><span class="pre">"extend"</span></tt> will take multiple values in a single comma-delimited string, and extend an existing list with them. That is, if <tt class="docutils literal"><span class="pre">--names</span></tt> is an <tt class="docutils literal"><span class="pre">"extend"</span></tt> option of type <tt class="docutils literal"><span class="pre">"string"</span></tt>, the command line</p> <div class="highlight-python"><pre>--names=foo,bar --names blah --names ding,dong</pre> </div> <p>would result in a list</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="s">"foo"</span><span class="p">,</span> <span class="s">"bar"</span><span class="p">,</span> <span class="s">"blah"</span><span class="p">,</span> <span class="s">"ding"</span><span class="p">,</span> <span class="s">"dong"</span><span class="p">]</span> </pre></div> </div> <p>Again we define a subclass of Option:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyOption</span><span class="p">(</span><span class="n">Option</span><span class="p">):</span> <span class="n">ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s">"extend"</span><span class="p">,)</span> <span class="n">STORE_ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">STORE_ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s">"extend"</span><span class="p">,)</span> <span class="n">TYPED_ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">TYPED_ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s">"extend"</span><span class="p">,)</span> <span class="n">ALWAYS_TYPED_ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">ALWAYS_TYPED_ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s">"extend"</span><span class="p">,)</span> <span class="k">def</span> <span class="nf">take_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span> <span class="k">if</span> <span class="n">action</span> <span class="o">==</span> <span class="s">"extend"</span><span class="p">:</span> <span class="n">lvalue</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">","</span><span class="p">)</span> <span class="n">values</span><span class="o">.</span><span class="n">ensure_value</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">lvalue</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">Option</span><span class="o">.</span><span class="n">take_action</span><span class="p">(</span> <span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">parser</span><span class="p">)</span> </pre></div> </div> <p>Features of note:</p> <ul> <li><p class="first"><tt class="docutils literal"><span class="pre">"extend"</span></tt> both expects a value on the command-line and stores that value somewhere, so it goes in both <a class="reference internal" href="#optparse.Option.STORE_ACTIONS" title="optparse.Option.STORE_ACTIONS"><tt class="xref py py-attr docutils literal"><span class="pre">STORE_ACTIONS</span></tt></a> and <a class="reference internal" href="#optparse.Option.TYPED_ACTIONS" title="optparse.Option.TYPED_ACTIONS"><tt class="xref py py-attr docutils literal"><span class="pre">TYPED_ACTIONS</span></tt></a>.</p> </li> <li><p class="first">to ensure that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> assigns the default type of <tt class="docutils literal"><span class="pre">"string"</span></tt> to <tt class="docutils literal"><span class="pre">"extend"</span></tt> actions, we put the <tt class="docutils literal"><span class="pre">"extend"</span></tt> action in <a class="reference internal" href="#optparse.Option.ALWAYS_TYPED_ACTIONS" title="optparse.Option.ALWAYS_TYPED_ACTIONS"><tt class="xref py py-attr docutils literal"><span class="pre">ALWAYS_TYPED_ACTIONS</span></tt></a> as well.</p> </li> <li><p class="first"><tt class="xref py py-meth docutils literal"><span class="pre">MyOption.take_action()</span></tt> implements just this one new action, and passes control back to <tt class="xref py py-meth docutils literal"><span class="pre">Option.take_action()</span></tt> for the standard <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> actions.</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">values</span></tt> is an instance of the optparse_parser.Values class, which provides the very useful <tt class="xref py py-meth docutils literal"><span class="pre">ensure_value()</span></tt> method. <tt class="xref py py-meth docutils literal"><span class="pre">ensure_value()</span></tt> is essentially <a class="reference internal" href="functions.html#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a> with a safety valve; it is called as</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">values</span><span class="o">.</span><span class="n">ensure_value</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> </pre></div> </div> <p>If the <tt class="docutils literal"><span class="pre">attr</span></tt> attribute of <tt class="docutils literal"><span class="pre">values</span></tt> doesn’t exist or is None, then ensure_value() first sets it to <tt class="docutils literal"><span class="pre">value</span></tt>, and then returns ‘value. This is very handy for actions like <tt class="docutils literal"><span class="pre">"extend"</span></tt>, <tt class="docutils literal"><span class="pre">"append"</span></tt>, and <tt class="docutils literal"><span class="pre">"count"</span></tt>, all of which accumulate data in a variable and expect that variable to be of a certain type (a list for the first two, an integer for the latter). Using <tt class="xref py py-meth docutils literal"><span class="pre">ensure_value()</span></tt> means that scripts using your action don’t have to worry about setting a default value for the option destinations in question; they can just leave the default as None and <tt class="xref py py-meth docutils literal"><span class="pre">ensure_value()</span></tt> will take care of getting it right when it’s needed.</p> </li> </ul> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">15.5. <tt class="docutils literal"><span class="pre">optparse</span></tt> — Parser for command line options</a><ul> <li><a class="reference internal" href="#background">15.5.1. Background</a><ul> <li><a class="reference internal" href="#terminology">15.5.1.1. Terminology</a></li> <li><a class="reference internal" href="#what-are-options-for">15.5.1.2. What are options for?</a></li> <li><a class="reference internal" href="#what-are-positional-arguments-for">15.5.1.3. What are positional arguments for?</a></li> </ul> </li> <li><a class="reference internal" href="#tutorial">15.5.2. Tutorial</a><ul> <li><a class="reference internal" href="#understanding-option-actions">15.5.2.1. Understanding option actions</a></li> <li><a class="reference internal" href="#the-store-action">15.5.2.2. The store action</a></li> <li><a class="reference internal" href="#handling-boolean-flag-options">15.5.2.3. Handling boolean (flag) options</a></li> <li><a class="reference internal" href="#other-actions">15.5.2.4. Other actions</a></li> <li><a class="reference internal" href="#default-values">15.5.2.5. Default values</a></li> <li><a class="reference internal" href="#generating-help">15.5.2.6. Generating help</a><ul> <li><a class="reference internal" href="#grouping-options">15.5.2.6.1. Grouping Options</a></li> </ul> </li> <li><a class="reference internal" href="#printing-a-version-string">15.5.2.7. Printing a version string</a></li> <li><a class="reference internal" href="#how-optparse-handles-errors">15.5.2.8. How <tt class="docutils literal"><span class="pre">optparse</span></tt> handles errors</a></li> <li><a class="reference internal" href="#putting-it-all-together">15.5.2.9. Putting it all together</a></li> </ul> </li> <li><a class="reference internal" href="#reference-guide">15.5.3. Reference Guide</a><ul> <li><a class="reference internal" href="#creating-the-parser">15.5.3.1. Creating the parser</a></li> <li><a class="reference internal" href="#populating-the-parser">15.5.3.2. Populating the parser</a></li> <li><a class="reference internal" href="#defining-options">15.5.3.3. Defining options</a></li> <li><a class="reference internal" href="#option-attributes">15.5.3.4. Option attributes</a></li> <li><a class="reference internal" href="#standard-option-actions">15.5.3.5. Standard option actions</a></li> <li><a class="reference internal" href="#standard-option-types">15.5.3.6. Standard option types</a></li> <li><a class="reference internal" href="#parsing-arguments">15.5.3.7. Parsing arguments</a></li> <li><a class="reference internal" href="#querying-and-manipulating-your-option-parser">15.5.3.8. Querying and manipulating your option parser</a></li> <li><a class="reference internal" href="#conflicts-between-options">15.5.3.9. Conflicts between options</a></li> <li><a class="reference internal" href="#cleanup">15.5.3.10. Cleanup</a></li> <li><a class="reference internal" href="#other-methods">15.5.3.11. Other methods</a></li> </ul> </li> <li><a class="reference internal" href="#option-callbacks">15.5.4. Option Callbacks</a><ul> <li><a class="reference internal" href="#defining-a-callback-option">15.5.4.1. Defining a callback option</a></li> <li><a class="reference internal" href="#how-callbacks-are-called">15.5.4.2. How callbacks are called</a></li> <li><a class="reference internal" href="#raising-errors-in-a-callback">15.5.4.3. Raising errors in a callback</a></li> <li><a class="reference internal" href="#callback-example-1-trivial-callback">15.5.4.4. Callback example 1: trivial callback</a></li> <li><a class="reference internal" href="#callback-example-2-check-option-order">15.5.4.5. Callback example 2: check option order</a></li> <li><a class="reference internal" href="#callback-example-3-check-option-order-generalized">15.5.4.6. Callback example 3: check option order (generalized)</a></li> <li><a class="reference internal" href="#callback-example-4-check-arbitrary-condition">15.5.4.7. Callback example 4: check arbitrary condition</a></li> <li><a class="reference internal" href="#callback-example-5-fixed-arguments">15.5.4.8. Callback example 5: fixed arguments</a></li> <li><a class="reference internal" href="#callback-example-6-variable-arguments">15.5.4.9. Callback example 6: variable arguments</a></li> </ul> </li> <li><a class="reference internal" href="#extending-optparse">15.5.5. Extending <tt class="docutils literal"><span class="pre">optparse</span></tt></a><ul> <li><a class="reference internal" href="#adding-new-types">15.5.5.1. Adding new types</a></li> <li><a class="reference internal" href="#adding-new-actions">15.5.5.2. Adding new actions</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="argparse.html" title="previous chapter">15.4. <tt class="docutils literal"><span class="pre">argparse</span></tt> — Parser for command-line options, arguments and sub-commands</a></p> <h4>Next topic</h4> <p class="topless"><a href="getopt.html" title="next chapter">15.6. <tt class="docutils literal"><span class="pre">getopt</span></tt> — C-style parser for command line options</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/optparse.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="getopt.html" title="15.6. getopt — C-style parser for command line options" >next</a> |</li> <li class="right" > <a href="argparse.html" title="15.4. argparse — Parser for command-line options, arguments and sub-commands" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="allos.html" >15. Generic Operating System Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close