|
1 | 1 | import argparse |
2 | 2 | import re |
3 | 3 |
|
4 | | -class SelfCompletingArgumentParser(argparse.ArgumentParser): |
5 | | - def __init__(self, *args, **kw): |
6 | | - argparse.ArgumentParser.__init__(self, *args, **kw) |
7 | | - self._completion_parser = argparse.ArgumentParser() |
8 | | - self._completion_parser.add_argument('--_completion', type=str) |
| 4 | +class _CompletionAction(argparse.Action): |
| 5 | + def __init__(self, |
| 6 | + option_strings, |
| 7 | + dest='_completion', |
| 8 | + metavar='CMD_LINE', |
| 9 | + default=argparse.SUPPRESS, |
| 10 | + help="Built-in command completion"): |
| 11 | + super(_CompletionAction, self).__init__( |
| 12 | + option_strings=option_strings, |
| 13 | + dest=dest, |
| 14 | + metavar=metavar, |
| 15 | + default=default, |
| 16 | + nargs=1, |
| 17 | + type=str, |
| 18 | + help=help) |
9 | 19 |
|
10 | | - def parse_args(self, *args, **kw): |
11 | | - comp_args = self._completion_parser.parse_args(*args, **kw) |
12 | | - if comp_args._completion is not None: |
13 | | - comp_words = re.split(r'\s+', comp_args._completion) |
14 | | - comp_word = comp_words[-1] |
15 | | - all_options = sum((a.option_strings for a in self._actions), []) |
16 | | - types = [a.type for a in self._actions if a.nargs != 0 and a.type is not None] |
17 | | - for opt in all_options: |
18 | | - if opt.startswith(comp_word): |
19 | | - print opt+' ' |
20 | | - if int in types and re.match(r'^\d*$', comp_word): |
21 | | - for i in xrange(10): |
22 | | - print i |
| 20 | + def __call__(self, parser, namespace, values, option_string=None): |
| 21 | + comp_words = re.split(r'\s+', values[0]) |
| 22 | + comp_word = comp_words[-1] |
| 23 | + all_options = sum((a.option_strings for a in parser._actions), []) |
| 24 | + types = [a.type for a in parser._actions if a.nargs != 0 and a.type is not None] |
| 25 | + for opt in all_options: |
| 26 | + if opt.startswith(comp_word): |
| 27 | + print opt+' ' |
| 28 | + if int in types and re.match(r'^\d*$', comp_word): |
| 29 | + for i in xrange(10): |
| 30 | + print i |
| 31 | + parser.exit() |
23 | 32 |
|
24 | | - self.exit(0) |
25 | | - else: |
26 | | - return argparse.ArgumentParser.parse_args(self, *args, **kw) |
| 33 | +class SelfCompletingArgumentParser(argparse.ArgumentParser): |
| 34 | + def __init__(self, *args, **kw): |
| 35 | + super(SelfCompletingArgumentParser, self).__init__(*args, **kw) |
| 36 | + completion_action = _CompletionAction(['--_completion']) |
| 37 | + self._add_action(completion_action) |
0 commit comments