开发者

In optparse module - command line option parser, how to confirm if an option was not provided?

From Python docs:

"Option.dest : If the option’s action implies writing or modifying a value somewhere, this tells optparse where to write it: dest names an attribute of the options object that optparse builds as it parses the command line."

Can we put some check on the name of the attribute (dest) to check if it's value was provided? Say, I want to perform some action to determine it's value when no value is provided for it in CLI, as I don't have a fixed开发者_JS百科 default value.

Checking against 'None' does not work.


You could use a default value of None for such options, which cannot be entered on the command line. Then you can check like

if opts.optional_value is None:
    # action for option not given
else:
    # use value from command line
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜