diff options
Diffstat (limited to 'scripts/lib/argparse_oe.py')
-rw-r--r-- | scripts/lib/argparse_oe.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/scripts/lib/argparse_oe.py b/scripts/lib/argparse_oe.py index ec7eb8d191..fd866922bd 100644 --- a/scripts/lib/argparse_oe.py +++ b/scripts/lib/argparse_oe.py | |||
@@ -1,6 +1,12 @@ | |||
1 | import sys | 1 | import sys |
2 | import argparse | 2 | import argparse |
3 | 3 | ||
4 | class ArgumentUsageError(Exception): | ||
5 | """Exception class you can raise (and catch) in order to show the help""" | ||
6 | def __init__(self, message, subcommand=None): | ||
7 | self.message = message | ||
8 | self.subcommand = subcommand | ||
9 | |||
4 | class ArgumentParser(argparse.ArgumentParser): | 10 | class ArgumentParser(argparse.ArgumentParser): |
5 | """Our own version of argparse's ArgumentParser""" | 11 | """Our own version of argparse's ArgumentParser""" |
6 | 12 | ||
@@ -9,6 +15,16 @@ class ArgumentParser(argparse.ArgumentParser): | |||
9 | self.print_help() | 15 | self.print_help() |
10 | sys.exit(2) | 16 | sys.exit(2) |
11 | 17 | ||
18 | def error_subcommand(self, message, subcommand): | ||
19 | if subcommand: | ||
20 | for action in self._actions: | ||
21 | if isinstance(action, argparse._SubParsersAction): | ||
22 | for choice, subparser in action.choices.items(): | ||
23 | if choice == subcommand: | ||
24 | subparser.error(message) | ||
25 | return | ||
26 | self.error(message) | ||
27 | |||
12 | def add_subparsers(self, *args, **kwargs): | 28 | def add_subparsers(self, *args, **kwargs): |
13 | ret = super(ArgumentParser, self).add_subparsers(*args, **kwargs) | 29 | ret = super(ArgumentParser, self).add_subparsers(*args, **kwargs) |
14 | ret._parser_class = ArgumentSubParser | 30 | ret._parser_class = ArgumentSubParser |