From d62fe7c9bc2df6a4464440a3cae0539074bf99aa Mon Sep 17 00:00:00 2001 From: Paul Eggleton Date: Tue, 22 Dec 2015 17:02:54 +1300 Subject: scripts: print usage in argparse-using scripts when a command-line error occurs For scripts that use Python's standard argparse module to parse command-line arguments, create a subclass which will show the usage the usage information when a command-line parsing error occurs. The most common case would be when the script is run with no arguments; at least then the user immediately gets to see what arguments they might need to pass instead of just an error message. Signed-off-by: Paul Eggleton Signed-off-by: Richard Purdie --- scripts/lib/argparse_oe.py | 11 +++++++++++ scripts/lib/scriptutils.py | 1 + 2 files changed, 12 insertions(+) create mode 100644 scripts/lib/argparse_oe.py (limited to 'scripts/lib') diff --git a/scripts/lib/argparse_oe.py b/scripts/lib/argparse_oe.py new file mode 100644 index 0000000000..c2fee6de05 --- /dev/null +++ b/scripts/lib/argparse_oe.py @@ -0,0 +1,11 @@ +import sys +import argparse + +class ArgumentParser(argparse.ArgumentParser): + """Our own version of argparse's ArgumentParser""" + + def error(self, message): + sys.stderr.write('ERROR: %s\n' % message) + self.print_help() + sys.exit(2) + diff --git a/scripts/lib/scriptutils.py b/scripts/lib/scriptutils.py index 3366882635..4dd7ef2a0d 100644 --- a/scripts/lib/scriptutils.py +++ b/scripts/lib/scriptutils.py @@ -19,6 +19,7 @@ import sys import os import logging import glob +import argparse def logger_create(name): logger = logging.getLogger(name) -- cgit v1.2.3