Skip to content
Browse files

Show full help for submanagers if called without arguments

  • Loading branch information...
1 parent a90431e commit 84e9568cfa234ec1ce7ce72b5c74c4fd216bbfb9 @Turbo87 Turbo87 committed Jan 26, 2014
Showing with 24 additions and 5 deletions.
  1. +20 −4 flask_script/__init__.py
  2. +4 −1 tests.py
View
24 flask_script/__init__.py
@@ -4,6 +4,7 @@
import os
import re
import sys
+import types
import inspect
import warnings
@@ -156,6 +157,8 @@ def create_parser(self, prog, parents=None):
description=self.description,
parents=[options_parser])
+ self._patch_argparser(parser)
+
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
@@ -174,6 +177,8 @@ def create_parser(self, prog, parents=None):
description=description,
parents=[command_parser], add_help=False)
+ if isinstance(command, Manager):
+ self._patch_argparser(subparser)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
@@ -186,6 +191,21 @@ def create_parser(self, prog, parents=None):
# def foo(self, app, *args, **kwargs):
# print(args)
+ def _patch_argparser(self, parser):
+ """
+ Patches the parser to print the full help if no arguments are supplied
+ """
+
+ def _parse_known_args(self, arg_strings, *args, **kw):
+ if not arg_strings:
+ self.print_help()
+ self.exit(2)
+
+ return self._parse_known_args2(arg_strings, *args, **kw)
+
+ parser._parse_known_args2 = parser._parse_known_args
+ parser._parse_known_args = types.MethodType(_parse_known_args, parser)
+
def get_options(self):
if self.parent:
return self.parent._options
@@ -341,10 +361,6 @@ def handle(self, prog, args=None):
app_parser = self.create_parser(prog)
- if args is None or len(args) == 0:
- app_parser.print_help()
- return 2
-
args = list(args or [])
app_namespace, remaining_args = app_parser.parse_known_args(args)
View
5 tests.py
@@ -718,7 +718,10 @@ def test_submanager_usage_and_help_and_description(self, capsys):
code = run('manage.py sub_manager', lambda: manager.run())
out, err = capsys.readouterr()
assert code == 2
- assert 'too few arguments' in err
+ assert 'sub_manager [--foo]' in out
+ assert 'shorter desc for submanager' not in out
+ assert 'longer desc for submanager' in out
+ assert 'simple command' in out
code = run('manage.py sub_manager -h', lambda: manager.run())
out, err = capsys.readouterr()

0 comments on commit 84e9568

Please sign in to comment.
Something went wrong with that request. Please try again.