diff options
| author | Mark McLoughlin <markmc@redhat.com> | 2012-01-23 10:42:07 +0000 |
|---|---|---|
| committer | Mark McLoughlin <markmc@redhat.com> | 2012-01-23 10:42:07 +0000 |
| commit | 8d7224325f56b624a3059f28983b090725d1fb2a (patch) | |
| tree | faf11d814bbfd699dffa65a668e293a5191a570e /openstack | |
| parent | 35aa7a3485fe8ca19b4f7e4af6517b668404bc48 (diff) | |
| download | oslo-8d7224325f56b624a3059f28983b090725d1fb2a.tar.gz oslo-8d7224325f56b624a3059f28983b090725d1fb2a.tar.xz oslo-8d7224325f56b624a3059f28983b090725d1fb2a.zip | |
Add support to cfg for disabling interspersed args
Implements blueprint cfg-disable-interspersed-args
Nova currently relies on cfg being implemented with optparse because it
uses optparse's disable_interspersed_args()
The use case for this is if you do:
$> nova-manage --verbose create --project foo --user bar
you want invoking ConfigOpts() to return:
['create', '--project', 'foo', '--user', 'bar']
as the "extra" args rather than aborting when it doesn't recognize the
--project arg.
This is a reasonable use case for cfg to support and it should just have
{disable,enable}_interspersed_args() methods.
If we ever switch from optparse to argparse, we'll do something like this:
parser.add_argument('--verbose')
...
parser.add_argument(
'extra_args',
nargs=argparse.REMAINDER if disable_interspersed_args else '*')
...
ns = parser.parse_args(...)
extra_args = ns.extra_args
i.e. we will need an 'extra_args' multi-value positional argument in any
case and we'll just pass nargs=REMAINDER if we want trailing options to
be included in the extra args.
Change-Id: I3ecb7dc18230327cf5aaaa7d832224e64aafa40c
Diffstat (limited to 'openstack')
| -rw-r--r-- | openstack/common/cfg.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/openstack/common/cfg.py b/openstack/common/cfg.py index 891fcbe..eacd180 100644 --- a/openstack/common/cfg.py +++ b/openstack/common/cfg.py @@ -193,6 +193,22 @@ Option values may reference other values using PEP 292 string substitution: ] Note that interpolation can be avoided by using '$$'. + +For command line utilities that dispatch to other command line utilities, the +disable_interspersed_args() method is available. If this this method is called, +then parsing e.g. + + script --verbose cmd --debug /tmp/mything + +will no longer return: + + ['cmd', '/tmp/mything'] + +as the leftover arguments, but will instead return: + + ['cmd', '--debug', '/tmp/mything'] + +i.e. argument parsing is stopped at the first non-option argument. """ import sys @@ -875,6 +891,31 @@ class ConfigOpts(object): opt_info = self._get_opt_info(name, group) opt_info['default'] = default + def disable_interspersed_args(self): + """Set parsing to stop on the first non-option. + + If this this method is called, then parsing e.g. + + script --verbose cmd --debug /tmp/mything + + will no longer return: + + ['cmd', '/tmp/mything'] + + as the leftover arguments, but will instead return: + + ['cmd', '--debug', '/tmp/mything'] + + i.e. argument parsing is stopped at the first non-option argument. + """ + self._oparser.disable_interspersed_args() + + def enable_interspersed_args(self): + """Set parsing to not stop on the first non-option. + + This it the default behaviour.""" + self._oparser.enable_interspersed_args() + def log_opt_values(self, logger, lvl): """Log the value of all registered opts. |
