Command-Line Modules

optparse

Legacy command-line option parsing library, deprecated in favor of argparse for new applications. It returns an options object and remaining positional operands. Existing code may still use it; migrate deliberately because parsing behavior is not identical.

Formal Syntax & Structure

python syntaxpython
import optparse

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
from optparse import OptionParserparser = OptionParser()parser.add_option("-n", type="int", dest="count", default=1)options, remaining = parser.parse_args(["-n", "3", "file"])print(options.count, remaining)
Python Reference