From 2b9ad7bdb94d54a46068bd8864fb8bb96161bcf5 Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Mon, 26 Nov 2012 06:47:52 +0000 Subject: Fix set_default() with boolean CLI options Porting to argparse broke set_default() with boolean CLI options. The new test case shows this borkage. The issue is that, by default, argparse differs subtly from optparse in its handling of defaults for boolean options. Compare: >>> p = optparse.OptionParser() >>> p.add_option('--foo', action='store_true') >>> p.add_option('--bar', action='store_true', default=False) >>> p.parse_args([]) (, []) to: >>> p = argparse.ArgumentParser() >>> p.add_argument('--foo', action='store_true') >>> p.add_argument('--bar', action='store_true', default=False) >>> p.add_argument('--blaa', action='store_true', default=None) >>> p.parse_args([]) Namespace(bar=False, blaa=None, foo=False) i.e. unless you specify a default for a boolean option, optparse defaults to None whereas argparse defaults to False. To get the same optparse behaviour with argparse, you need default=None. Change-Id: Ifc92a834c4ba59e939d80ac5de24d7051232f5b5 --- openstack/common/cfg.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'openstack') diff --git a/openstack/common/cfg.py b/openstack/common/cfg.py index 28baa04..9be96cc 100644 --- a/openstack/common/cfg.py +++ b/openstack/common/cfg.py @@ -632,7 +632,8 @@ class Opt(object): kwargs['dest'] = dest else: kwargs['nargs'] = '?' - kwargs.update({'metavar': self.metavar, + kwargs.update({'default': None, + 'metavar': self.metavar, 'help': self.help, }) return kwargs -- cgit