anyoption.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #ifndef _ANYOPTION_H
  2. #define _ANYOPTION_H
  3. #define _CRT_SECURE_NO_WARNINGS /* Microsoft C/C++ Compiler: Disable C4996 \
  4. warnings for security-enhanced CRT \
  5. functions */
  6. #include <cstring>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <stdlib.h>
  10. #include <string>
  11. #define COMMON_OPT 1
  12. #define COMMAND_OPT 2
  13. #define FILE_OPT 3
  14. #define COMMON_FLAG 4
  15. #define COMMAND_FLAG 5
  16. #define FILE_FLAG 6
  17. #define COMMAND_OPTION_TYPE 1
  18. #define COMMAND_FLAG_TYPE 2
  19. #define FILE_OPTION_TYPE 3
  20. #define FILE_FLAG_TYPE 4
  21. #define UNKNOWN_TYPE 5
  22. #define DEFAULT_MAXOPTS 10
  23. #define MAX_LONG_PREFIX_LENGTH 2
  24. #define DEFAULT_MAXUSAGE 3
  25. #define DEFAULT_MAXHELP 10
  26. #define TRUE_FLAG "true"
  27. using namespace std;
  28. class AnyOption {
  29. public: /* the public interface */
  30. AnyOption();
  31. explicit AnyOption(int maxoptions);
  32. explicit AnyOption(int maxoptions, int maxcharoptions);
  33. ~AnyOption();
  34. /*
  35. * following set methods specifies the
  36. * special characters and delimiters
  37. * if not set traditional defaults will be used
  38. */
  39. void setCommandPrefixChar(char _prefix); /* '-' in "-w" */
  40. void setCommandLongPrefix(const char *_prefix); /* '--' in "--width" */
  41. void setFileCommentChar(char _comment); /* '#' in shell scripts */
  42. void setFileDelimiterChar(char _delimiter); /* ':' in "width : 100" */
  43. /*
  44. * provide the input for the options
  45. * like argv[] for commnd line and the
  46. * option file name to use;
  47. */
  48. void useCommandArgs(int _argc, char **_argv);
  49. void useFiileName(const char *_filename);
  50. /*
  51. * turn off the POSIX style options
  52. * this means anything starting with a '-' or "--"
  53. * will be considered a valid option
  54. * which also means you cannot add a bunch of
  55. * POIX options chars together like "-lr" for "-l -r"
  56. *
  57. */
  58. void noPOSIX();
  59. /*
  60. * prints warning verbose if you set anything wrong
  61. */
  62. void setVerbose();
  63. /*
  64. * there are two types of options
  65. *
  66. * Option - has an associated value ( -w 100 )
  67. * Flag - no value, just a boolean flag ( -nogui )
  68. *
  69. * the options can be either a string ( GNU style )
  70. * or a character ( traditional POSIX style )
  71. * or both ( --width, -w )
  72. *
  73. * the options can be common to the command line and
  74. * the option file, or can belong only to either of
  75. * command line and option file
  76. *
  77. * following set methods, handle all the above
  78. * cases of options.
  79. */
  80. /* options command to command line and option file */
  81. void setOption(const char *opt_string);
  82. void setOption(char opt_char);
  83. void setOption(const char *opt_string, char opt_char);
  84. void setFlag(const char *opt_string);
  85. void setFlag(char opt_char);
  86. void setFlag(const char *opt_string, char opt_char);
  87. /* options read from command line only */
  88. void setCommandOption(const char *opt_string);
  89. void setCommandOption(char opt_char);
  90. void setCommandOption(const char *opt_string, char opt_char);
  91. void setCommandFlag(const char *opt_string);
  92. void setCommandFlag(char opt_char);
  93. void setCommandFlag(const char *opt_string, char opt_char);
  94. /* options read from an option file only */
  95. void setFileOption(const char *opt_string);
  96. void setFileOption(char opt_char);
  97. void setFileOption(const char *opt_string, char opt_char);
  98. void setFileFlag(const char *opt_string);
  99. void setFileFlag(char opt_char);
  100. void setFileFlag(const char *opt_string, char opt_char);
  101. /*
  102. * process the options, registered using
  103. * useCommandArgs() and useFileName();
  104. */
  105. void processOptions();
  106. void processCommandArgs();
  107. void processCommandArgs(int max_args);
  108. bool processFile();
  109. /*
  110. * process the specified options
  111. */
  112. void processCommandArgs(int _argc, char **_argv);
  113. void processCommandArgs(int _argc, char **_argv, int max_args);
  114. bool processFile(const char *_filename);
  115. /*
  116. * get the value of the options
  117. * will return NULL if no value is set
  118. */
  119. char *getValue(const char *_option);
  120. bool getFlag(const char *_option);
  121. char *getValue(char _optchar);
  122. bool getFlag(char _optchar);
  123. /*
  124. * Print Usage
  125. */
  126. void printUsage();
  127. void printAutoUsage();
  128. void addUsage(const char *line);
  129. void printHelp();
  130. /* print auto usage printing for unknown options or flag */
  131. void autoUsagePrint(bool flag);
  132. /*
  133. * get the argument count and arguments sans the options
  134. */
  135. int getArgc() const;
  136. char *getArgv(int index) const;
  137. bool hasOptions() const;
  138. private: /* the hidden data structure */
  139. int argc; /* command line arg count */
  140. char **argv; /* commnd line args */
  141. const char *filename; /* the option file */
  142. char *appname; /* the application name from argv[0] */
  143. int *new_argv; /* arguments sans options (index to argv) */
  144. int new_argc; /* argument count sans the options */
  145. int max_legal_args; /* ignore extra arguments */
  146. /* option strings storage + indexing */
  147. int max_options; /* maximum number of options */
  148. const char **options; /* storage */
  149. int *optiontype; /* type - common, command, file */
  150. int *optionindex; /* index into value storage */
  151. int option_counter; /* counter for added options */
  152. /* option chars storage + indexing */
  153. int max_char_options; /* maximum number options */
  154. char *optionchars; /* storage */
  155. int *optchartype; /* type - common, command, file */
  156. int *optcharindex; /* index into value storage */
  157. int optchar_counter; /* counter for added options */
  158. /* values */
  159. char **values; /* common value storage */
  160. int g_value_counter; /* globally updated value index LAME! */
  161. /* help and usage */
  162. const char **usage; /* usage */
  163. int max_usage_lines; /* max usage lines reserved */
  164. int usage_lines; /* number of usage lines */
  165. bool command_set; /* if argc/argv were provided */
  166. bool file_set; /* if a filename was provided */
  167. bool mem_allocated; /* if memory allocated in init() */
  168. bool posix_style; /* enables to turn off POSIX style options */
  169. bool verbose; /* silent|verbose */
  170. bool print_usage; /* usage verbose */
  171. bool print_help; /* help verbose */
  172. char opt_prefix_char; /* '-' in "-w" */
  173. char long_opt_prefix[MAX_LONG_PREFIX_LENGTH + 1]; /* '--' in "--width" */
  174. char file_delimiter_char; /* ':' in width : 100 */
  175. char file_comment_char; /* '#' in "#this is a comment" */
  176. char equalsign;
  177. char comment;
  178. char delimiter;
  179. char endofline;
  180. char whitespace;
  181. char nullterminate;
  182. bool set; // was static member
  183. bool once; // was static member
  184. bool hasoptions;
  185. bool autousage;
  186. private: /* the hidden utils */
  187. void init();
  188. void init(int maxopt, int maxcharopt);
  189. bool alloc();
  190. void allocValues(int index, size_t length);
  191. void cleanup();
  192. bool valueStoreOK();
  193. /* grow storage arrays as required */
  194. bool doubleOptStorage();
  195. bool doubleCharStorage();
  196. bool doubleUsageStorage();
  197. bool setValue(const char *option, char *value);
  198. bool setFlagOn(const char *option);
  199. bool setValue(char optchar, char *value);
  200. bool setFlagOn(char optchar);
  201. void addOption(const char *option, int type);
  202. void addOption(char optchar, int type);
  203. void addOptionError(const char *opt) const;
  204. void addOptionError(char opt) const;
  205. bool findFlag(char *value);
  206. void addUsageError(const char *line);
  207. bool CommandSet() const;
  208. bool FileSet() const;
  209. bool POSIX() const;
  210. char parsePOSIX(char *arg);
  211. int parseGNU(char *arg);
  212. bool matchChar(char c);
  213. int matchOpt(char *opt);
  214. /* dot file methods */
  215. char *readFile();
  216. char *readFile(const char *fname);
  217. bool consumeFile(char *buffer);
  218. void processLine(char *theline, int length);
  219. char *chomp(char *str);
  220. void valuePairs(char *type, char *value);
  221. void justValue(char *value);
  222. void printVerbose(const char *msg) const;
  223. void printVerbose(char *msg) const;
  224. void printVerbose(char ch) const;
  225. void printVerbose() const;
  226. };
  227. #endif /* ! _ANYOPTION_H */