1
0

testargtable3.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "argtable3.h"
  2. /* global arg_xxx structs */
  3. struct arg_lit *a, *b, *c, *verb, *help, *version;
  4. struct arg_int *scal;
  5. struct arg_file *o, *file;
  6. struct arg_end *end;
  7. int main(int argc, char *argv[])
  8. {
  9. /* the global arg_xxx structs are initialised within the argtable */
  10. void *argtable[] = {
  11. help = arg_lit0(NULL, "help", "display this help and exit"),
  12. version = arg_lit0(NULL, "version", "display version info and exit"),
  13. a = arg_lit0("a", NULL,"the -a option"),
  14. b = arg_lit0("b", NULL, "the -b option"),
  15. c = arg_lit0("c", NULL, "the -c option"),
  16. scal = arg_int0(NULL, "scalar", "<n>", "foo value"),
  17. verb = arg_lit0("v", "verbose", "verbose output"),
  18. o = arg_file0("o", NULL, "myfile", "output file"),
  19. file = arg_filen(NULL, NULL, "<file>", 1, 100, "input files"),
  20. end = arg_end(20),
  21. };
  22. int exitcode = 0;
  23. char progname[] = "testargtable2.exe";
  24. int nerrors;
  25. nerrors = arg_parse(argc,argv,argtable);
  26. /* special case: '--help' takes precedence over error reporting */
  27. if (help->count > 0)
  28. {
  29. printf("Usage: %s", progname);
  30. arg_print_syntax(stdout, argtable, "\n");
  31. printf("List information about the FILE(s) "
  32. "(the current directory by default).\n\n");
  33. arg_print_glossary(stdout, argtable, " %-25s %s\n");
  34. exitcode = 0;
  35. goto exit;
  36. }
  37. /* If the parser returned any errors then display them and exit */
  38. if (nerrors > 0)
  39. {
  40. /* Display the error details contained in the arg_end struct.*/
  41. arg_print_errors(stdout, end, progname);
  42. printf("Try '%s --help' for more information.\n", progname);
  43. exitcode = 1;
  44. goto exit;
  45. }
  46. exit:
  47. /* deallocate each non-null entry in argtable[] */
  48. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  49. return exitcode;
  50. }