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: 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')