aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorChad Versace <chad.versace@linux.intel.com>2013-09-16 17:26:58 -0700
committerChad Versace <chad.versace@linux.intel.com>2013-09-16 18:22:34 -0700
commitc3cdc8f5fb3305e5d216cbdf5fe2ef813ce85a85 (patch)
tree11b8b4c44ed33051fcd40607c6501c83ed99392f /examples
parentc5e44c3bccb653be4ba12523619ff66f236be433 (diff)
downloadwaffle-c3cdc8f5fb3305e5d216cbdf5fe2ef813ce85a85.tar.gz
gl_basic: Replace positional args with --long-opts
For example, gl_basic glx gl 3.2 becomes gl_basic --platform=glx --api=gl --version=3.2 To accomplish this, replace handrolled argument parsing with getopt. Signed-off-by: Chad Versace <chad.versace@linux.intel.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/gl_basic.c140
1 files changed, 88 insertions, 52 deletions
diff --git a/examples/gl_basic.c b/examples/gl_basic.c
index 0586061..90acb1c 100644
--- a/examples/gl_basic.c
+++ b/examples/gl_basic.c
@@ -35,6 +35,7 @@
#define _POSIX_C_SOURCE 199309L // glibc feature macro for nanosleep.
+#include <getopt.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
@@ -54,22 +55,34 @@ removeXcodeArgs(int *argc, char **argv);
static const char *usage_message =
"usage:\n"
- " gl_basic <platform> <context_api> [version] [profile]\n"
- "\n"
- "arguments:\n"
- " platform: One of android, cgl, gbm, glx, wayland, x11_egl.\n"
- " context_api: One of gl, gles1, gles2, gles3.\n"
- " version: In form \"major.minor\".\n"
- " profile: One of core, compat, or none.\n"
+ " gl_basic --platform=android|cgl|gbm|glx|wayland|x11_egl\n"
+ " --api=gl|gles1|gles2|gles3\n"
+ " [--version=MAJOR.MINOR]\n"
+ " [--profile=core|compat|none]\n"
"\n"
"examples:\n"
- " gl_basic glx gl\n"
- " gl_basic x11_egl gl 3.2 core\n"
- " gl_basic wayland gles3\n"
+ " gl_basic --platform=glx --api=gl\n"
+ " gl_basic --platform=x11_egl --api=gl --version=3.2 --profile=core\n"
+ " gl_basic --platform=wayland --api=gles3\n"
"\n"
"description:\n"
" Create a window. Fill it with red, green, then blue.\n";
+enum {
+ OPT_PLATFORM = 1,
+ OPT_API,
+ OPT_VERSION,
+ OPT_PROFILE,
+};
+
+static const struct option get_opts[] = {
+ { .name = "platform", .has_arg = required_argument, .val = OPT_PLATFORM },
+ { .name = "api", .has_arg = required_argument, .val = OPT_API },
+ { .name = "version", .has_arg = required_argument, .val = OPT_VERSION },
+ { .name = "profile", .has_arg = required_argument, .val = OPT_PROFILE },
+ { 0 },
+};
+
/// @defgroup Error handlers
/// @{
///
@@ -224,65 +237,85 @@ enum_map_translate_str(
static bool
parse_args(int argc, char *argv[], struct options *opts)
{
- const char *arg;
bool ok;
+ bool loop_get_opt = true;
#ifdef __APPLE__
removeXcodeArgs(&argc, argv);
#endif
- if (argc < 3)
- usage_error_printf("not enough arguments");
-
// Set some context attrs to invalid values.
opts->context_profile = -1;
opts->context_version = -1;
- // Set platform.
- arg = argv[1];
- ok = enum_map_translate_str(platform_map, arg, &opts->platform);
- if (!ok) {
- usage_error_printf("'%s' is not a valid platform", arg);
+ while (loop_get_opt) {
+ int opt = getopt_long(argc, argv, "", get_opts, NULL);
+ switch (opt) {
+ case -1:
+ loop_get_opt = false;
+ break;
+ case '?':
+ goto error_unrecognized_arg;
+ case OPT_PLATFORM:
+ ok = enum_map_translate_str(platform_map, optarg,
+ &opts->platform);
+ if (!ok) {
+ usage_error_printf("'%s' is not a valid platform",
+ optarg);
+ }
+ break;
+ case OPT_API:
+ ok = enum_map_translate_str(context_api_map, optarg,
+ &opts->context_api);
+ if (!ok) {
+ usage_error_printf("'%s' is not a valid API for a GL "
+ "context", optarg);
+ }
+ break;
+ case OPT_VERSION: {
+ int major;
+ int minor;
+ int match_count;
+
+ match_count = sscanf(optarg, "%d.%d", &major, &minor);
+ if (match_count != 2) {
+ usage_error_printf("'%s' is not a valid GL version",
+ optarg);
+ }
+ opts->context_version = 10 * major + minor;
+ break;
+ }
+ case OPT_PROFILE:
+ if (strcmp(optarg, "none") == 0) {
+ opts->context_profile = WAFFLE_NONE;
+ } else if (strcmp(optarg, "core") == 0) {
+ opts->context_profile = WAFFLE_CONTEXT_CORE_PROFILE;
+ } else if (strcmp(optarg, "compat") == 0) {
+ opts->context_profile = WAFFLE_CONTEXT_COMPATIBILITY_PROFILE;
+ } else {
+ usage_error_printf("'%s' is not a valid GL profile",
+ optarg);
+ }
+ break;
+ default:
+ abort();
+ loop_get_opt = false;
+ break;
+ }
}
- // Set context_api.
- arg = argv[2];
- ok = enum_map_translate_str(context_api_map, arg, &opts->context_api);
- if (!ok) {
- usage_error_printf("'%s' is not a valid API for a GL context", arg);
+ if (optind < argc) {
+ goto error_unrecognized_arg;
}
- // Set context_version.
- if (argc >= 4) {
- int major;
- int minor;
- int match_count;
-
- arg = argv[3];
- match_count = sscanf(arg, "%d.%d", &major, &minor);
- if (match_count != 2) {
- usage_error_printf("'%s' is not a valid GL version", arg);
- }
- opts->context_version = 10 * major + minor;
+ if (!opts->platform) {
+ usage_error_printf("--platform is required");
}
- // Set context_profile.
- if (argc >= 5) {
- arg = argv[4];
- if (strcmp(arg, "none") == 0)
- opts->context_profile = WAFFLE_NONE;
- else if (strcmp(arg, "core") == 0)
- opts->context_profile = WAFFLE_CONTEXT_CORE_PROFILE;
- else if (strcmp(arg, "compat") == 0)
- opts->context_profile = WAFFLE_CONTEXT_COMPATIBILITY_PROFILE;
- else {
- usage_error_printf("'%s' is not a valid GL profile", arg);
- }
+ if (!opts->context_api) {
+ usage_error_printf("--api is required");
}
- if (argc >= 6)
- usage_error_printf("too many arguments");
-
// Set dl.
switch (opts->context_api) {
case WAFFLE_CONTEXT_OPENGL: opts->dl = WAFFLE_DL_OPENGL; break;
@@ -295,6 +328,9 @@ parse_args(int argc, char *argv[], struct options *opts)
}
return true;
+
+error_unrecognized_arg:
+ usage_error_printf("unrecognized option '%s'", optarg);
}
/// @}
@@ -407,7 +443,7 @@ main(int argc, char **argv)
bool ok;
int i;
- struct options opts;
+ struct options opts = {0};
int32_t init_attrib_list[3];
int32_t config_attrib_list[64];