Help messages: specify which options are required
Closes-Bug: #1223283 Change-Id: I080fa73bd45a1f9f442dbcdfa65fdc24e30521da
This commit is contained in:
@@ -27,6 +27,13 @@ from ceilometerclient.openstack.common import importutils
|
|||||||
# Decorator for cli-args
|
# Decorator for cli-args
|
||||||
def arg(*args, **kwargs):
|
def arg(*args, **kwargs):
|
||||||
def _decorator(func):
|
def _decorator(func):
|
||||||
|
if 'help' in kwargs:
|
||||||
|
if 'default' in kwargs:
|
||||||
|
kwargs['help'] += " Defaults to %s." % kwargs['default']
|
||||||
|
required = kwargs.get('required', False)
|
||||||
|
if required:
|
||||||
|
kwargs['help'] += " Required."
|
||||||
|
|
||||||
# Because of the sematics of decorator composition if we just append
|
# Because of the sematics of decorator composition if we just append
|
||||||
# to the options list positional options will appear to be backwards.
|
# to the options list positional options will appear to be backwards.
|
||||||
func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
|
func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
|
||||||
|
|||||||
@@ -74,3 +74,22 @@ class UtilsTest(test_utils.BaseTestCase):
|
|||||||
'statictic': 'avg',
|
'statictic': 'avg',
|
||||||
'comparison_operator': 'or'},
|
'comparison_operator': 'or'},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
def test_arg(self):
|
||||||
|
@utils.arg(help="not_required_no_default.")
|
||||||
|
def not_required_no_default():
|
||||||
|
pass
|
||||||
|
_, args = not_required_no_default.__dict__['arguments'][0]
|
||||||
|
self.assertEqual(args['help'], "not_required_no_default.")
|
||||||
|
|
||||||
|
@utils.arg(required=True, help="required_no_default.")
|
||||||
|
def required_no_default():
|
||||||
|
pass
|
||||||
|
_, args = required_no_default.__dict__['arguments'][0]
|
||||||
|
self.assertEqual(args['help'], "required_no_default. Required.")
|
||||||
|
|
||||||
|
@utils.arg(default=42, help="not_required_default.")
|
||||||
|
def not_required_default():
|
||||||
|
pass
|
||||||
|
_, args = not_required_default.__dict__['arguments'][0]
|
||||||
|
self.assertEqual(args['help'], "not_required_default. Defaults to 42.")
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ OPERATORS_STRING = dict(gt='>', ge='>=',
|
|||||||
|
|
||||||
@utils.arg('-q', '--query', metavar='<QUERY>',
|
@utils.arg('-q', '--query', metavar='<QUERY>',
|
||||||
help='key[op]value; list.')
|
help='key[op]value; list.')
|
||||||
@utils.arg('-m', '--meter', metavar='<NAME>',
|
@utils.arg('-m', '--meter', metavar='<NAME>', required=True,
|
||||||
help='Name of meter to show samples for.')
|
help='Name of meter to show samples for.')
|
||||||
@utils.arg('-p', '--period', metavar='<PERIOD>',
|
@utils.arg('-p', '--period', metavar='<PERIOD>',
|
||||||
help='Period in seconds over which to group samples.')
|
help='Period in seconds over which to group samples.')
|
||||||
@@ -44,8 +44,6 @@ def do_statistics(cc, args):
|
|||||||
fields = {'meter_name': args.meter,
|
fields = {'meter_name': args.meter,
|
||||||
'q': options.cli_to_array(args.query),
|
'q': options.cli_to_array(args.query),
|
||||||
'period': args.period}
|
'period': args.period}
|
||||||
if args.meter is None:
|
|
||||||
raise exc.CommandError('Meter name not provided (-m <meter name>)')
|
|
||||||
try:
|
try:
|
||||||
statistics = cc.statistics.list(**fields)
|
statistics = cc.statistics.list(**fields)
|
||||||
except exc.HTTPNotFound:
|
except exc.HTTPNotFound:
|
||||||
@@ -62,14 +60,12 @@ def do_statistics(cc, args):
|
|||||||
|
|
||||||
@utils.arg('-q', '--query', metavar='<QUERY>',
|
@utils.arg('-q', '--query', metavar='<QUERY>',
|
||||||
help='key[op]value; list.')
|
help='key[op]value; list.')
|
||||||
@utils.arg('-m', '--meter', metavar='<NAME>',
|
@utils.arg('-m', '--meter', metavar='<NAME>', required=True,
|
||||||
help='Name of meter to show samples for.')
|
help='Name of meter to show samples for.')
|
||||||
def do_sample_list(cc, args):
|
def do_sample_list(cc, args):
|
||||||
'''List the samples for this meters.'''
|
'''List the samples for this meters.'''
|
||||||
fields = {'meter_name': args.meter,
|
fields = {'meter_name': args.meter,
|
||||||
'q': options.cli_to_array(args.query)}
|
'q': options.cli_to_array(args.query)}
|
||||||
if args.meter is None:
|
|
||||||
raise exc.CommandError('Meter name not provided (-m <meter name>)')
|
|
||||||
try:
|
try:
|
||||||
samples = cc.samples.list(**fields)
|
samples = cc.samples.list(**fields)
|
||||||
except exc.HTTPNotFound:
|
except exc.HTTPNotFound:
|
||||||
@@ -89,15 +85,15 @@ def do_sample_list(cc, args):
|
|||||||
@utils.arg('--user-id', metavar='<USER_ID>',
|
@utils.arg('--user-id', metavar='<USER_ID>',
|
||||||
help='User to associate with sample '
|
help='User to associate with sample '
|
||||||
'(only settable by admin users)')
|
'(only settable by admin users)')
|
||||||
@utils.arg('-r', '--resource-id', metavar='<RESOURCE_ID>',
|
@utils.arg('-r', '--resource-id', metavar='<RESOURCE_ID>', required=True,
|
||||||
help='ID of the resource.')
|
help='ID of the resource.')
|
||||||
@utils.arg('-m', '--meter-name', metavar='<METER_NAME>',
|
@utils.arg('-m', '--meter-name', metavar='<METER_NAME>',
|
||||||
help='the meter name')
|
help='the meter name')
|
||||||
@utils.arg('--meter-type', metavar='<METER_TYPE>',
|
@utils.arg('--meter-type', metavar='<METER_TYPE>', required=True,
|
||||||
help='the meter type')
|
help='the meter type')
|
||||||
@utils.arg('--meter-unit', metavar='<METER_UNIT>',
|
@utils.arg('--meter-unit', metavar='<METER_UNIT>', required=True,
|
||||||
help='the meter unit')
|
help='the meter unit')
|
||||||
@utils.arg('--sample-volume', metavar='<SAMPLE_VOLUME>',
|
@utils.arg('--sample-volume', metavar='<SAMPLE_VOLUME>', required=True,
|
||||||
help='The sample volume')
|
help='The sample volume')
|
||||||
@utils.arg('--resource-metadata', metavar='<RESOURCE_METADATA>',
|
@utils.arg('--resource-metadata', metavar='<RESOURCE_METADATA>',
|
||||||
help='resource metadata')
|
help='resource metadata')
|
||||||
@@ -188,12 +184,10 @@ def _display_alarm(alarm):
|
|||||||
utils.print_dict(data, wrap=72)
|
utils.print_dict(data, wrap=72)
|
||||||
|
|
||||||
|
|
||||||
@utils.arg('-a', '--alarm_id', metavar='<ALARM_ID>',
|
@utils.arg('-a', '--alarm_id', metavar='<ALARM_ID>', required=True,
|
||||||
help='ID of the alarm to show.')
|
help='ID of the alarm to show.')
|
||||||
def do_alarm_show(cc, args={}):
|
def do_alarm_show(cc, args={}):
|
||||||
'''Show an alarm.'''
|
'''Show an alarm.'''
|
||||||
if args.alarm_id is None:
|
|
||||||
raise exc.CommandError('Alarm ID not provided (-a <alarm id>)')
|
|
||||||
try:
|
try:
|
||||||
alarm = cc.alarms.get(args.alarm_id)
|
alarm = cc.alarms.get(args.alarm_id)
|
||||||
except exc.HTTPNotFound:
|
except exc.HTTPNotFound:
|
||||||
@@ -416,8 +410,6 @@ def do_alarm_combination_update(cc, args={}):
|
|||||||
help='ID of the alarm to delete.')
|
help='ID of the alarm to delete.')
|
||||||
def do_alarm_delete(cc, args={}):
|
def do_alarm_delete(cc, args={}):
|
||||||
'''Delete an alarm.'''
|
'''Delete an alarm.'''
|
||||||
if args.alarm_id is None:
|
|
||||||
raise exc.CommandError('Alarm ID not provided (-a <alarm id>)')
|
|
||||||
try:
|
try:
|
||||||
cc.alarms.delete(args.alarm_id)
|
cc.alarms.delete(args.alarm_id)
|
||||||
except exc.HTTPNotFound:
|
except exc.HTTPNotFound:
|
||||||
@@ -462,12 +454,10 @@ def do_resource_list(cc, args={}):
|
|||||||
sortby=1)
|
sortby=1)
|
||||||
|
|
||||||
|
|
||||||
@utils.arg('-r', '--resource_id', metavar='<RESOURCE_ID>',
|
@utils.arg('-r', '--resource_id', metavar='<RESOURCE_ID>', required=True,
|
||||||
help='ID of the resource to show.')
|
help='ID of the resource to show.')
|
||||||
def do_resource_show(cc, args={}):
|
def do_resource_show(cc, args={}):
|
||||||
'''Show the resource.'''
|
'''Show the resource.'''
|
||||||
if args.resource_id is None:
|
|
||||||
raise exc.CommandError('Resource id not provided (-r <resource id>)')
|
|
||||||
try:
|
try:
|
||||||
resource = cc.resources.get(args.resource_id)
|
resource = cc.resources.get(args.resource_id)
|
||||||
except exc.HTTPNotFound:
|
except exc.HTTPNotFound:
|
||||||
|
|||||||
Reference in New Issue
Block a user