diff options
-rw-r--r-- | scripts/lib/argparse_oe.py | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/scripts/lib/argparse_oe.py b/scripts/lib/argparse_oe.py index 95b42e75ea..75002d02af 100644 --- a/scripts/lib/argparse_oe.py +++ b/scripts/lib/argparse_oe.py | |||
@@ -27,15 +27,20 @@ class ArgumentParser(argparse.ArgumentParser): | |||
27 | 27 | ||
28 | def error_subcommand(self, message, subcommand): | 28 | def error_subcommand(self, message, subcommand): |
29 | if subcommand: | 29 | if subcommand: |
30 | for action in self._actions: | 30 | action = self._get_subparser_action() |
31 | if isinstance(action, argparse._SubParsersAction): | 31 | try: |
32 | for choice, subparser in action.choices.items(): | 32 | subparser = action._name_parser_map[subcommand] |
33 | if choice == subcommand: | 33 | except KeyError: |
34 | subparser.error(message) | 34 | self.error('no subparser for name "%s"' % subcommand) |
35 | return | 35 | else: |
36 | subparser.error(message) | ||
37 | |||
36 | self.error(message) | 38 | self.error(message) |
37 | 39 | ||
38 | def add_subparsers(self, *args, **kwargs): | 40 | def add_subparsers(self, *args, **kwargs): |
41 | if 'dest' not in kwargs: | ||
42 | kwargs['dest'] = '_subparser_name' | ||
43 | |||
39 | ret = super(ArgumentParser, self).add_subparsers(*args, **kwargs) | 44 | ret = super(ArgumentParser, self).add_subparsers(*args, **kwargs) |
40 | # Need a way of accessing the parent parser | 45 | # Need a way of accessing the parent parser |
41 | ret._parent_parser = self | 46 | ret._parent_parser = self |
@@ -48,6 +53,38 @@ class ArgumentParser(argparse.ArgumentParser): | |||
48 | def add_subparser_group(self, groupname, groupdesc, order=0): | 53 | def add_subparser_group(self, groupname, groupdesc, order=0): |
49 | self._subparser_groups[groupname] = (groupdesc, order) | 54 | self._subparser_groups[groupname] = (groupdesc, order) |
50 | 55 | ||
56 | def parse_args(self, args=None, namespace=None): | ||
57 | """Parse arguments, using the correct subparser to show the error.""" | ||
58 | args, argv = self.parse_known_args(args, namespace) | ||
59 | if argv: | ||
60 | message = 'unrecognized arguments: %s' % ' '.join(argv) | ||
61 | if self._subparsers: | ||
62 | subparser = self._get_subparser(args) | ||
63 | subparser.error(message) | ||
64 | else: | ||
65 | self.error(message) | ||
66 | sys.exit(2) | ||
67 | return args | ||
68 | |||
69 | def _get_subparser(self, args): | ||
70 | action = self._get_subparser_action() | ||
71 | if action.dest == argparse.SUPPRESS: | ||
72 | self.error('cannot get subparser, the subparser action dest is suppressed') | ||
73 | |||
74 | name = getattr(args, action.dest) | ||
75 | try: | ||
76 | return action._name_parser_map[name] | ||
77 | except KeyError: | ||
78 | self.error('no subparser for name "%s"' % name) | ||
79 | |||
80 | def _get_subparser_action(self): | ||
81 | if not self._subparsers: | ||
82 | self.error('cannot return the subparser action, no subparsers added') | ||
83 | |||
84 | for action in self._subparsers._group_actions: | ||
85 | if isinstance(action, argparse._SubParsersAction): | ||
86 | return action | ||
87 | |||
51 | 88 | ||
52 | class ArgumentSubParser(ArgumentParser): | 89 | class ArgumentSubParser(ArgumentParser): |
53 | def __init__(self, *args, **kwargs): | 90 | def __init__(self, *args, **kwargs): |