uoptions.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 2000-2011, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: uoptions.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2000apr17
  16. * created by: Markus W. Scherer
  17. *
  18. * This file provides a command line argument parser.
  19. */
  20. #ifndef __UOPTIONS_H__
  21. #define __UOPTIONS_H__
  22. #include "unicode/utypes.h"
  23. /* This should usually be called before calling u_parseArgs */
  24. /*#if U_PLATFORM == U_PF_OS390 && (U_CHARSET_FAMILY == U_ASCII_FAMILY)*/
  25. /* translate args from EBCDIC to ASCII */
  26. /*# define U_MAIN_INIT_ARGS(argc, argv) __argvtoascii_a(argc, argv)*/
  27. /*#elif defined(XP_MAC_CONSOLE)*/
  28. #if defined(XP_MAC_CONSOLE)
  29. # include <console.h>
  30. /* Get the arguments from the GUI, since old Macs don't have a console Window. */
  31. # define U_MAIN_INIT_ARGS(argc, argv) argc = ccommand((char***)&argv)
  32. #else
  33. /* Normally we do nothing. */
  34. # define U_MAIN_INIT_ARGS(argc, argv)
  35. #endif
  36. /* forward declarations for the function declaration */
  37. struct UOption;
  38. typedef struct UOption UOption;
  39. /* function to be called for a command line option */
  40. typedef int UOptionFn(void *context, UOption *option);
  41. /* values of UOption.hasArg */
  42. enum { UOPT_NO_ARG, UOPT_REQUIRES_ARG, UOPT_OPTIONAL_ARG };
  43. /* structure describing a command line option */
  44. struct UOption {
  45. const char *longName; /* "foo" for --foo */
  46. const char *value; /* output placeholder, will point to the argument string, if any */
  47. UOptionFn *optionFn; /* function to be called when this option occurs */
  48. void *context; /* parameter for the function */
  49. char shortName; /* 'f' for -f */
  50. char hasArg; /* enum value: option takes no/requires/may have argument */
  51. char doesOccur; /* boolean for "this one occured" */
  52. };
  53. /* macro for an entry in a declaration of UOption[] */
  54. #define UOPTION_DEF(longName, shortName, hasArg) \
  55. { longName, NULL, NULL, NULL, shortName, hasArg, 0 }
  56. /* ICU Tools option definitions */
  57. #define UOPTION_HELP_H UOPTION_DEF("help", 'h', UOPT_NO_ARG)
  58. #define UOPTION_HELP_QUESTION_MARK UOPTION_DEF("help", '?', UOPT_NO_ARG)
  59. #define UOPTION_VERBOSE UOPTION_DEF("verbose", 'v', UOPT_NO_ARG)
  60. #define UOPTION_QUIET UOPTION_DEF("quiet", 'q', UOPT_NO_ARG)
  61. #define UOPTION_VERSION UOPTION_DEF("version", 'V', UOPT_NO_ARG)
  62. #define UOPTION_COPYRIGHT UOPTION_DEF("copyright", 'c', UOPT_NO_ARG)
  63. #define UOPTION_DESTDIR UOPTION_DEF("destdir", 'd', UOPT_REQUIRES_ARG)
  64. #define UOPTION_SOURCEDIR UOPTION_DEF("sourcedir", 's', UOPT_REQUIRES_ARG)
  65. #define UOPTION_ENCODING UOPTION_DEF("encoding", 'e', UOPT_REQUIRES_ARG)
  66. #define UOPTION_ICUDATADIR UOPTION_DEF("icudatadir", 'i', UOPT_REQUIRES_ARG)
  67. #define UOPTION_WRITE_JAVA UOPTION_DEF("write-java", 'j', UOPT_OPTIONAL_ARG)
  68. #define UOPTION_PACKAGE_NAME UOPTION_DEF("package-name", 'p', UOPT_REQUIRES_ARG)
  69. #define UOPTION_BUNDLE_NAME UOPTION_DEF("bundle-name", 'b', UOPT_REQUIRES_ARG)
  70. /**
  71. * C Command line argument parser.
  72. *
  73. * This function takes the argv[argc] command line and a description of
  74. * the program's options in form of an array of UOption structures.
  75. * Each UOption defines a long and a short name (a string and a character)
  76. * for options like "--foo" and "-f".
  77. *
  78. * Each option is marked with whether it does not take an argument,
  79. * requires one, or optionally takes one. The argument may follow in
  80. * the same argv[] entry for short options, or it may always follow
  81. * in the next argv[] entry.
  82. *
  83. * An argument is in the next argv[] entry for both long and short name
  84. * options, except it is taken from directly behind the short name in
  85. * its own argv[] entry if there are characters following the option letter.
  86. * An argument in its own argv[] entry must not begin with a '-'
  87. * unless it is only the '-' itself. There is no restriction of the
  88. * argument format if it is part of the short name options's argv[] entry.
  89. *
  90. * The argument is stored in the value field of the corresponding
  91. * UOption entry, and the doesOccur field is set to 1 if the option
  92. * is found at all.
  93. *
  94. * Short name options without arguments can be collapsed into a single
  95. * argv[] entry. After an option letter takes an argument, following
  96. * letters will be taken as its argument.
  97. *
  98. * If the same option is found several times, then the last
  99. * argument value will be stored in the value field.
  100. *
  101. * For each option, a function can be called. This could be used
  102. * for options that occur multiple times and all arguments are to
  103. * be collected.
  104. *
  105. * All options are removed from the argv[] array itself. If the parser
  106. * is successful, then it returns the number of remaining non-option
  107. * strings (including argv[0]).
  108. * argv[0], the program name, is never read or modified.
  109. *
  110. * An option "--" ends option processing; everything after this
  111. * remains in the argv[] array.
  112. *
  113. * An option string "-" alone is treated as a non-option.
  114. *
  115. * If an option is not recognized or an argument missing, then
  116. * the parser returns with the negative index of the argv[] entry
  117. * where the error was detected.
  118. *
  119. * The OS/400 compiler requires that argv either be "char* argv[]",
  120. * or "const char* const argv[]", and it will not accept,
  121. * "const char* argv[]" as a definition for main().
  122. *
  123. * @param argv This parameter is modified
  124. * @param options This parameter is modified
  125. */
  126. U_CAPI int U_EXPORT2
  127. u_parseArgs(int argc, char* argv[],
  128. int optionCount, UOption options[]);
  129. #endif