Skip to content

Commit

Permalink
Make the CLI parse properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Dec 27, 2015
1 parent 63757dd commit 5dce037
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/pytest_benchmark/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
"""
py.test-benchmark
"""
import argparse

from .plugin import add_display_options
Expand All @@ -26,9 +21,15 @@ def __init__(self, *args, **kwargs):
**kwargs)
self.add_argument(
'-h', '--help',
action='store_true', help='Display help and exit.'
nargs='?', action=HelpAction, help='Display help and exit.'
)
self.add_command(
'help',
description='Display help and exit.'
).add_argument(
'command',
nargs='?', action=HelpAction
)
self.add_command('help', description='Display help and exit.').add_argument('command', nargs='?', action=HelpAction)

@property
def epilog(self):
Expand All @@ -41,27 +42,24 @@ def epilog(self, value):
def add_command(self, name, **opts):
if self.commands is None:
self.commands = self.add_subparsers(
title='commands', dest='command', help='', parser_class=argparse.ArgumentParser
title='commands', dest='command', metavar='COMMAND', help='', parser_class=argparse.ArgumentParser
)
self.commands_dispatch = {}

command = self.commands.add_parser(name, formatter_class=argparse.RawDescriptionHelpFormatter, **opts)
# command.add_argument(
# '-h', '--help',
# action='store_true', help='Show this help message and exit.'
# )
self.commands_dispatch[name] = command
return command

def parse_args(self):
args = super(CommandArgumentParser, self).parse_args()
print(args)
if args.help:
if args.command:
if args.command:
if args.help:
return super(CommandArgumentParser, self).parse_args([args.command, '--help'])
else:
self.print_help()
self.exit()
else:
self.error('the following arguments are required: COMMAND (choose from %s)' % ', '.join(
map(repr, self.commands.choices)))
return args


def main():
Expand Down

0 comments on commit 5dce037

Please sign in to comment.