From e016deb4bbfae014ada1808abaeeb30558cac209 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Thu, 10 Oct 2019 09:08:37 +0200 Subject: Convert standard_kconfig() to argparse for better feedback Stuff like this is not the pinnacle of helpful design, and hides that the commands actually have long help texts (that can be viewed with pydoc): $ ./menuconfig.py --help [Errno 2] No such file or directory: '--help' Fix it by converting standard_kconfig() to argparse, and add a 'description' argument to it for the command-specific help text. --help now shows the same help text shown by pydoc, and some other error messages are improved as well. Also fix some copy-paste errors and outdated paths in the help texts for the all*config commands. --- kconfiglib.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'kconfiglib.py') diff --git a/kconfiglib.py b/kconfiglib.py index dacf5eb..237ecd1 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -6057,21 +6057,35 @@ def unescape(s): _unescape_sub = re.compile(r"\\(.)").sub -def standard_kconfig(): +def standard_kconfig(description=None): """ - Helper for tools. Loads the top-level Kconfig specified as the first - command-line argument, or "Kconfig" if there are no command-line arguments. - Returns the Kconfig instance. + Argument parsing helper for tools that take a single optional Kconfig file + argument (default: Kconfig). Uses argparse internally. - Exits with sys.exit() (which raises a SystemExit exception) and prints a - usage note to stderr if more than one command-line argument is passed. + Exits with sys.exit() (which raises SystemExit) on errors. + + description (default: None): + The 'description' passed to argparse.ArgumentParser(). + argparse.RawDescriptionHelpFormatter is used, so formatting is preserved. """ - if len(sys.argv) > 2: - sys.exit("usage: {} [Kconfig]".format(sys.argv[0])) + import argparse + + parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter, + description=description) + + parser.add_argument( + "kconfig", + metavar="KCONFIG", + default="Kconfig", + nargs="?", + help="Kconfig file (default: Kconfig)") + + args = parser.parse_args() - # Only show backtraces for unexpected exceptions + # Suppress backtraces for expected exceptions try: - return Kconfig("Kconfig" if len(sys.argv) < 2 else sys.argv[1]) + return Kconfig(args.kconfig) except (EnvironmentError, KconfigError) as e: # Some long exception messages have extra newlines for better # formatting when reported as an unhandled exception. Strip them here. -- cgit v1.2.3