On 16 Mai 2006, [EMAIL PROTECTED] wrote:

> Is there a way to leverage optionparser so it can accept input from both
> command line and a configuration file?
>
> Current code block is:
>
>     #
>     # Parse command line options and automatically build
>     # help/usage display
>     #
>     parser = OptionParser()
>     parser.add_option("-m","--qmanager", dest="qmanager",
>                 help="\t\tQueue Manager to inquire against"),
>     parser.add_option("-q","--queue", dest="queue",
>                 help="\t\tQueue the message will be sent to"),
>     parser.add_option("-t","--to", dest="mto",
>                 help="\t\taddress any mail messages will be sent to")
>     (options, args) = parser.parse_args()

parse_args() can take the list to parse as argument; per default it
parses sys.argv[1:].  So you could build a list of options from your
config file and call parse_args() like that:

       (options, args) = parser.parse_args(configlst + sys.argv[1:])

Like that options given on the command line would overwrite options from
the config file.

Another approach could be to define default values for the variables.
Let's take your above example.

parser.add_option("-m","--qmanager", dest="qmanager",
                  help="\t\tQueue Manager to inquire against"),
parser.add_option("-q","--queue", dest="queue",
                  help="\t\tQueue the message will be sent to"),
parser.add_option("-t","--to", dest="mto",
                  help="\t\taddress any mail messages will be sent to")

Then you would read the values from the config file and call
parser.set_defaults() with the values.

parser.set_defaults(qmanager="Foo", queue="Bar", mto="[EMAIL PROTECTED]")


   Karl
-- 
Please do *not* send copies of replies to me.
I read the list
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to