int get_option(char **argvp[], const struct option *options, char **arg) { char **argv = *argvp; const char *opt = *argv; int i; *arg = NULL; if (opt == NULL || opt[0] != '-' || opt[1] == 0) return -1; if (opt[1] == '-') { if (opt[2] == 0) { /* '--' => no more options */ *argvp = argv + 1; return -1; } i = long_option(opt + 2, options); } else if (opt[2]) { return -1; } else { i = short_option(opt[1], options); } argv++; if (options[i].has_arg) { if (*argv == NULL) die("option `%s' requires an argument\n", opt); *arg = *argv++; } *argvp = argv; return i; }
//----------------------------------------------------------------------------- int32_t Args::getopt() { // Always reset optarg m_optarg = NULL; // End of arguments if (m_optind >= m_scope) { return -1; } switch (option_type(m_argv[m_optind])) { case AOT_SHORT: { return short_option(m_argv[m_optind]); } case AOT_LONG: { return long_option(m_argv[m_optind]); } case AOT_NOT_OPTION: { not_option(); return getopt(); } default: { return '?'; } } }
void p11_tool_usage (const p11_tool_desc *usages, const struct option *longopts) { const struct option *longopt; const int indent = 22; const char *long_name; const char *description; const char *next; char short_name; int spaces; int len; int i; for (i = 0; usages[i].text != NULL; i++) { /* If no option, then this is a heading */ if (!usages[i].option) { printf ("%s\n\n", usages[i].text); continue; } longopt = find_option (longopts, usages[i].option); long_name = longopt ? longopt->name : NULL; short_name = short_option (usages[i].option); description = usages[i].text; if (short_name && long_name) len = printf (" -%c, --%s", (int)short_name, long_name); else if (long_name) len = printf (" --%s", long_name); else len = printf (" -%c", (int)short_name); if (longopt && longopt->has_arg) len += printf ("%s<%s>", long_name ? "=" : " ", usages[i].arg ? usages[i].arg : "..."); if (len < indent) { spaces = indent - len; } else { printf ("\n"); spaces = indent; } while (description) { while (spaces-- > 0) fputc (' ', stdout); next = strchr (description, '\n'); if (next) { next += 1; printf ("%.*s", (int)(next - description), description); description = next; spaces = indent; } else { printf ("%s\n", description); break; } } } }
int parse_options(const struct weston_option *options, int count, int *argc, char *argv[]) { int i, j; for (i = 1, j = 1; i < *argc; i++) { if (argv[i][0] == '-') { if (argv[i][1] == '-') { /* Long option, e.g. --foo or --foo=bar */ if (long_option(options, count, argv[i])) continue; } else { /* Short option, e.g -f or -f42 */ if (short_option(options, count, argv[i])) continue; /* ...also handle -f 42 */ if (i+1 < *argc && short_option_with_arg(options, count, argv[i], argv[i+1])) { i++; continue; } } } argv[j++] = argv[i]; } argv[j] = NULL; *argc = j; return j; }
int p11_tool_getopt (int argc, char *argv[], const struct option *longopts) { p11_buffer buf; int ret; char opt; int i; if (!p11_buffer_init_null (&buf, 64)) return_val_if_reached (-1); for (i = 0; longopts[i].name != NULL; i++) { opt = short_option (longopts[i].val); if (opt != 0) { p11_buffer_add (&buf, &opt, 1); assert (longopts[i].has_arg != optional_argument); if (longopts[i].has_arg == required_argument) p11_buffer_add (&buf, ":", 1); } } ret = getopt_long (argc, argv, buf.data, longopts, NULL); p11_buffer_uninit (&buf); return ret; }