static bool parse_opts(int argc, char* argv[]) { int option_index = 0; int c; strcpy(g_host, "127.0.0.1"); while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) { switch (c) { case 'h': if (strlen(optarg) >= sizeof(g_host)) { error("ERROR: host exceeds max length"); return false; } strcpy(g_host, optarg); error("host: %s", g_host); break; case 'p': g_port = atoi(optarg); break; case 'U': if (strlen(optarg) >= sizeof(g_user)) { error("ERROR: user exceeds max length"); return false; } strcpy(g_user, optarg); error("user: %s", g_user); break; case 'P': as_password_prompt_hash(optarg, g_password); break; case 'S': // Exclude all but the specified suite from the plan. atf_suite_filter(optarg); break; case 'T': // Exclude all but the specified test. atf_test_filter(optarg); break; default: error("unrecognized options"); return false; } } return true; }
//------------------------------------------------ // Parse command line options. // bool example_get_opts(int argc, char* argv[], int which_opts) { strcpy(g_host, DEFAULT_HOST); g_port = DEFAULT_PORT; strcpy(g_namespace, DEFAULT_NAMESPACE); strcpy(g_set, DEFAULT_SET); strcpy(g_key_str, DEFAULT_KEY_STR); g_n_keys = DEFAULT_NUM_KEYS; const char* short_options; struct option* long_options; if (which_opts) { short_options = short_options_multikey; long_options = long_options_multikey; } else { short_options = short_options_basic; long_options = long_options_basic; } int option_index = 0; int c; while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) { switch (c) { case 'h': if (strlen(optarg) >= sizeof(g_host)) { LOG("ERROR: host exceeds max length"); return false; } strcpy(g_host, optarg); break; case 'p': g_port = atoi(optarg); break; case 'U': strcpy(g_user, optarg); break; case 'P': as_password_prompt_hash(optarg, g_password); break; case 'n': if (strlen(optarg) >= sizeof(g_namespace)) { LOG("ERROR: namespace exceeds max length"); return false; } strcpy(g_namespace, optarg); break; case 's': if (strlen(optarg) >= sizeof(g_set)) { LOG("ERROR: set name exceeds max length"); return false; } strcpy(g_set, optarg); break; case 'k': if (strlen(optarg) >= sizeof(g_key_str)) { LOG("ERROR: key string exceeds max length"); return false; } strcpy(g_key_str, optarg); break; case 'K': g_n_keys = atoi(optarg); break; default: usage(short_options); return false; } } if (strchr(short_options, 'h')) { LOG("host: %s", g_host); } if (strchr(short_options, 'p')) { LOG("port: %d", g_port); } if (strchr(short_options, 'U')) { LOG("user: %s", g_user); } if (strchr(short_options, 'n')) { LOG("namespace: %s", g_namespace); } if (strchr(short_options, 's')) { LOG("set name: %s", g_set); } if (strchr(short_options, 'k')) { LOG("key (string): %s", g_key_str); } if (strchr(short_options, 'K')) { LOG("number of keys: %u", g_n_keys); } // Initialize the test as_key object. We won't need to destroy it since it // isn't being created on the heap or with an external as_key_value. as_key_init_str(&g_key, g_namespace, g_set, g_key_str); return true; }