diff --git a/llvm/utils/lit/lit/cl_arguments.py b/llvm/utils/lit/lit/cl_arguments.py --- a/llvm/utils/lit/lit/cl_arguments.py +++ b/llvm/utils/lit/lit/cl_arguments.py @@ -8,7 +8,7 @@ def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('test_paths', - nargs='*', + nargs='+', help='Files or paths to include in the test suite') parser.add_argument("--version", @@ -20,13 +20,12 @@ dest="numWorkers", metavar="N", help="Number of workers used for testing", - type=int, - default=None) + type=_positive_int, + default=lit.util.detectCPUs()) parser.add_argument("--config-prefix", dest="configPrefix", metavar="NAME", help="Prefix for 'lit' config files", - action="store", default=None) parser.add_argument("-D", "--param", dest="userParameters", @@ -66,7 +65,6 @@ format_group.add_argument("-o", "--output", dest="output_path", help="Write test results to the provided path", - action="store", metavar="PATH") format_group.add_argument("--no-progress-bar", dest="useProgressBar", @@ -128,8 +126,7 @@ execution_group.add_argument("--max-failures", dest="maxFailures", help="Stop execution after the given number of failures.", - action="store", - type=int, + type=_positive_int, default=None) selection_group = parser.add_argument_group("Test Selection") @@ -137,14 +134,12 @@ dest="maxTests", metavar="N", help="Maximum number of tests to run", - action="store", type=int, default=None) selection_group.add_argument("--max-time", dest="maxTime", metavar="N", help="Maximum time to spend testing (in seconds)", - action="store", type=float, default=None) selection_group.add_argument("--shuffle", @@ -158,19 +153,18 @@ selection_group.add_argument("--filter", metavar="REGEX", help="Only run tests with paths matching the given regular expression", - action="store", default=os.environ.get("LIT_FILTER")) - selection_group.add_argument("--num-shards", dest="numShards", metavar="M", + selection_group.add_argument("--num-shards", + dest="numShards", + metavar="M", help="Split testsuite into M pieces and only run one", - action="store", - type=int, + type=_positive_int, default=os.environ.get("LIT_NUM_SHARDS")) selection_group.add_argument("--run-shard", dest="runShard", metavar="N", help="Run shard #N of the testsuite", - action="store", - type=int, + type=_positive_int, default=os.environ.get("LIT_RUN_SHARD")) debug_group = parser.add_argument_group("Debug and Experimental Options") @@ -192,27 +186,27 @@ opts = parser.parse_args(sys.argv[1:] + shlex.split(os.environ.get("LIT_OPTS", ""))) - # Validate options - if not opts.test_paths: - parser.error('No inputs specified') - - if opts.numWorkers is None: - opts.numWorkers = lit.util.detectCPUs() - elif opts.numWorkers <= 0: - parser.error("Option '--workers' or '-j' requires positive integer") - - if opts.maxFailures is not None and opts.maxFailures <= 0: - parser.error("Option '--max-failures' requires positive integer") - + # Validate command line options if opts.echoAllCommands: opts.showOutput = True - if (opts.numShards is not None) or (opts.runShard is not None): - if (opts.numShards is None) or (opts.runShard is None): + if opts.numShards or opts.runShard: + if not opts.numShards or not opts.runShard: parser.error("--num-shards and --run-shard must be used together") - if opts.numShards <= 0: - parser.error("--num-shards must be positive") - if (opts.runShard < 1) or (opts.runShard > opts.numShards): + if opts.runShard > opts.numShards: parser.error("--run-shard must be between 1 and --num-shards (inclusive)") return opts + +def _positive_int(arg): + try: + n = int(arg) + except ValueError: + raise _arg_error('positive integer', arg) + if n <= 0: + raise _arg_error('positive integer', arg) + return n + +def _arg_error(desc, arg): + msg = "requires %s, but found '%s'" % (desc, arg) + return argparse.ArgumentTypeError(msg) diff --git a/llvm/utils/lit/tests/max-failures.py b/llvm/utils/lit/tests/max-failures.py --- a/llvm/utils/lit/tests/max-failures.py +++ b/llvm/utils/lit/tests/max-failures.py @@ -11,4 +11,4 @@ # CHECK: Failing Tests (31) # CHECK: Failing Tests (1) # CHECK: Failing Tests (2) -# CHECK: error: Option '--max-failures' requires positive integer +# CHECK: error: argument --max-failures: requires positive integer, but found '0' diff --git a/llvm/utils/lit/tests/selecting.py b/llvm/utils/lit/tests/selecting.py --- a/llvm/utils/lit/tests/selecting.py +++ b/llvm/utils/lit/tests/selecting.py @@ -87,7 +87,7 @@ # # RUN: not %{lit} --num-shards 0 --run-shard 2 %{inputs}/discovery >%t.out 2>%t.err # RUN: FileCheck --check-prefix=CHECK-SHARD-ERR < %t.err %s -# CHECK-SHARD-ERR: error: --num-shards must be positive +# CHECK-SHARD-ERR: error: argument --num-shards: requires positive integer, but found '0' # # RUN: not %{lit} --num-shards 3 --run-shard 4 %{inputs}/discovery >%t.out 2>%t.err # RUN: FileCheck --check-prefix=CHECK-SHARD-ERR2 < %t.err %s