1
0

myprog.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*******************************************************************************
  2. * Example source code for using the argtable3 library to implement:
  3. *
  4. * myprog [-lRv] [-k <int>] [-D MACRO]... [-o <output>] [--help]
  5. * [--version] <file> [<file>]...
  6. *
  7. * This file is part of the argtable3 library.
  8. *
  9. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  10. * <sheitmann@users.sourceforge.net>
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  28. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  31. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. ******************************************************************************/
  35. #include <argtable3.h>
  36. int mymain(int l, int R, int k,
  37. const char **defines, int ndefines,
  38. const char *outfile,
  39. int v,
  40. const char **infiles, int ninfiles)
  41. {
  42. int i;
  43. if (l>0) printf("list files (-l)\n");
  44. if (R>0) printf("recurse through directories (-R)\n");
  45. if (v>0) printf("verbose is enabled (-v)\n");
  46. printf("scalar k=%d\n",k);
  47. printf("output is \"%s\"\n", outfile);
  48. for (i=0; i<ndefines; i++)
  49. printf("user defined macro \"%s\"\n",defines[i]);
  50. for (i=0; i<ninfiles; i++)
  51. printf("infile[%d]=\"%s\"\n",i,infiles[i]);
  52. return 0;
  53. }
  54. int main(int argc, char **argv)
  55. {
  56. struct arg_lit *list = arg_lit0("lL",NULL, "list files");
  57. struct arg_lit *recurse = arg_lit0("R",NULL, "recurse through subdirectories");
  58. struct arg_int *repeat = arg_int0("k","scalar",NULL, "define scalar value k (default is 3)");
  59. struct arg_str *defines = arg_strn("D","define","MACRO",0,argc+2, "macro definitions");
  60. struct arg_file *outfile = arg_file0("o",NULL,"<output>", "output file (default is \"-\")");
  61. struct arg_lit *verbose = arg_lit0("v","verbose,debug", "verbose messages");
  62. struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit");
  63. struct arg_lit *version = arg_lit0(NULL,"version", "print version information and exit");
  64. struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,argc+2, "input file(s)");
  65. struct arg_end *end = arg_end(20);
  66. void* argtable[] = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end};
  67. const char* progname = "myprog";
  68. int nerrors;
  69. int exitcode=0;
  70. /* verify the argtable[] entries were allocated sucessfully */
  71. if (arg_nullcheck(argtable) != 0)
  72. {
  73. /* NULL entries were detected, some allocations must have failed */
  74. printf("%s: insufficient memory\n",progname);
  75. exitcode=1;
  76. goto exit;
  77. }
  78. /* set any command line default values prior to parsing */
  79. repeat->ival[0]=3;
  80. outfile->filename[0]="-";
  81. /* Parse the command line as defined by argtable[] */
  82. nerrors = arg_parse(argc,argv,argtable);
  83. /* special case: '--help' takes precedence over error reporting */
  84. if (help->count > 0)
  85. {
  86. printf("Usage: %s", progname);
  87. arg_print_syntax(stdout,argtable,"\n");
  88. printf("This program demonstrates the use of the argtable2 library\n");
  89. printf("for parsing command line arguments. Argtable accepts integers\n");
  90. printf("in decimal (123), hexadecimal (0xff), octal (0o123) and binary\n");
  91. printf("(0b101101) formats. Suffixes KB, MB and GB are also accepted.\n");
  92. arg_print_glossary(stdout,argtable," %-25s %s\n");
  93. exitcode=0;
  94. goto exit;
  95. }
  96. /* special case: '--version' takes precedence error reporting */
  97. if (version->count > 0)
  98. {
  99. printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname);
  100. printf("September 2003, Stewart Heitmann\n");
  101. exitcode=0;
  102. goto exit;
  103. }
  104. /* If the parser returned any errors then display them and exit */
  105. if (nerrors > 0)
  106. {
  107. /* Display the error details contained in the arg_end struct.*/
  108. arg_print_errors(stdout,end,progname);
  109. printf("Try '%s --help' for more information.\n",progname);
  110. exitcode=1;
  111. goto exit;
  112. }
  113. /* special case: uname with no command line options induces brief help */
  114. if (argc==1)
  115. {
  116. printf("Try '%s --help' for more information.\n",progname);
  117. exitcode=0;
  118. goto exit;
  119. }
  120. /* normal case: take the command line options at face value */
  121. exitcode = mymain(list->count, recurse->count, repeat->ival[0],
  122. defines->sval, defines->count,
  123. outfile->filename[0], verbose->count,
  124. infiles->filename, infiles->count);
  125. exit:
  126. /* deallocate each non-null entry in argtable[] */
  127. arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
  128. return exitcode;
  129. }