Skip to content
Myr

add command_class and group_class to Group

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent dee49e5 · Browse files · 1 file+46 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+46 added−6 removed

  • other:+46 added−6 removed

1 file+46 added−6 removed

modified:src/click/core.py+46 added-6 removed
1391 unmodified lines
13921393 :param commands: a dictionary of commands.1394 """13951396 #: If set, this is used by the group's :meth:`command` decorator1397 #: as the default :class:`Command` class. This is useful to make all1398 #: subcommands use a custom command class.1399 #:1400 #: .. versionadded:: 8.01401 command_class = None14021403 #: If set, this is used by the group's :meth:`group` decorator1404 #: as the default :class:`Group` class. This is useful to make all1405 #: subgroups use a custom group class.1406 #:1407 #: If set to the special value :class:`type` (literally1408 #: ``group_class = type``), this group's class will be used as the1409 #: default class. This makes a custom group class continue to make1410 #: custom groups.1411 #:1412 #: .. versionadded:: 8.01413 group_class = None14141415 def __init__(self, name=None, commands=None, **attrs):1416 super().__init__(name, **attrs)1417 #: the registered subcommands by their exported names.1418 self.commands = commands or {}
9 unmodified lines
1428 self.commands[name] = cmd14291430 def command(self, *args, **kwargs):1431 """A shortcut decorator for declaring and attaching a command to1413 the group. This takes the same arguments as :func:`command` but1414 immediately registers the created command with this instance by1415 calling into :meth:`add_command`.1432 the group. This takes the same arguments as :func:`command` and1433 immediately registers the created command with this group by1434 calling :meth:`add_command`.14351436 To customize the command class used, set the1437 :attr:`command_class` attribute.14381439 .. versionchanged:: 8.01440 Added the :attr:`command_class` attribute.1441 """1442 from .decorators import command14431444 if self.command_class is not None and "cls" not in kwargs:1445 kwargs["cls"] = self.command_class14461447 def decorator(f):1448 cmd = command(*args, **kwargs)(f)1449 self.add_command(cmd)1450 return cmd
1 unmodified line
1452 return decorator14531454 def group(self, *args, **kwargs):1455 """A shortcut decorator for declaring and attaching a group to1428 the group. This takes the same arguments as :func:`group` but1429 immediately registers the created command with this instance by1430 calling into :meth:`add_command`.1456 the group. This takes the same arguments as :func:`group` and1457 immediately registers the created group with this group by1458 calling :meth:`add_command`.14591460 To customize the group class used, set the :attr:`group_class`1461 attribute.14621463 .. versionchanged:: 8.01464 Added the :attr:`group_class` attribute.1465 """1466 from .decorators import group14671468 if self.group_class is not None and "cls" not in kwargs:1469 if self.group_class is type:1470 kwargs["cls"] = type(self)1471 else:1472 kwargs["cls"] = self.group_class14731474 def decorator(f):1475 cmd = group(*args, **kwargs)(f)1476 self.add_command(cmd)1477 return cmd
692 unmodified lines