Details
-
Improvement
-
Status: Resolved
-
Normal
-
Resolution: Fixed
-
4.0.11, 4.1.4, 5.0-alpha1, 5.0
-
None
-
Code Clarity
-
Normal
-
All
-
None
-
Description
Argparse is described in PEP 389 – argparse - New Command Line Parsing Module
A partial upgrade path from optparse to argparse:
https://docs.python.org/3/library/argparse.html#upgrading-optparse-code
- Replace all optparse.OptionParser.add_option() calls with ArgumentParser.add_argument() calls.
- Replace (options, args) = parser.parse_args() with args = parser.parse_args() and add additional ArgumentParser.add_argument() calls for the positional arguments. Keep in mind that what was previously called options, now in the argparse context is called args.
- Replace optparse.OptionParser.disable_interspersed_args() by using parse_intermixed_args() instead of parse_args().
- Replace callback actions and the callback_* keyword arguments with type or action arguments.
- Replace string names for type keyword arguments with the corresponding type objects (e.g. int, float, complex, etc).
- Replace optparse.Values with Namespace and optparse.OptionError and optparse.OptionValueError with ArgumentError.
- 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 to parser.add_argument('--version', action='version', version='<the version>').