Advertisement
MdSadmanSiraj

argp example 2

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