summaryrefslogtreecommitdiff
path: root/tools/patman/cmdline.py
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2025-05-10 13:05:07 +0200
committerSimon Glass <sjg@chromium.org>2025-05-27 10:07:42 +0100
commit0ab5e441e7f7433cad6bf16ea509dfc415a18b4f (patch)
tree47de89266f9066e8b3d743d2dcd14e2758bf5b05 /tools/patman/cmdline.py
parent42588591e2e7a62258cfa7b487b0accad5db3fca (diff)
patman: Enhance implementation of file-based defaults
Patman allows defaults for its command-line flags to be read from a file. The implementation of this does not fully work with subcommands, since we don't want a default for those. Also, it relies on being able to parse any sort of cmdline in order to figure out the options that are available. But in some cases, the cmdline may not parse, e.g. if there are required options, or conflicting options. Add a class which can be safely used to parse any cmdline, since it allows execution to continue even when errors are obtained. Use this to determine the defaults, being careful to skip sub/commands. Another way to handle all of this would be to maintain the defaults separately and insert them into the parser manually. For now, I'm not sure which is best. Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'tools/patman/cmdline.py')
-rw-r--r--tools/patman/cmdline.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/tools/patman/cmdline.py b/tools/patman/cmdline.py
index 108fa52d820..8e10c7bb14f 100644
--- a/tools/patman/cmdline.py
+++ b/tools/patman/cmdline.py
@@ -21,6 +21,25 @@ PATMAN_DIR = pathlib.Path(__file__).parent
HAS_TESTS = os.path.exists(PATMAN_DIR / "func_test.py")
+class ErrorCatchingArgumentParser(argparse.ArgumentParser):
+ def __init__(self, **kwargs):
+ self.exit_state = None
+ self.catch_error = False
+ super().__init__(**kwargs)
+
+ def error(self, message):
+ if self.catch_error:
+ self.message = message
+ else:
+ super().error(message)
+
+ def exit(self, status=0, message=None):
+ if self.catch_error:
+ self.exit_state = True
+ else:
+ super().exit(status, message)
+
+
def add_send_args(par):
"""Add arguments for the 'send' command
@@ -150,7 +169,7 @@ def setup_parser():
them as specified by tags you place in the commits. Use -n to do a dry
run first.'''
- parser = argparse.ArgumentParser(epilog=epilog)
+ parser = ErrorCatchingArgumentParser(epilog=epilog)
parser.add_argument(
'-D', '--debug', action='store_true',
help='Enabling debugging (provides a full traceback on error)')