Skip to content

Commit 75acc03

Browse files
committed
command: add with_group decorator
This decorator allows to add a group of arguments. The with_server decorator has been replaced by this decorator. Search criteria for the 'search' command has been added to a separate group to improve clarity Signed-off-by: Thomas Faivre <thomas.faivre@6wind.com>
1 parent d76a836 commit 75acc03

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

bzlib/command.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,14 @@ def decorator(cls):
137137
return decorator
138138

139139

140-
def with_server(cls):
141-
def add_server_args(parser):
142-
group = parser.add_argument_group('server arguments')
143-
group.add_argument('--server', help='name of Bugzilla server to use')
144-
group.add_argument('--url', help='base URL of Bugzilla server')
145-
group.add_argument('--user', help='Bugzilla username')
146-
group.add_argument('--password', help='Bugzilla password')
147-
cls.args = cls.args + [add_server_args]
148-
return cls
140+
def with_group(name, arguments, description=None):
141+
def decorator(cls):
142+
def add_argument_group(parser):
143+
group = parser.add_argument_group(name, description)
144+
arguments(cls, group)
145+
cls.args = cls.args + [add_argument_group]
146+
return cls
147+
return decorator
149148

150149

151150
class Command(object):
@@ -253,7 +252,14 @@ def __call__(self):
253252
self._parser.parse_args([self._args.subcommand, '--help'])
254253

255254

256-
@with_server
255+
def _make_server_arguments_group(cls, group):
256+
group.add_argument('--server', help='name of Bugzilla server to use')
257+
group.add_argument('--url', help='base URL of Bugzilla server')
258+
group.add_argument('--user', help='Bugzilla username')
259+
group.add_argument('--password', help='Bugzilla password')
260+
261+
262+
@with_group('server arguments', _make_server_arguments_group)
257263
class BugzillaCommand(Command):
258264
def __init__(self, *args, **kwargs):
259265
super(BugzillaCommand, self).__init__(*args, **kwargs)
@@ -816,33 +822,31 @@ def __call__(self):
816822
)
817823

818824

819-
def _make_set_argument(arg):
820-
template = 'Only match bugs {{}}of the given {}({})'.format(
821-
arg, 's' if arg[-1] != 's' else 'es')
822-
return [
823-
lambda x: x.add_argument('--' + arg, nargs='+',
825+
826+
def _make_set_arguments_group(cls, group):
827+
group.add_argument('--summary', nargs='+',
828+
help='Match summary against any of the given substrings.')
829+
830+
for arg in cls.set_arguments:
831+
template = 'Only match bugs {{}}of the given {}({})'.format(
832+
arg, 's' if arg[-1] != 's' else 'es')
833+
group.add_argument('--' + arg, nargs='+',
824834
metavar=arg.upper(),
825835
help=template.format('')),
826-
lambda x: x.add_argument('--not-' + arg, nargs='+',
836+
group.add_argument('--not-' + arg, nargs='+',
827837
metavar=arg.upper(),
828838
help=template.format('NOT ')),
829-
]
830839

831840

841+
@with_group('search criteria', _make_set_arguments_group)
832842
class Search(BugzillaCommand):
833843
"""Search for bugs matching given criteria.
834844
835845
If both '--foo' and '--not-foo' are given for any argument 'foo',
836846
the former takes precendence.
837847
"""
838-
args = BugzillaCommand.args + [
839-
lambda x: x.add_argument('--summary', nargs='+',
840-
help='Match summary against any of the given substrings.'),
841-
]
842848
simple_arguments = ['summary']
843849
set_arguments = 'product', 'component', 'status', 'resolution', 'version'
844-
for x in set_arguments:
845-
args.extend(_make_set_argument(x))
846850

847851
def __call__(self):
848852
kwargs = {

0 commit comments

Comments
 (0)