Originally, the
argparse
module had attempted to maintain compatibility with optparse
. However, optparse
was difficult to extend transparently, particularly with the changes required to support the new nargs=
specifiers and better usage messages. When most everything in optparse
had either been copy-pasted over or monkey-patched, it no longer seemed practical to try to maintain the backwards compatibility.- Handling positional arguments.
- Supporting sub-commands.
- Allowing alternative option prefixes like
+
and/
. - Handling zero-or-more and one-or-more style arguments.
- Producing more informative usage messages.
- Providing a much simpler interface for custom
type
andaction
.
- Replace all
optparse.OptionParser.add_option()
calls withArgumentParser.add_argument()
calls. - Replace
(options, args) = parser.parse_args()
withargs = parser.parse_args()
and add additionalArgumentParser.add_argument()
calls for the positional arguments. Keep in mind that what was previously calledoptions
, now inargparse
context is calledargs
. - Replace callback actions and the
callback_*
keyword arguments withtype
oraction
arguments. - Replace string names for
type
keyword arguments with the corresponding type objects (e.g. int, float, complex, etc). - Replace
optparse.Values
withNamespace
andoptparse.OptionError
andoptparse.OptionValueError
withArgumentError
. - Replace strings with implicit arguments such as
%default
or%prog
with the standard Python syntax to use dictionaries to format strings, that is,%(default)s
and%(prog)s
. - Replace the OptionParser constructor
version
argument with a call toparser.add_argument('--version', action='version', version='
.version>')
Comments
Post a Comment
https://gengwg.blogspot.com/