Advertisement
MdSadmanSiraj

argp example 2

Jul 15th, 2022
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <error.h>
  3. #include <argp.h>
  4.  
  5. const char *argp_program_version =
  6.   "argp-ex4 1.0";
  7. const char *argp_program_bug_address =
  8.   "<bug-gnu-utils@prep.ai.mit.edu>";
  9.  
  10. /* Program documentation. */
  11. static char doc[] =
  12.   "Argp example #4 -- a program with somewhat more complicated\
  13. options\
  14. \vThis part of the documentation comes *after* the options;\
  15. note that the text is automatically filled, but it's possible\
  16. to force a line-break, e.g.\n<-- here.";
  17.  
  18. /* A description of the arguments we accept. */
  19. static char args_doc[] = "ARG1 [STRING...]";
  20.  
  21. /* Keys for options without short-options. */
  22. #define OPT_ABORT  1            /* –abort */
  23.  
  24. /* The options we understand. */
  25. static struct argp_option options[] = {
  26.   {"verbose",  'v', 0,       0, "Produce verbose output" },
  27.   {"quiet",    'q', 0,       0, "Don't produce any output" },
  28.   {"silent",   's', 0,       OPTION_ALIAS },
  29.   {"output",   'o', "FILE",  0,
  30.    "Output to FILE instead of standard output" },
  31.  
  32.   {0,0,0,0, "The following options should be grouped together:" },
  33.   {"repeat",   'r', "COUNT", OPTION_ARG_OPTIONAL,
  34.    "Repeat the output COUNT (default 10) times"},
  35.   {"abort",    OPT_ABORT, 0, 0, "Abort before showing any output"},
  36.  
  37.   { 0 }
  38. };
  39.  
  40. /* Used by main to communicate with parse_opt. */
  41. struct arguments
  42. {
  43.   char *arg1;                   /* arg1 */
  44.   char **strings;               /* [string…] */
  45.   int silent, verbose, abort;   /* ‘-s’, ‘-v’, ‘--abort’ */
  46.   char *output_file;            /* file arg to ‘--output’ */
  47.   int repeat_count;             /* count arg to ‘--repeat’ */
  48. };
  49.  
  50. /* Parse a single option. */
  51. static error_t
  52. parse_opt (int key, char *arg, struct argp_state *state)
  53. {
  54.   /* Get the input argument from argp_parse, which we
  55.      know is a pointer to our arguments structure. */
  56.   struct arguments *arguments = state->input;
  57.  
  58.   switch (key)
  59.     {
  60.     case 'q': case 's':
  61.       arguments->silent = 1;
  62.       break;
  63.     case 'v':
  64.       arguments->verbose = 1;
  65.       break;
  66.     case 'o':
  67.       arguments->output_file = arg;
  68.       break;
  69.     case 'r':
  70.       arguments->repeat_count = arg ? atoi (arg) : 10;
  71.       break;
  72.     case OPT_ABORT:
  73.       arguments->abort = 1;
  74.       break;
  75.  
  76.     case ARGP_KEY_NO_ARGS:
  77.       argp_usage (state);
  78.  
  79.     case ARGP_KEY_ARG:
  80.       /* Here we know that state->arg_num == 0, since we
  81.          force argument parsing to end before any more arguments can
  82.          get here. */
  83.       arguments->arg1 = arg;
  84.  
  85.       /* Now we consume all the rest of the arguments.
  86.          state->next is the index in state->argv of the
  87.          next argument to be parsed, which is the first string
  88.          we’re interested in, so we can just use
  89.          &state->argv[state->next] as the value for
  90.          arguments->strings.
  91.  
  92.          In addition, by setting state->next to the end
  93.          of the arguments, we can force argp to stop parsing here and
  94.          return. */
  95.       arguments->strings = &state->argv[state->next];
  96.       state->next = state->argc;
  97.  
  98.       break;
  99.  
  100.     default:
  101.       return ARGP_ERR_UNKNOWN;
  102.     }
  103.   return 0;
  104. }
  105.  
  106. /* Our argp parser. */
  107. static struct argp argp = { options, parse_opt, args_doc, doc };
  108.  
  109. int
  110. main (int argc, char **argv)
  111. {
  112.   int i, j;
  113.   struct arguments arguments;
  114.  
  115.   /* Default values. */
  116.   arguments.silent = 0;
  117.   arguments.verbose = 0;
  118.   arguments.output_file = "-";
  119.   arguments.repeat_count = 1;
  120.   arguments.abort = 0;
  121.  
  122.   /* Parse our arguments; every option seen by parse_opt will be
  123.      reflected in arguments. */
  124.   argp_parse (&argp, argc, argv, 0, 0, &arguments);
  125.  
  126.   if (arguments.abort)
  127.     error (10, 0, "ABORTED");
  128.  
  129.   for (i = 0; i < arguments.repeat_count; i++)
  130.     {
  131.       printf ("ARG1 = %s\n", arguments.arg1);
  132.       printf ("STRINGS = ");
  133.       for (j = 0; arguments.strings[j]; j++)
  134.         printf (j == 0 ? "%s" : ", %s", arguments.strings[j]);
  135.       printf ("\n");
  136.       printf ("OUTPUT_FILE = %s\nVERBOSE = %s\nSILENT = %s\n",
  137.               arguments.output_file,
  138.               arguments.verbose ? "yes" : "no",
  139.               arguments.silent ? "yes" : "no");
  140.     }
  141.  
  142.   exit (0);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement