Skip to content

Commit 96c5525

Browse files
committed
Merge branch 'v1.0' into v2
2 parents b80aca7 + 1d1f2e0 commit 96c5525

File tree

3 files changed

+65
-40
lines changed

3 files changed

+65
-40
lines changed

flask_script/__init__.py

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
import re
66
import sys
77
import types
8-
import inspect
98
import warnings
109

1110
import argparse
1211

1312
from flask import Flask
1413

15-
from ._compat import text_type, iteritems, izip
14+
from ._compat import iteritems
1615
from .commands import Group, Option, Command, Server, Shell
1716
from .cli import prompt, prompt_pass, prompt_bool, prompt_choices
1817

@@ -266,44 +265,7 @@ def command(self, func):
266265
267266
"""
268267

269-
args, varargs, keywords, defaults = inspect.getargspec(func)
270-
271-
options = []
272-
273-
# first arg is always "app" : ignore
274-
275-
defaults = defaults or []
276-
kwargs = dict(izip(*[reversed(l) for l in (args, defaults)]))
277-
278-
for arg in args:
279-
280-
if arg in kwargs:
281-
282-
default = kwargs[arg]
283-
284-
if isinstance(default, bool):
285-
options.append(Option('-%s' % arg[0],
286-
'--%s' % arg,
287-
action="store_true",
288-
dest=arg,
289-
required=False,
290-
default=default))
291-
else:
292-
options.append(Option('-%s' % arg[0],
293-
'--%s' % arg,
294-
dest=arg,
295-
type=text_type,
296-
required=False,
297-
default=default))
298-
299-
else:
300-
options.append(Option(arg, type=text_type))
301-
302-
command = Command()
303-
command.run = func
304-
command.__doc__ = func.__doc__
305-
command.option_list = options
306-
268+
command = Command(func)
307269
self.add_command(func.__name__, command)
308270

309271
return func

flask_script/commands.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import code
66
import warnings
77
import string
8+
import inspect
89

910
import argparse
1011

1112
from flask import _request_ctx_stack
1213

1314
from .cli import prompt, prompt_pass, prompt_bool, prompt_choices
15+
from ._compat import izip, text_type
1416

1517

1618
class InvalidCommand(Exception):
@@ -90,11 +92,56 @@ def __init__(self, *args, **kwargs):
9092
class Command(object):
9193
"""
9294
Base class for creating commands.
95+
96+
:param func: Initialize this command by introspecting the function.
9397
"""
9498

9599
option_list = []
96100
add_help = True
97101

102+
def __init__(self, func=None):
103+
if func is None:
104+
return
105+
106+
args, varargs, keywords, defaults = inspect.getargspec(func)
107+
if inspect.ismethod(func):
108+
args = args[1:]
109+
110+
options = []
111+
112+
# first arg is always "app" : ignore
113+
114+
defaults = defaults or []
115+
kwargs = dict(izip(*[reversed(l) for l in (args, defaults)]))
116+
117+
for arg in args:
118+
119+
if arg in kwargs:
120+
121+
default = kwargs[arg]
122+
123+
if isinstance(default, bool):
124+
options.append(Option('-%s' % arg[0],
125+
'--%s' % arg,
126+
action="store_true",
127+
dest=arg,
128+
required=False,
129+
default=default))
130+
else:
131+
options.append(Option('-%s' % arg[0],
132+
'--%s' % arg,
133+
dest=arg,
134+
type=text_type,
135+
required=False,
136+
default=default))
137+
138+
else:
139+
options.append(Option(arg, type=text_type))
140+
141+
self.run = func
142+
self.__doc__ = func.__doc__
143+
self.option_list = options
144+
98145
@property
99146
def description(self):
100147
description = self.__doc__ or ''

tests.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,22 @@ def hello(name):
280280
out, err = capsys.readouterr()
281281
assert 'hello joe' in out
282282

283+
def test_method_command_decorator_with_pos_arg(self, capsys):
284+
285+
manager = Manager(self.app)
286+
287+
class SomeTest(object):
288+
def hello(self,name):
289+
print('hello ' + name)
290+
sometest = SomeTest()
291+
manager.command(sometest.hello)
292+
293+
assert 'hello' in manager._commands
294+
295+
code = run('manage.py hello joe', lambda: manager.run())
296+
out, err = capsys.readouterr()
297+
assert 'hello joe' in out
298+
283299
def test_command_decorator_with_options(self, capsys):
284300

285301
manager = Manager(self.app)

0 commit comments

Comments
 (0)