summaryrefslogtreecommitdiffstats
path: root/scripts/lib/argparse_oe.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/argparse_oe.py')
-rw-r--r--scripts/lib/argparse_oe.py16
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 @@
import sys
import argparse
+class ArgumentUsageError(Exception):
+ """Exception class you can raise (and catch) in order to show the help"""
+ def __init__(self, message, subcommand=None):
+ self.message = message
+ self.subcommand = subcommand
+
class ArgumentParser(argparse.ArgumentParser):
"""Our own version of argparse's ArgumentParser"""
@@ -9,6 +15,16 @@ class ArgumentParser(argparse.ArgumentParser):
self.print_help()
sys.exit(2)
+ def error_subcommand(self, message, subcommand):
+ if subcommand:
+ for action in self._actions:
+ if isinstance(action, argparse._SubParsersAction):
+ for choice, subparser in action.choices.items():
+ if choice == subcommand:
+ subparser.error(message)
+ return
+ self.error(message)
+
def add_subparsers(self, *args, **kwargs):
ret = super(ArgumentParser, self).add_subparsers(*args, **kwargs)
ret._parser_class = ArgumentSubParser