registering a cli opt on a filter object doesn't actually work

Bug #1366946 reported by Doug Hellmann
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
oslo.config
Fix Released
Medium
Tong Da

Bug Description

Registering a CLI option using cfgfilter doesn't work. This is logical, but we have an API for doing it and I think we probably want to just delete it.

Given this program:

  #!/usr/bin/env python

  from oslo.config import cfg
  from oslo.config import cfgfilter

  import sys

  c = cfg.CONF
  c(sys.argv[1:])

  f = cfgfilter.ConfigFilter(c)
  f.register_cli_opt(
      cfg.BoolOpt('myflag',
                  default=False,
                  help='turn on myflag',
                  )
  )

  print f.myflag

Running it I get:

  Traceback (most recent call last):
    File "./cli_with_filter.py", line 15, in <module>
      help='turn on myflag',
    File "/home/dhellmann/repos/openstack/oslo.config/.tox/py27/local/lib/python2.7/site-packages/oslo/config/cfgfilter.py", line 215, in register_cli_opt
      return self._fconf.register_cli_opt(opt, group)
    File "/home/dhellmann/repos/openstack/oslo.config/.tox/py27/local/lib/python2.7/site-packages/oslo/config/cfg.py", line 1613, in __inner
      result = f(self, *args, **kwargs)
    File "/home/dhellmann/repos/openstack/oslo.config/.tox/py27/local/lib/python2.7/site-packages/oslo/config/cfg.py", line 1769, in register_cli_opt
      raise ArgsAlreadyParsedError("cannot register CLI option")
  oslo.config.cfg.ArgsAlreadyParsedError: arguments already parsed: cannot register CLI option

If I move the call to parse the CLI arguments to a point after the filter option is defined:

  #!/usr/bin/env python

  from oslo.config import cfg
  from oslo.config import cfgfilter

  import sys

  c = cfg.CONF

  f = cfgfilter.ConfigFilter(c)
  f.register_cli_opt(
      cfg.BoolOpt('myflag',
                  default=False,
                  help='turn on myflag',
                  )
  )

  c(sys.argv[1:])
  print f.myflag

Then running the command without any options gives me the option value, but trying to actually use the flag gives an error:

  usage: cli_with_filter.py [-h] [--config-dir DIR] [--config-file PATH]
                            [--version]
  cli_with_filter.py: error: unrecognized arguments: --myflag

Changed in oslo.config:
importance: Undecided → Medium
status: New → Triaged
Tong Da (tongda)
Changed in oslo.config:
assignee: nobody → Tong Da (tongda)
Revision history for this message
Tong Da (tongda) wrote :

It seems that registering cli opt after the original ConfigOpts is parsed is meaningless. So I think it would make more sense to throw an exception if the registered opt is an unknown opt, otherwise, it should behave similar to import_opt() when registering a known opt.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to oslo.config (master)

Fix proposed to branch: master
Review: https://review.openstack.org/171745

Changed in oslo.config:
status: Triaged → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Fix proposed to branch: master
Review: https://review.openstack.org/171908

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Change abandoned on oslo.config (master)

Change abandoned by Davanum Srinivas (dims) (<email address hidden>) on branch: master
Review: https://review.openstack.org/171745

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to oslo.config (master)

Reviewed: https://review.openstack.org/171908
Committed: https://git.openstack.org/cgit/openstack/oslo.config/commit/?id=f1f972aeb7e7e6f672b55fb8eefc37de71f5d551
Submitter: Jenkins
Branch: master

commit f1f972aeb7e7e6f672b55fb8eefc37de71f5d551
Author: Tong Damon Da <email address hidden>
Date: Thu Apr 9 13:19:28 2015 +0800

    make registering a cli opt on a filter object work.

    Change the behaviour of ConfigFilter.register_cli_opt to:

    1. If the opt is already registered before parsing, then registering
    just import it.
    2. if the opt is not registered before, then raise an exception named

    CliOptRegisteredError.
    By adding this modification, the behaviour of
    ConfigFilter.register_cli_opt looks more consistent to
    ConfigOpts.register_cli_opt. The solution to the question mentioned in
    Bug#1366946 should be like this:

    from oslo_config import cfg
    from oslo_config import cfgfilter

    import sys

    c = cfg.CONF
    c.register_cli_opt(
    cfg.BoolOpt('myflag',
    default=False,
    help='turn on myflag',
    )
    )
    c(sys.argv[1:])

    f = cfgfilter.ConfigFilter(c)
    f.register_cli_opt(
    cfg.BoolOpt('myflag',
    default=False,
    help='turn on myflag',
    )
    )

    print f.myflag

    Closes-Bug: #1366946
    Change-Id: I94df9409f72807461370b4aaf8eb2543c52a89bb

Changed in oslo.config:
status: In Progress → Fix Committed
Changed in oslo.config:
milestone: none → 1.12.0
status: Fix Committed → Fix Released
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.