Advertisement
MdSadmanSiraj

argp example 1

Jul 15th, 2022
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <argp.h>
  3.  
  4. const char *argp_program_version =
  5.   "argp-ex3 1.0";
  6. const char *argp_program_bug_address =
  7.   "<bug-gnu-utils@gnu.org>";
  8.  
  9. /* Program documentation. */
  10. static char doc[] =
  11.   "Argp example #3 -- a program with options and arguments using argp";
  12.  
  13. /* A description of the arguments we accept. */
  14. static char args_doc[] = "ARG1 ARG2";
  15.  
  16. /* The options we understand. */
  17. static struct argp_option options[] = {
  18.   {"verbose",  'v', 0,      0,  "Produce verbose output" },
  19.   {"quiet",    'q', 0,      0,  "Don't produce any output" },
  20.   {"silent",   's', 0,      OPTION_ALIAS },
  21.   {"output",   'o', "FILE", 0,
  22.    "Output to FILE instead of standard output" },
  23.   { 0 }
  24. };
  25.  
  26. /* Used by main to communicate with parse_opt. */
  27. struct arguments
  28. {
  29.   char *args[2];                /* arg1 & arg2 */
  30.   int silent, verbose;
  31.   char *output_file;
  32. };
  33.  
  34. /* Parse a single option. */
  35. static error_t
  36. parse_opt (int key, char *arg, struct argp_state *state)
  37. {
  38.   /* Get the input argument from argp_parse, which we
  39.      know is a pointer to our arguments structure. */
  40.   struct arguments *arguments = state->input;
  41.  
  42.   switch (key)
  43.     {
  44.     case 'q': case 's':
  45.       arguments->silent = 1;
  46.       break;
  47.     case 'v':
  48.       arguments->verbose = 1;
  49.       break;
  50.     case 'o':
  51.       arguments->output_file = arg;
  52.       break;
  53.  
  54.     case ARGP_KEY_ARG:
  55.       if (state->arg_num >= 2)
  56.         /* Too many arguments. */
  57.         argp_usage (state);
  58.  
  59.       arguments->args[state->arg_num] = arg;
  60.  
  61.       break;
  62.  
  63.     case ARGP_KEY_END:
  64.       if (state->arg_num < 2)
  65.         /* Not enough arguments. */
  66.         argp_usage (state);
  67.       break;
  68.  
  69.     default:
  70.       return ARGP_ERR_UNKNOWN;
  71.     }
  72.   return 0;
  73. }
  74.  
  75. /* Our argp parser. */
  76. static struct argp argp = { options, parse_opt, args_doc, doc };
  77.  
  78. int
  79. main (int argc, char **argv)
  80. {
  81.   struct arguments arguments;
  82.  
  83.   /* Default values. */
  84.   arguments.silent = 0;
  85.   arguments.verbose = 0;
  86.   arguments.output_file = "-";
  87.  
  88.   /* Parse our arguments; every option seen by parse_opt will
  89.      be reflected in arguments. */
  90.   argp_parse (&argp, argc, argv, 0, 0, &arguments);
  91.  
  92.   printf ("ARG1 = %s\nARG2 = %s\nOUTPUT_FILE = %s\n"
  93.           "VERBOSE = %s\nSILENT = %s\n",
  94.           arguments.args[0], arguments.args[1],
  95.           arguments.output_file,
  96.           arguments.verbose ? "yes" : "no",
  97.           arguments.silent ? "yes" : "no");
  98.  
  99.   exit (0);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement