oslo.config print_help() and print_usage() crash or are not "useful"

Bug #1196601 reported by Andrew Forrest
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
oslo-incubator
Fix Released
Medium
Davanum Srinivas (DIMS)

Bug Description

Adapting the "common usage pattern in OpenStack" cited in [1]:

    from oslo.config import cfg

    opts = [
        cfg.StrOpt('bind_host', default='0.0.0.0'),
        cfg.IntOpt('bind_port', default=9292),
    ]

    CONF = cfg.CONF
    CONF.register_opts(opts)

Attempts to call "CONF.print_usage()" or "CONF.print_help()" after this point result in a traceback owing to the fact that the private member _oparser of the CONF object (class ConfigOpts()) is not yet set:

    Traceback (most recent call last):
      File "demo.py", line 19, in <module>
        CONF.print_help()
      File "[...]/python2.7/site-packages/oslo/config/cfg.py", line 1603, in print_help
        self._oparser.print_help(file)
    AttributeError: 'NoneType' object has no attribute 'print_help'

This member is set via three public methods:
    + ConfigOpts.reset() or ConfigOpts.clear() [reset calls clear]
    + calling the ConfigOpts object itself

In the first bullet immediately above there is not much beyond the default -h to report in the help or usage message afterwards. In the second case, a call to the object itself triggers the processing of the command line options which will then respond to the necessity of a help or usage message itself precluding the need (or even the opportunity) for the main program to call these functions.

---
[1] http://docs.openstack.org/developer/oslo.config/api/oslo.config.cfg.html#module-oslo.config.cfg

summary: - oslo.config print_help and print_usage not "useful"
+ oslo.config print_help() and print_usage() crash or are not "useful"
Changed in oslo:
milestone: none → havana-2
assignee: nobody → Davanum Srinivas (DIMS) (dims-v)
importance: Undecided → Medium
status: New → Confirmed
Revision history for this message
Mark McLoughlin (markmc) wrote :

Andrew - could you go into some more detail about why exactly you want to print help/usage before parsing the command line?

Changed in oslo:
status: Confirmed → Incomplete
Revision history for this message
Andrew Forrest (forrest-r) wrote :

It's not that I want to do those things before parsing the command line.

I was converting a program with its own help and usage message routines to use oslo.config and I noticed these facilities were available and I tried to use them but, as you can see, that isn't possible: either the program crashes because _oparser is not yet set or it exits under the control of ConfigOpts.__call__() after producing its own help or usage message (as appropriate) precluding a future call to print_help() or print_usage().

If these methods were removed future oslo.config users would not have to rediscover this issue.

Does that help?

Revision history for this message
Mark McLoughlin (markmc) wrote :

Ok, it sounds like your concern is mostly that this isn't obvious. We've added explicit error handling to make it more clear: https://review.openstack.org/#/c/35326/

Changed in oslo:
status: Incomplete → Fix Committed
Revision history for this message
Andrew Forrest (forrest-r) wrote :

I appreciate you trying to understand my concern but that's not it.

Apparently I don't understand the use-case for these functions. Would you please indulge me once more? Disregard the AttributeError case for the moment, when (after a call to the ConfigOpts object) would it ever be useful to call either of these methods?

Revision history for this message
Mark McLoughlin (markmc) wrote :

You'd use it after calling CONF() to parse the arguments - if, for example, the user supplied incompatible values

Revision history for this message
Mark McLoughlin (markmc) wrote :

Basically, we don't initialize the argument parser at until CONF() is called because not all CLI options may have been registered before then

Revision history for this message
Andrew Forrest (forrest-r) wrote :

Re #5: okay, I can see that now.

Sorry for suggesting that these functions might not be useful.

Thierry Carrez (ttx)
Changed in oslo:
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in oslo:
milestone: havana-2 → 2013.2
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.