1
1
.. currentmodule :: argparse
2
2
3
3
.. _upgrading-optparse-code :
4
+ .. _migrating-optparse-code :
4
5
5
- ==========================
6
- Upgrading optparse code
7
- ==========================
6
+ ============================================
7
+ Migrating `` optparse `` code to `` argparse ``
8
+ ============================================
8
9
9
- Originally, the :mod: `argparse ` module had attempted to maintain compatibility
10
- with :mod: `optparse `. However, :mod: `optparse ` was difficult to extend
11
- transparently, particularly with the changes required to support
12
- ``nargs= `` specifiers and better usage messages. When most everything in
13
- :mod: `optparse ` had either been copy-pasted over or monkey-patched, it no
14
- longer seemed practical to try to maintain the backwards compatibility.
15
-
16
- The :mod: `argparse ` module improves on the :mod: `optparse `
17
- module in a number of ways including:
10
+ The :mod: `argparse ` module offers several higher level features not natively
11
+ provided by the :mod: `optparse ` module, including:
18
12
19
13
* Handling positional arguments.
20
14
* Supporting subcommands.
@@ -23,7 +17,23 @@ module in a number of ways including:
23
17
* Producing more informative usage messages.
24
18
* Providing a much simpler interface for custom ``type `` and ``action ``.
25
19
26
- A partial upgrade path from :mod: `optparse ` to :mod: `argparse `:
20
+ Originally, the :mod: `argparse ` module attempted to maintain compatibility
21
+ with :mod: `optparse `. However, the fundamental design differences between
22
+ supporting declarative command line option processing (while leaving positional
23
+ argument processing to application code), and supporting both named options
24
+ and positional arguments in the declarative interface mean that the
25
+ API has diverged from that of ``optparse `` over time.
26
+
27
+ As described in :ref: `choosing-an-argument-parser `, applications that are
28
+ currently using :mod: `optparse ` and are happy with the way it works can
29
+ just continue to use ``optparse ``.
30
+
31
+ Application developers that are considering migrating should also review
32
+ the list of intrinsic behavioural differences described in that section
33
+ before deciding whether or not migration is desirable.
34
+
35
+ For applications that do choose to migrate from :mod: `optparse ` to :mod: `argparse `,
36
+ the following suggestions should be helpful:
27
37
28
38
* Replace all :meth: `optparse.OptionParser.add_option ` calls with
29
39
:meth: `ArgumentParser.add_argument ` calls.
0 commit comments