static options_t *parse_options(int argc, char *argv[]) { options_t *options = malloc_s(sizeof(options_t)); memset(options, 0, sizeof(options_t)); /* Parameters for getopt_long() function */ static const char short_options[] = "f:c:o:v"; static const struct option long_options[] = { { "format", required_argument, NULL, 'f' }, { "certoutform", required_argument, NULL, 'c' }, { "certout", required_argument, NULL, 'o' }, { "help", no_argument, NULL, 1 }, { "version", no_argument, NULL, 'v' }, { NULL, 0, NULL, 0 } }; int c, ind; while ((c = getopt_long(argc, argv, short_options, long_options, &ind))) { if (c < 0) break; switch (c) { case 1: // --help option usage(); exit(EXIT_SUCCESS); case 'f': if (output_set_format_by_name(optarg) < 0) EXIT_ERROR("invalid format option"); break; case 'v': printf("%s %s\n%s\n", PROGRAM, TOOLKIT, COPY); exit(EXIT_SUCCESS); case 'c': options->certoutform = parse_certoutform(optarg); break; case 'o': options->certout = parse_certout(optarg); break; default: fprintf(stderr, "%s: try '--help' for more information\n", PROGRAM); exit(EXIT_FAILURE); } } return options; }
static options_t *parse_options(int argc, char *argv[]) { options_t *options = malloc_s(sizeof(options_t)); memset(options, 0, sizeof(options_t)); // parameters for getopt_long() function static const char short_options[] = "f:a:c:h:s:V"; static const struct option long_options[] = { { "help", no_argument, NULL, 1 }, { "format", required_argument, NULL, 'f' }, { "all", no_argument, NULL, 'a' }, { "content", no_argument, NULL, 'c' }, { "header", required_argument, NULL, 'h' }, { "section-name", required_argument, NULL, 's' }, { "section-index", required_argument, NULL, 2 }, { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; // Setting the default option options->content = true; int c, ind; while ((c = getopt_long(argc, argv, short_options, long_options, &ind))) { if (c < 0) break; switch (c) { case 1: // --help option usage(); exit(EXIT_SUCCESS); case 'f': if (output_set_format_by_name(optarg) < 0) EXIT_ERROR("invalid format option"); break; case 'a': options->all = true; break; case 'c': // default options->all = false; //TODO remover? options->content = true; break; case 's': options->all = false; options->headers.all = false; // TODO: How do we need to handle non-ascii names? options->sections.name = strdup(optarg); break; case 2: options->all = false; options->headers.all = false; options->sections.index = strtol(optarg, NULL, 10); if (options->sections.index < 1 || options->sections.index > MAX_SECTIONS) { EXIT_ERROR("Bad argument for section-index,"); } break; case 'V': printf("%s %s\n%s\n", PROGRAM, TOOLKIT, COPY); exit(EXIT_SUCCESS); case 'h': options->all = false; options->headers.all = false; parse_header_name(options, optarg); break; default: fprintf(stderr, "%s: try '--help' for more information\n", PROGRAM); exit(EXIT_FAILURE); } } // TODO: Warn about simultaneous usage of -h, -s, and --section-index. return options; }
static options_t *parse_options(int argc, char *argv[]) { options_t *options = malloc_s(sizeof(options_t)); memset(options, 0, sizeof(options_t)); /* Parameters for getopt_long() function */ static const char short_options[] = "a:f:isxvV"; static const struct option long_options[] = { { "all", required_argument, NULL, 'a' }, { "format", required_argument, NULL, 'f' }, { "info", no_argument, NULL, 'i' }, { "statistics", no_argument, NULL, 's' }, { "extract", no_argument, NULL, 'x' }, { "file-version", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'V' }, { "help", no_argument, NULL, 1 }, { NULL, 0, NULL, 0 } }; int c, ind; while ((c = getopt_long(argc, argv, short_options, long_options, &ind))) { if (c < 0) break; switch (c) { case 'a': options->all = true; break; case 'f': if (output_set_format_by_name(optarg) < 0) EXIT_ERROR("invalid format option"); break; case 'i': options->info = true; break; case 's': options->statistics = true; break; case 'x': options->extract = true; break; case 'v': options->version = true; break; case 'V': printf("%s %s\n%s\n", PROGRAM, TOOLKIT, COPY); exit(EXIT_SUCCESS); case 1: // --help option usage(); exit(EXIT_SUCCESS); default: fprintf(stderr, "%s: try '--help' for more information\n", PROGRAM); exit(EXIT_FAILURE); } } return options; }