Guest User

Problem with optional argument to an option in libargtable2

a guest
Jul 26th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. /* Written by con-f-use - gmx d o t de */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <argtable2.h>
  6.  
  7. int main(int argc, char** argv) {
  8.     int i;
  9.     struct arg_file *argOutput, *argFiles;
  10.  
  11.     argOutput = arg_file0("o", NULL, NULL,
  12.         "File to write output to. Default is 'output.txt'. Omit whole option to discard all output"
  13.     );
  14.     argOutput->hdr.flag |= ARG_HASOPTVALUE;
  15.     argOutput->filename[0] = "output.txt";
  16.     argFiles = arg_filen(NULL, NULL, "INPUT-FILES", 1, argc-1, "Input files");
  17.     struct arg_end *end = arg_end(5);
  18.  
  19.     void *argtable[] = { argOutput, argFiles, end };
  20.  
  21.     if( arg_nullcheck(argtable) != 0 ) exit(EXIT_FAILURE);
  22.  
  23.     if( arg_parse(argc,argv,argtable) != 0 ) {
  24.         arg_print_errors(stderr, end, "Error");
  25.         printf("\nUsage:\n%s", argv[0]);
  26.         arg_print_syntax(stdout, argtable, "\n");
  27.         arg_print_glossary_gnu(stdout, argtable);
  28.         exit(EXIT_FAILURE);
  29.     }
  30.  
  31.     for(i=0; i<argFiles->count; ++i)
  32.         printf("inputfile[%i] = %s\n", i, argFiles->filename[i]);
  33.  
  34.     if( argOutput->count > 0 )
  35.         printf("outputfile = %s\n", argOutput->filename[0] );
  36.     else
  37.         printf("Default value used for output-file.\n");
  38.  
  39.     return EXIT_SUCCESS;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment