1
|
|
|
import argparse |
2
|
|
|
|
3
|
|
|
from .plugin import add_display_options |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class HelpAction(argparse.Action): |
7
|
|
|
def __call__(self, parser, namespace, values, option_string=None): |
8
|
|
|
namespace.command = values |
9
|
|
|
namespace.help = True |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class CommandArgumentParser(argparse.ArgumentParser): |
13
|
|
|
commands = None |
14
|
|
|
commands_dispatch = None |
15
|
|
|
|
16
|
|
|
def __init__(self, *args, **kwargs): |
17
|
|
|
kwargs['add_help'] = False |
18
|
|
|
|
19
|
|
|
super(CommandArgumentParser, self).__init__(*args, |
20
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter, |
21
|
|
|
**kwargs) |
22
|
|
|
self.add_argument( |
23
|
|
|
'-h', '--help', |
24
|
|
|
nargs='?', action=HelpAction, help='Display help and exit.' |
25
|
|
|
) |
26
|
|
|
self.add_command( |
27
|
|
|
'help', |
28
|
|
|
description='Display help and exit.' |
29
|
|
|
).add_argument( |
30
|
|
|
'command', |
31
|
|
|
nargs='?', action=HelpAction |
32
|
|
|
) |
33
|
|
|
|
34
|
|
|
@property |
35
|
|
|
def epilog(self): |
36
|
|
|
return '\n'.join(' %-20s %s' % (name, parser.description) for name, parser in self.commands_dispatch.items()) |
37
|
|
|
|
38
|
|
|
@epilog.setter |
39
|
|
|
def epilog(self, value): |
40
|
|
|
pass |
41
|
|
|
|
42
|
|
|
def add_command(self, name, **opts): |
43
|
|
|
if self.commands is None: |
44
|
|
|
self.commands = self.add_subparsers( |
45
|
|
|
title='commands', dest='command', metavar='COMMAND', help='', parser_class=argparse.ArgumentParser |
46
|
|
|
) |
47
|
|
|
self.commands_dispatch = {} |
48
|
|
|
|
49
|
|
|
command = self.commands.add_parser(name, formatter_class=argparse.RawDescriptionHelpFormatter, **opts) |
50
|
|
|
self.commands_dispatch[name] = command |
51
|
|
|
return command |
52
|
|
|
|
53
|
|
|
def parse_args(self): |
54
|
|
|
args = super(CommandArgumentParser, self).parse_args() |
55
|
|
|
print(args) |
56
|
|
|
if args.command: |
57
|
|
|
if args.help: |
58
|
|
|
return super(CommandArgumentParser, self).parse_args([args.command, '--help']) |
59
|
|
|
else: |
60
|
|
|
self.error('the following arguments are required: COMMAND (choose from %s)' % ', '.join( |
61
|
|
|
map(repr, self.commands.choices))) |
62
|
|
|
return args |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def main(): |
66
|
|
|
parser = CommandArgumentParser('py.test-benchmark', description="pytest_benchmark's management commands.") |
67
|
|
|
|
68
|
|
|
parser.add_command( |
69
|
|
|
'list', |
70
|
|
|
description='List saved runs.', |
71
|
|
|
) |
72
|
|
|
display_command = parser.add_command( |
73
|
|
|
'compare', |
74
|
|
|
description='Compare saved runs.', |
75
|
|
|
epilog='''examples: |
76
|
|
|
|
77
|
|
|
pytest-benchmark compare 'Linux-CPython-3.5-64bit/*' |
78
|
|
|
|
79
|
|
|
Loads all benchmarks ran with that interpreter. Note the special quoting that disables your shell's glob |
80
|
|
|
expansion. |
81
|
|
|
|
82
|
|
|
pytest-benchmark compare 0001 |
83
|
|
|
|
84
|
|
|
Loads first run from all the interpreters. |
85
|
|
|
|
86
|
|
|
pytest-benchmark compare /foo/bar/0001_abc.json /lorem/ipsum/0001_sir_dolor.json |
87
|
|
|
|
88
|
|
|
Loads runs from exactly those files.''') |
89
|
|
|
add_display_options(display_command.add_argument) |
90
|
|
|
display_command.add_argument('run', nargs='+', help='Glob to match stored runs.') |
91
|
|
|
args = parser.parse_args() |
92
|
|
|
print(args) |
93
|
|
|
|