Utility method for boolean argument

In the recent commit, True/False of boolean argument
became case-insensitve and this code is copy-and-paste'ed
in several places.

Now there are several number of patches which add explicit
arguments for *-update or *-list and the code of boolean
argumetns like below will be used more than now.

This commit add a utility method to register such boolean opt.

       parser.add_argument(
           '--enabled',
           dest='enabled', metavar='{True,False}',
           choices=['True', 'true', 'False', 'false'],
           help=_('Whether to enable or disable this rule.'),
           default=argparse.SUPPRESS)

Change-Id: I9575eeef32154a8b92589c2cc7889803216bddb2
This commit is contained in:
Akihiro Motoki
2015-01-28 03:02:46 +09:00
committed by Akihiro Motoki
parent 2dce00b41d
commit b0923a3e12
4 changed files with 26 additions and 20 deletions

View File

@@ -17,6 +17,7 @@
"""Utilities and helper functions."""
import argparse
import logging
import os
@@ -158,3 +159,15 @@ def safe_encode_dict(data):
return (k, _safe_encode_without_obj(v))
return dict(list(map(_encode_item, data.items())))
def add_boolean_argument(parser, name, **kwargs):
for keyword in ('metavar', 'choices'):
kwargs.pop(keyword, None)
default = kwargs.pop('default', argparse.SUPPRESS)
parser.add_argument(
name,
metavar='{True,False}',
choices=['True', 'true', 'False', 'false'],
default=default,
**kwargs)

View File

@@ -18,6 +18,7 @@
import argparse
from neutronclient.common import utils
from neutronclient.i18n import _
from neutronclient.neutron import v2_0 as neutronv20
@@ -95,12 +96,9 @@ class CreateFirewallRule(neutronv20.CreateCommand):
'--destination-port',
help=_('Destination port (integer in [1, 65535] or range in '
'a:b).'))
parser.add_argument(
'--enabled',
dest='enabled', metavar='{True,False}',
choices=['True', 'true', 'False', 'false'],
help=_('Whether to enable or disable this rule.'),
default=argparse.SUPPRESS)
utils.add_boolean_argument(
parser, '--enabled', dest='enabled',
help=_('Whether to enable or disable this rule.'))
parser.add_argument(
'--protocol', choices=['tcp', 'udp', 'icmp', 'any'],
required=True,

View File

@@ -14,6 +14,7 @@
# under the License.
from neutronclient.common import exceptions
from neutronclient.common import utils
from neutronclient.common import validators
from neutronclient.i18n import _
from neutronclient.neutron import v2_0 as neutronV20
@@ -70,9 +71,8 @@ class PacketFilterOptionMixin(object):
dest='admin_state', action='store_false',
help=_('Set Admin State Up to false'))
else:
parser.add_argument(
'--admin-state', metavar='{True,False}',
choices=['True', 'true', 'False', 'false'],
utils.add_boolean_argument(
parser, '--admin-state',
help=_('Set a value of Admin State Up'))
parser.add_argument(
@@ -207,7 +207,7 @@ class UpdatePacketFilter(PacketFilterOptionMixin,
self.validate_fields(parsed_args)
body = {}
if parsed_args.admin_state:
if hasattr(parsed_args, 'admin_state'):
body['admin_state_up'] = (parsed_args.admin_state == 'True')
# fields which allows None

View File

@@ -21,6 +21,7 @@ import argparse
from oslo.serialization import jsonutils
from neutronclient.common import exceptions
from neutronclient.common import utils
from neutronclient.i18n import _
from neutronclient.neutron import v2_0 as neutronV20
@@ -66,17 +67,11 @@ class CreateRouter(neutronV20.CreateCommand):
parser.add_argument(
'name', metavar='NAME',
help=_('Name of router to create.'))
parser.add_argument(
'--distributed',
dest='distributed', metavar='{True,False}',
choices=['True', 'true', 'False', 'false'],
default=argparse.SUPPRESS,
utils.add_boolean_argument(
parser, '--distributed', dest='distributed',
help=_('Create a distributed router.'))
parser.add_argument(
'--ha',
dest='ha', metavar='{True,False}',
choices=['True', 'true', 'false', 'False'],
default=argparse.SUPPRESS,
utils.add_boolean_argument(
parser, '--ha', dest='ha',
help=_('Create a highly available router.'))
def args2body(self, parsed_args):