From d8a1de5886e7beda0274b4fa49ae6d6508464cdf Mon Sep 17 00:00:00 2001 From: Chris McNabb Date: Mon, 9 May 2016 09:28:18 -0500 Subject: [PATCH 1/2] failing test --- tests.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests.py b/tests.py index 93d0071..09089d1 100644 --- a/tests.py +++ b/tests.py @@ -165,15 +165,20 @@ def __exit__(self, a,b,c): pass class AppForTesting(object): + name = "APP" def __init__(self, verbose=False): self.verbose = verbose def test_request_context(self): return EmptyContext() def __call__(self,**kw): if self.verbose: - print("APP "+" ".join("%s=%s" % (k,v) for k,v in kw.items())) + print("{} {}".format(self.name, + " ".join("%s=%s" % (k,v) for k,v in kw.items()))) return self +class SubAppForTesting(AppForTesting): + name = "SUB" + class TestManager: @@ -804,6 +809,25 @@ def test_submanager_separate_options(self, capsys): assert 'APP name_sub=MySubName' in out assert 'OK name=MyName' in out + def test_submanager_overrides_app(self, capsys): + def gen_app(app, **kwargs): + return SubAppForTesting(verbose=True) + sub_manager = Manager(gen_app, with_default_commands=False) + sub_manager.add_command('opt', CommandWithOptionalArg()) + sub_manager.add_option('-n', '--name', dest='name_sub', required=False) + + manager = Manager(AppForTesting(verbose=True), with_default_commands=False) + manager.add_command('sub_manager', sub_manager) + manager.add_option('-n', '--name', dest='name_main', required=False) + + code = run('manage.py -n MyMainName sub_manager -n MySubName opt -n MyName', manager.run) + out, err = capsys.readouterr() + assert code == 0 + assert 'APP name_main=MyMainName' in out + assert 'SUB name_sub=MySubName' in out + assert 'OK name=MyName' in out + + def test_manager_usage_with_submanager(self, capsys): sub_manager = Manager(usage='Example sub-manager') From b1c55a6954e692318af5d9ec45290e1f0e136ac8 Mon Sep 17 00:00:00 2001 From: Chris McNabb Date: Mon, 9 May 2016 10:11:11 -0500 Subject: [PATCH 2/2] call the sub-manager's app and allow it to override the parent app --- flask_script/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/flask_script/__init__.py b/flask_script/__init__.py index 306f522..9cc5c5f 100644 --- a/flask_script/__init__.py +++ b/flask_script/__init__.py @@ -146,6 +146,13 @@ def __call__(self, app=None, **kwargs): If your sub-Manager does not override this, any values for options will get lost. """ + # If we have a parent, we are a sub-manager + if self.parent and self.app: + # call our own app and use the result if it's not None + res = self.app(app=app, **kwargs) + if res != None: + app = res + if app is None: app = self.app if app is None: