Permalink
Browse files

Add ability to name commands from a class attribute.

  • Loading branch information...
1 parent 72b5a3b commit f7660d19996c691694a5f51650b2896ddfde55af @mehcode mehcode committed Oct 5, 2013
Showing with 51 additions and 1 deletion.
  1. +18 −1 flask_script/__init__.py
  2. +33 −0 tests.py
View
@@ -2,6 +2,7 @@
from __future__ import absolute_import
import os
+import re
import sys
import inspect
@@ -177,13 +178,29 @@ def get_options(self):
return self._options
- def add_command(self, name, command):
+ def add_command(self, *args):
"""
Adds command to registry.
:param command: Command instance
+ :param name: Name of the command (optional)
"""
+ if len(args) == 1:
+ command = args[0]
+ name = None
+
+ else:
+ name, command = args
+
+ if name is None:
+ if hasattr(command, 'name'):
+ name = command.name
+
+ else:
+ name = type(command).__name__.lower()
+ name = re.sub(r'command$', '', name)
+
if isinstance(command, Manager):
command.parent = self
View
@@ -66,6 +66,22 @@ def run(self):
print('OK')
+class NamedCommand(Command):
+ 'named command'
+
+ def run(self):
+ print('OK')
+
+
+class ExplicitNamedCommand(Command):
+ 'named command'
+
+ name = 'named'
+
+ def run(self):
+ print('OK')
+
+
class CommandWithArgs(Command):
'command with args'
@@ -162,6 +178,23 @@ def test_add_command(self):
assert isinstance(manager._commands['simple'], SimpleCommand)
+ def test_add_named_command(self):
+
+ manager = Manager(self.app)
+ manager.add_command(NamedCommand())
+
+ assert 'named' in manager._commands
+ assert isinstance(manager._commands['named'], NamedCommand)
+
+ def test_add_explicit_named_command(self):
+
+ manager = Manager(self.app)
+ manager.add_command(ExplicitNamedCommand())
+
+ name = ExplicitNamedCommand.name
+ assert name in manager._commands
+ assert isinstance(manager._commands[name], ExplicitNamedCommand)
+
def test_simple_command_decorator(self, capsys):
manager = Manager(self.app)

0 comments on commit f7660d1

Please sign in to comment.