diff options
author | Christopher Larson <chris_larson@mentor.com> | 2016-04-27 16:23:59 -0700 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2016-05-06 10:31:13 +0100 |
commit | 3e79d54523e348258cc7d9455376429ea57106f2 (patch) | |
tree | 1da64324aad040d0abe75071c0a930a6aacbc51e /scripts | |
parent | 55c760bda391f8ac398aa078391a78415dd68a82 (diff) | |
download | poky-3e79d54523e348258cc7d9455376429ea57106f2.tar.gz |
scripts/lib/argparse_oe: show subparser help for unrecognized args
As an example, `recipetool create foo bar baz` shows `recipetool: error:
unrecognized arguments: bar baz` and then displays the main help, not the help
for the create command. Fix by saving the subparser name and using it in
parse_args() to look up the subparser.
(From OE-Core rev: 7fdaaedf4c63c8d019f03f84e22f9b838ef19aa6)
Signed-off-by: Christopher Larson <chris_larson@mentor.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts')
-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): |