scripts: Fixed conflicting -C/--compare vs -C/--context errors

Just by hiding -C/--context, -W/--width, --color from argparse unless
a related flag (-h/--help, -A/--annotate, etc) is found in sys.argv.

This is the same trick we use in test.py/bench.py/perf.py.

---

In other news my litmus test that the scripts work was broken.

This does _not_ error if a script errors:

  $ for f in scripts/*.py ; do $f --help ; done

An alternative that works is piping stdout to /dev/null, Python's
exceptions go to stderr by default:

  $ for f in scripts/*.py ; do $f --help >/dev/null ; done
This commit is contained in:
Christopher Haster
2026-02-12 19:27:24 -06:00
parent 93c85870e8
commit 4fe8d96101
3 changed files with 77 additions and 50 deletions
+27 -17
View File
@@ -1100,9 +1100,13 @@ def main(gcda_paths, *,
if __name__ == "__main__": if __name__ == "__main__":
import argparse import argparse
import sys import sys
import re
argparse.ArgumentParser._handle_conflict_ignore = lambda *_: None
argparse._ArgumentGroup._handle_conflict_ignore = lambda *_: None
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Find coverage info after running tests.", description="Find coverage info after running tests.",
allow_abbrev=False) allow_abbrev=False,
conflict_handler='ignore')
parser.add_argument( parser.add_argument(
'gcda_paths', 'gcda_paths',
nargs='*', nargs='*',
@@ -1274,22 +1278,28 @@ if __name__ == "__main__":
'-B', '--branches', '-B', '--branches',
action='store_true', action='store_true',
help="Show uncovered branches.") help="Show uncovered branches.")
parser.add_argument( if any(re.fullmatch(
'-C', '--context', '-[^-]*[hALB].*'
type=lambda x: int(x, 0), '|--help'
default=3, '|--annotate'
help="Show n additional lines of context. Defaults to 3.") '|--lines'
parser.add_argument( '|--branches', a) for a in sys.argv):
'-W', '--width', parser.add_argument(
type=lambda x: int(x, 0), '-C', '--context',
default=80, type=lambda x: int(x, 0),
help="Assume source is styled with this many columns. Defaults " default=3,
"to 80.") help="Show n additional lines of context. Defaults to 3.")
parser.add_argument( parser.add_argument(
'--color', '-W', '--width',
choices=['never', 'always', 'auto'], type=lambda x: int(x, 0),
default='auto', default=80,
help="When to use terminal colors. Defaults to 'auto'.") help="Assume source is styled with this many columns. "
"Defaults to 80.")
parser.add_argument(
'--color',
choices=['never', 'always', 'auto'],
default='auto',
help="When to use terminal colors. Defaults to 'auto'.")
parser.add_argument( parser.add_argument(
'-e', '--error-on-lines', '-e', '--error-on-lines',
action='store_true', action='store_true',
+21 -16
View File
@@ -1871,22 +1871,27 @@ if __name__ == "__main__":
help="Show lines with samples above this threshold as a percent " help="Show lines with samples above this threshold as a percent "
"of all lines. Defaults to " "of all lines. Defaults to "
"%s." % ','.join(str(t) for t in THRESHOLD)) "%s." % ','.join(str(t) for t in THRESHOLD))
parser.add_argument( if any(re.fullmatch(
'-C', '--context', '-[^-]*[hAT].*'
type=lambda x: int(x, 0), '|--help'
default=3, '|--annotate'
help="Show n additional lines of context. Defaults to 3.") '|--threshold', a) for a in sys.argv):
parser.add_argument( parser.add_argument(
'-W', '--width', '-C', '--context',
type=lambda x: int(x, 0), type=lambda x: int(x, 0),
default=80, default=3,
help="Assume source is styled with this many columns. Defaults " help="Show n additional lines of context. Defaults to 3.")
"to 80.") parser.add_argument(
parser.add_argument( '-W', '--width',
'--color', type=lambda x: int(x, 0),
choices=['never', 'always', 'auto'], default=80,
default='auto', help="Assume source is styled with this many columns. "
help="When to use terminal colors. Defaults to 'auto'.") "Defaults to 80.")
parser.add_argument(
'--color',
choices=['never', 'always', 'auto'],
default='auto',
help="When to use terminal colors. Defaults to 'auto'.")
parser.add_argument( parser.add_argument(
'-j', '--jobs', '-j', '--jobs',
nargs='?', nargs='?',
+29 -17
View File
@@ -1644,10 +1644,14 @@ def main(**args):
if __name__ == "__main__": if __name__ == "__main__":
import argparse import argparse
import sys import sys
import re
argparse.ArgumentParser._handle_conflict_ignore = lambda *_: None
argparse._ArgumentGroup._handle_conflict_ignore = lambda *_: None
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Aggregate and report call-stack propagated " description="Aggregate and report call-stack propagated "
"block-device operations from trace output.", "block-device operations from trace output.",
allow_abbrev=False) allow_abbrev=False,
conflict_handler='ignore')
class AppendPath(argparse.Action): class AppendPath(argparse.Action):
def __call__(self, parser, namespace, value, option): def __call__(self, parser, namespace, value, option):
if getattr(namespace, 'paths', None) is None: if getattr(namespace, 'paths', None) is None:
@@ -1887,22 +1891,30 @@ if __name__ == "__main__":
help="Show lines with erases above this threshold as a percent " help="Show lines with erases above this threshold as a percent "
"of all lines. Defaults to " "of all lines. Defaults to "
"%s." % ','.join(str(t) for t in THRESHOLD)) "%s." % ','.join(str(t) for t in THRESHOLD))
parser.add_argument( if any(re.fullmatch(
'-C', '--context', '-[^-]*[hAT].*'
type=lambda x: int(x, 0), '|--help'
default=3, '|--annotate'
help="Show n additional lines of context. Defaults to 3.") '|--threshold'
parser.add_argument( '|--read-threshold'
'-W', '--width', '|--prog-threshold'
type=lambda x: int(x, 0), '|--erase-threshold', a) for a in sys.argv):
default=80, parser.add_argument(
help="Assume source is styled with this many columns. Defaults " '-C', '--context',
"to 80.") type=lambda x: int(x, 0),
parser.add_argument( default=3,
'--color', help="Show n additional lines of context. Defaults to 3.")
choices=['never', 'always', 'auto'], parser.add_argument(
default='auto', '-W', '--width',
help="When to use terminal colors. Defaults to 'auto'.") type=lambda x: int(x, 0),
default=80,
help="Assume source is styled with this many columns. "
"Defaults to 80.")
parser.add_argument(
'--color',
choices=['never', 'always', 'auto'],
default='auto',
help="When to use terminal colors. Defaults to 'auto'.")
parser.add_argument( parser.add_argument(
'-j', '--jobs', '-j', '--jobs',
nargs='?', nargs='?',