Skip to content

Commit d254683

Browse files
committed
Add the ability to set the arguments for help
Simply set Manager.help_args to the appropriate list
1 parent 0224b54 commit d254683

File tree

5 files changed

+63
-12
lines changed

5 files changed

+63
-12
lines changed

docs/index.rst

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,28 @@ The ``@option`` decorator is explained in more detail below.
146146

147147
Help was previously available with **--help** and **-h**. This had a couple
148148
of less-than-ideal consequences, among them the inability to use **-h** as
149-
a shortcut for **--host**. If you want to restore the original meaning of
150-
**-h**,
149+
a shortcut for **--host** or similar options.
150+
151+
*New in version 2.0.2*
152+
153+
If you want to restore the original meaning of **-h**, set your manager's
154+
``help_args`` attribute to a list of argument strings you want to be
155+
considered helpful.
156+
157+
manager = Manager()
158+
manager.help_args = ('-h','-?','--help)
159+
160+
You can override this list in sub-commands and -managers::
161+
162+
def talker(host='localhost'):
163+
pass
164+
ccmd = ConnectCmd(talker)
165+
ccmd.help_args = ('-?','--help)
166+
manager.add_command("connect", ccmd)
167+
manager.run()
168+
169+
so that **manager -h** prints help, while **manager connect -h fubar.example.com**
170+
connects to a remote host.
151171

152172
Adding arguments to commands
153173
----------------------------
@@ -351,8 +371,8 @@ Before version 2, options and command names could be interspersed freely.
351371
The author decided to discontinue this practice for a number of reasons;
352372
the problem with the most impact was that it was not possible to do
353373

354-
> python manage.py connect -d DEST
355-
> python manage.py import -d DIR
374+
> python manage.py connect -d DEST
375+
> python manage.py import -d DIR
356376

357377
as these options collided.
358378

flask_script/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
except ImportError:
3535
ARGCOMPLETE_IMPORTED = False
3636

37-
def add_help(parser):
38-
parser.add_argument('-?', '--help',
37+
def add_help(parser, help_args):
38+
if not help_args:
39+
return
40+
parser.add_argument(*help_args,
3941
action='help', default=argparse.SUPPRESS, help=_('show this help message and exit'))
4042

4143
class Manager(object):
@@ -68,6 +70,7 @@ def run(self):
6870
:param disable_argcomplete: disable automatic loading of argcomplete.
6971
7072
"""
73+
help_args = ('-?','--help')
7174

7275
def __init__(self, app=None, with_default_commands=None, usage=None,
7376
help=None, description=None, disable_argcomplete=False):
@@ -174,7 +177,7 @@ def create_parser(self, prog, func_stack=(), parent=None):
174177
description=self.description,
175178
parents=[options_parser],
176179
add_help=False)
177-
add_help(parser)
180+
add_help(parser, self.help_args)
178181

179182
self._patch_argparser(parser)
180183

flask_script/commands.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Command(object):
9797
"""
9898

9999
option_list = ()
100-
add_help = True
100+
help_args = None
101101

102102
def __init__(self, func=None):
103103
if func is None:
@@ -163,13 +163,17 @@ def get_options(self):
163163
return self.option_list
164164

165165
def create_parser(self, *args, **kwargs):
166-
167166
func_stack = kwargs.pop('func_stack',())
168167
parent = kwargs.pop('parent',None)
169168
parser = argparse.ArgumentParser(*args, add_help=False, **kwargs)
170-
if self.add_help:
169+
help_args = self.help_args
170+
while help_args is None and parent is not None:
171+
help_args = parent.help_args
172+
parent = getattr(parent,'parent',None)
173+
174+
if help_args:
171175
from flask_script import add_help
172-
add_help(parser)
176+
add_help(parser,help_args)
173177

174178
for option in self.get_options():
175179
if isinstance(option, Group):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
setup(
2929
name='Flask-Script',
30-
version='2.0.1',
30+
version='2.0.2',
3131
url='http://github.com/smurfix/flask-script',
3232
license='BSD',
3333
author='Dan Jacob',

tests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,30 @@ def hello(name='fred'):
323323
out, err = capsys.readouterr()
324324
assert 'Prints your name' in out
325325

326+
def test_no_help(self, capsys):
327+
"""
328+
Tests that erasing --help really works.
329+
"""
330+
331+
manager = Manager(self.app)
332+
manager.help_args = ()
333+
334+
@manager.command
335+
def hello(name='fred'):
336+
'Prints your name'
337+
print('hello ' + name)
338+
assert 'hello' in manager._commands
339+
340+
code = run('manage.py --help hello', manager.run)
341+
out, err = capsys.readouterr()
342+
print(out)
343+
assert 'too many arguments' in err
344+
345+
code = run('manage.py hello --help', manager.run)
346+
out, err = capsys.readouterr()
347+
print(out)
348+
assert 'too many arguments' in err
349+
326350
def test_command_decorator_with_boolean_options(self, capsys):
327351

328352
manager = Manager(self.app)

0 commit comments

Comments
 (0)