diff options
| author | Ulf Magnusson <ulfalizer@gmail.com> | 2019-10-10 09:08:37 +0200 |
|---|---|---|
| committer | Ulf Magnusson <ulfalizer@gmail.com> | 2019-10-10 10:07:29 +0200 |
| commit | e016deb4bbfae014ada1808abaeeb30558cac209 (patch) | |
| tree | 96ac8582ac84224efc3994bfd9e588ae662c6945 /kconfiglib.py | |
| parent | 275ddef8dfdc2b404e172d8bdbbc10e5c68b4843 (diff) | |
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.
Diffstat (limited to 'kconfiglib.py')
| -rw-r--r-- | kconfiglib.py | 34 |
1 files changed, 24 insertions, 10 deletions
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. |
