summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorImre Deak <imre.deak@intel.com>2013-08-26 17:02:49 +0300
committerImre Deak <imre.deak@intel.com>2013-08-26 17:18:38 +0300
commit40cc4b8d103f4406bb51334c1338b0ae774eed17 (patch)
tree250364075472912ae22d1e98e8686a4d1ea138e5
parent04a4fae32e252666aa12729d232acbc20a0ea8ce (diff)
lib/drmtest: fix handling of -h --help argument parsing
So far we handled -h or --help arguments properly only if the test called igt_subtest_init_parse_opts(). Fix this for igt_subtest_init() callers too. Make sure we still don't exit for any other unknown options, which the caller may parse with a second getopt scan. Signed-off-by: Imre Deak <imre.deak@intel.com>
-rw-r--r--lib/drmtest.c46
1 files changed, 26 insertions, 20 deletions
diff --git a/lib/drmtest.c b/lib/drmtest.c
index a01ccfa0..ad91c657 100644
--- a/lib/drmtest.c
+++ b/lib/drmtest.c
@@ -681,8 +681,9 @@ static void print_usage(const char *command_str, const char *help_str,
fprintf(f, "Usage: %s [OPTIONS]\n"
" --list-subtests\n"
- " --run-subtest <pattern>\n"
- "%s\n", command_str, help_str);
+ " --run-subtest <pattern>\n", command_str);
+ if (help_str)
+ fprintf(f, "%s\n", help_str);
}
int igt_subtest_init_parse_opts(int argc, char **argv,
@@ -695,10 +696,8 @@ int igt_subtest_init_parse_opts(int argc, char **argv,
static struct option long_options[] = {
{"list-subtests", 0, 0, 'l'},
{"run-subtest", 1, 0, 'r'},
- {NULL, 0, 0, 0,}
+ {"help", 0, 0, 'h'},
};
- struct option help_opt =
- {"help", 0, 0, 'h'};
const char *command_str;
char *short_opts;
struct option *combined_opts;
@@ -718,27 +717,18 @@ int igt_subtest_init_parse_opts(int argc, char **argv,
all_opt_count++;
extra_opt_count = all_opt_count;
- /* Add space for a long help option for any passed-in help string */
- if (help_str)
- all_opt_count++;
- /* Add space for the subtest long options (and the final NULL entry) */
all_opt_count += ARRAY_SIZE(long_options);
combined_opts = malloc(all_opt_count * sizeof(*combined_opts));
memcpy(combined_opts, extra_long_opts,
extra_opt_count * sizeof(*combined_opts));
- if (help_str) {
- combined_opts[extra_opt_count] = help_opt;
- extra_opt_count++;
- }
/* Copy the subtest long options (and the final NULL entry) */
memcpy(&combined_opts[extra_opt_count], long_options,
ARRAY_SIZE(long_options) * sizeof(*combined_opts));
- ret = asprintf(&short_opts, "%s%s",
- extra_short_opts ? extra_short_opts : "",
- help_str ? "h" : "");
+ ret = asprintf(&short_opts, "%sh",
+ extra_short_opts ? extra_short_opts : "");
assert(ret >= 0);
while ((c = getopt_long(argc, argv, short_opts, combined_opts,
@@ -752,11 +742,22 @@ int igt_subtest_init_parse_opts(int argc, char **argv,
if (!list_subtests)
run_single_subtest = strdup(optarg);
break;
- case '?':
case 'h':
- print_usage(command_str, help_str, c == '?');
- ret = c == '?' ? -2 : -1;
+ print_usage(command_str, help_str, false);
+ ret = -2;
goto out;
+ case '?':
+ if (opterr) {
+ print_usage(command_str, help_str, true);
+ ret = -1;
+ goto out;
+ }
+ /*
+ * Just ignore the error, since the unknown argument
+ * can be something the caller understands and will
+ * parse by doing a second getopt scanning.
+ */
+ break;
default:
ret = extra_opt_handler(c, option_index);
if (ret)
@@ -770,10 +771,15 @@ out:
void igt_subtest_init(int argc, char **argv)
{
+ int ret;
+
/* supress getopt errors about unknown options */
opterr = 0;
- (void)igt_subtest_init_parse_opts(argc, argv, NULL, NULL, NULL, NULL);
+ ret = igt_subtest_init_parse_opts(argc, argv, NULL, NULL, NULL, NULL);
+ if (ret < 0)
+ /* exit with no error for -h/--help */
+ exit(ret == -2 ? 0 : ret);
/* reset opt parsing */
optind = 1;