Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex


14.3.3.1 Creating the parser

The first step in usingoptparse is to create an OptionParser instance:

parser = OptionParser(...)

The OptionParser constructor has no required arguments, but a number ofoptional keyword arguments. You should always pass them as keywordarguments, i.e. do not rely on the order in which the arguments aredeclared.

usage (default:"%prog [options]")
The usage summary to print when your program is run incorrectly orwith a help option. Whenoptparse prints the usage string, it expands%prog toos.path.basename(sys.argv[0]) (or toprog ifyou passed that keyword argument). To suppress a usage message,pass the special valueoptparse.SUPPRESS_USAGE.
option_list (default:[])
A list of Option objects to populate the parser with. The optionsinoption_list are added after any options instandard_option_list (a class attribute that may be set byOptionParser subclasses), but before any version or help options.Deprecated; useadd_option() after creating the parser instead.
option_class (default: optparse.Option)
Class to use when adding options to the parser inadd_option().
version (default:None)
A version string to print when the user supplies a version option.If you supply a true value forversion,optparse automatically addsa version option with the single option string--version. Thesubstring"%prog" is expanded the same as forusage.
conflict_handler (default:"error")
Specifies what to do when options with conflicting option stringsare added to the parser; see section 14.3.3, Conflicts between options.
description (default:None)
A paragraph of text giving a brief overview of your program.optparsereformats this paragraph to fit the current terminal width andprints it when the user requests help (afterusage, but beforethe list of options).
formatter (default: a new IndentedHelpFormatter)
An instance of optparse.HelpFormatter that will be used forprinting help text.optparse provides two concrete classes for thispurpose: IndentedHelpFormatter and TitledHelpFormatter.
add_help_option (default:True)
If true,optparse will add a help option (with option strings"-h"and--help) to the parser.
prog
The string to use when expanding"%prog" inusage andversion instead ofos.path.basename(sys.argv[0]).


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp