1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
/*
* util_opt_example - Example program for util_opt
*
* Copyright IBM Corp. 2016, 2017
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
//! [code]
#include <stdio.h>
#include <stdlib.h>
#include "lib/util_opt.h"
#define OPT_NOSHORT 256
/*
* Define the command line options
*/
static struct util_opt opt_vec[] = {
UTIL_OPT_SECTION("OPTION WITHOUT ARGUMENTS"),
/* Our own options */
{
.option = { "single", no_argument, NULL, 's'},
.desc = "A single option without an argument",
},
UTIL_OPT_SECTION("OPTIONS WITH ARGUMENTS"),
{
.option = { "req_arg", required_argument, NULL, 'r'},
.argument = "REQ_ARG",
.desc = "Option with a required argument REQ_ARG",
},
{
/*
* NOTE: For specifying an optional parameter OPT_ARG use
* either "-oOPT_ARG" or "--opt_arg=OPT_ARG" on the commandline.
* Specifying "-o OPT_ARG" or "--opt_arg OPT_ARG" will not work.
*/
.option = { "opt_arg", optional_argument, NULL, 'o'},
.argument = "OPT_ARG",
.desc = "Option with an optional argument OPT_ARG. " \
"We don't recommend using this feature.",
},
UTIL_OPT_SECTION("OPTION WITHOUT SHORT OPTION"),
{
.option = { "noshort", no_argument, NULL, OPT_NOSHORT},
.desc = "Option with only a long name",
.flags = UTIL_OPT_FLAG_NOSHORT,
},
UTIL_OPT_SECTION("OPTION WITHOUT LONG OPTION"),
{
.option = { NULL, no_argument, NULL, 'l'},
.desc = "Option with only a short name",
.flags = UTIL_OPT_FLAG_NOLONG,
},
UTIL_OPT_SECTION("OPTION WITH MANUALLY FORMATTED DESCRIPTION"),
{
.option = { "manual", no_argument, NULL, 'm' },
.desc = "Option descriptions can be formatted\n" \
"using the new line character '\\n' to:\n" \
" - Display descriptions more meaningful\n" \
" - Create lists within the description",
},
UTIL_OPT_SECTION("STANDARD OPTIONS"),
/* Standard option: -h,--help */
UTIL_OPT_HELP,
/* Standard option: -v,--version */
UTIL_OPT_VERSION,
/* End-marker for option vector */
UTIL_OPT_END
};
/*
* Parse the command line options with util_opt functions
*/
int main(int argc, char *argv[])
{
int c;
/* Install option vector */
util_opt_init(opt_vec, NULL);
/* Parse all options specified in argv[] */
while (1) {
/* Get the next option 'c' from argv[] */
c = util_opt_getopt_long(argc, argv);
/* No more options on command line? */
if (c == -1)
break;
/* Find the right action for option 'c' */
switch (c) {
case 'h':
util_opt_print_help();
return EXIT_SUCCESS;
case 'v':
printf("Specified: --version\n");
return EXIT_SUCCESS;
case 's':
printf("Specified: --single\n");
break;
case 'o':
if (optarg != NULL)
printf("Specified: --opt_arg %s\n", optarg);
else
printf("Specified: --opt_arg [without arg]\n");
break;
case 'r':
printf("Specified: --req_arg %s\n", optarg);
break;
case OPT_NOSHORT:
printf("Specified: --noshort\n");
break;
case 'l':
printf("Specified: -l\n");
break;
case 'm':
printf("Specified: --manual\n");
break;
default:
util_opt_print_parse_error(c, argv);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
//! [code]
|