From: Imre Deak <imre.deak@intel.com>
To: intel-gfx@lists.freedesktop.org
Subject: [igt PATCH 3/4] lib: add subtest extra command line option handling
Date: Mon, 5 Aug 2013 14:45:25 +0300 [thread overview]
Message-ID: <1375703126-19323-4-git-send-email-imre.deak@intel.com> (raw)
In-Reply-To: <1375703126-19323-1-git-send-email-imre.deak@intel.com>
At the moment any command line option handling done by tests will
interfere with the option handling of the subtest interface. To fix this
add a new version of the subtest_init function accepting optional short
and long command line options. Merge these together with the subtest
interface's own long options and handle both together in the same
getopt_long call.
Signed-off-by: Imre Deak <imre.deak@intel.com>
---
lib/drmtest.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
lib/drmtest.h | 6 +++++
2 files changed, 81 insertions(+), 10 deletions(-)
diff --git a/lib/drmtest.c b/lib/drmtest.c
index afbaa35..7a68091 100644
--- a/lib/drmtest.c
+++ b/lib/drmtest.c
@@ -657,7 +657,21 @@ void drmtest_stop_signal_helper(void)
static bool list_subtests = false;
static char *run_single_subtest = NULL;
-void drmtest_subtest_init(int argc, char **argv)
+static void print_usage(const char *command_str, const char *help_str,
+ bool output_on_stderr)
+{
+ FILE *f = output_on_stderr ? stderr : stdout;
+
+ fprintf(f, "Usage: %s [OPTIONS]\n"
+ " --list-subtests\n"
+ " --run-subtest <pattern>\n"
+ "%s\n", command_str, help_str);
+}
+
+int drmtest_subtest_init_parse_opts(int argc, char **argv, const char *opts,
+ struct option *long_opts,
+ const char *help_str,
+ drmtest_opt_handler_t opt_handler)
{
int c, option_index = 0;
static struct option long_options[] = {
@@ -665,24 +679,75 @@ void drmtest_subtest_init(int argc, char **argv)
{"run-subtest", 1, 0, 'r'},
{NULL, 0, 0, 0,}
};
+ struct option help_opt =
+ {"help", 0, 0, 'h'};
+ const char *command_str;
+ char *short_opts;
+ struct option *combined_opts;
+ int extra_opts;
+ int all_opts;
+ int ret = 0;
+
+ command_str = argv[0];
+ if (strrchr(command_str, '/'))
+ command_str = strrchr(command_str, '/') + 1;
+
+ all_opts = 0;
+ while (long_opts && long_opts[all_opts].name)
+ all_opts++;
+ extra_opts = all_opts;
+ if (help_str)
+ all_opts++;
+ all_opts += ARRAY_SIZE(long_options);
+
+ combined_opts = malloc(all_opts * sizeof(*combined_opts));
+ memcpy(combined_opts, long_opts, extra_opts * sizeof(*combined_opts));
+ if (help_str) {
+ combined_opts[extra_opts] = help_opt;
+ extra_opts++;
+ }
+ memcpy(&combined_opts[extra_opts], long_options,
+ ARRAY_SIZE(long_options) * sizeof(*combined_opts));
- /* supress getopt errors about unknown options */
- opterr = 0;
- /* restrict the option parsing to long option names to avoid collisions
- * with options the test declares */
- while((c = getopt_long(argc, argv, "",
- long_options, &option_index)) != -1) {
+ ret = asprintf(&short_opts, "%s%s",
+ opts ? opts : "", help_str ? "h" : "");
+ assert(ret >= 0);
+
+ while ((c = getopt_long(argc, argv, short_opts, combined_opts,
+ &option_index)) != -1) {
switch(c) {
case 'l':
- list_subtests = true;
- goto out;
+ if (!run_single_subtest)
+ list_subtests = true;
+ break;
case 'r':
- run_single_subtest = strdup(optarg);
+ if (!list_subtests)
+ run_single_subtest = strdup(optarg);
+ break;
+ case '?':
+ case 'h':
+ print_usage(command_str, help_str, c == '?');
+ ret = c == '?' ? -2 : -1;
goto out;
+ default:
+ ret = opt_handler(c, option_index);
+ if (ret)
+ goto out;
}
}
out:
+ return ret;
+}
+
+void drmtest_subtest_init(int argc, char **argv)
+{
+ /* supress getopt errors about unknown options */
+ opterr = 0;
+
+ (void)drmtest_subtest_init_parse_opts(argc, argv, NULL, NULL, NULL,
+ NULL);
+
/* reset opt parsing */
optind = 1;
}
diff --git a/lib/drmtest.h b/lib/drmtest.h
index 172bed5..5ca7e2e 100644
--- a/lib/drmtest.h
+++ b/lib/drmtest.h
@@ -94,6 +94,12 @@ void drmtest_progress(const char *header, uint64_t i, uint64_t total);
/* subtest infrastructure */
void drmtest_subtest_init(int argc, char **argv);
+typedef int (*drmtest_opt_handler_t)(int opt, int opt_index);
+struct option;
+int drmtest_subtest_init_parse_opts(int argc, char **argv, const char *opts,
+ struct option *long_opts,
+ const char *help_str,
+ drmtest_opt_handler_t opt_handler);
bool drmtest_run_subtest(const char *subtest_name);
bool drmtest_only_list_subtests(void);
--
1.8.3.2
next prev parent reply other threads:[~2013-08-05 11:45 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-05 11:45 [igt PATCH 0/4] add support for testing clone output configs Imre Deak
2013-08-05 11:45 ` [igt PATCH 1/4] lib: shorten DP/eDP connector names Imre Deak
2013-08-05 11:45 ` [igt PATCH 2/4] lib: handle SIGSEGV similarly to other error signals Imre Deak
2013-08-05 11:45 ` Imre Deak [this message]
2013-08-06 9:09 ` [igt PATCH 3/4] lib: add subtest extra command line option handling Daniel Vetter
2013-08-16 11:07 ` Imre Deak
2013-08-16 12:09 ` Daniel Vetter
2013-08-05 11:45 ` [igt PATCH 4/4] tests: add kms_setmode Imre Deak
2013-08-06 9:23 ` Daniel Vetter
2013-08-16 11:23 ` Imre Deak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1375703126-19323-4-git-send-email-imre.deak@intel.com \
--to=imre.deak@intel.com \
--cc=intel-gfx@lists.freedesktop.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).