cmdline.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/cmdline.h
  3. // Purpose: wxCmdLineParser and related classes for parsing the command
  4. // line options
  5. // Author: Vadim Zeitlin
  6. // Modified by:
  7. // Created: 04.01.00
  8. // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  9. // Licence: wxWindows licence
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_CMDLINE_H_
  12. #define _WX_CMDLINE_H_
  13. #include "wx/defs.h"
  14. #include "wx/string.h"
  15. #include "wx/arrstr.h"
  16. #include "wx/cmdargs.h"
  17. // determines ConvertStringToArgs() behaviour
  18. enum wxCmdLineSplitType
  19. {
  20. wxCMD_LINE_SPLIT_DOS,
  21. wxCMD_LINE_SPLIT_UNIX
  22. };
  23. #if wxUSE_CMDLINE_PARSER
  24. class WXDLLIMPEXP_FWD_BASE wxDateTime;
  25. // ----------------------------------------------------------------------------
  26. // constants
  27. // ----------------------------------------------------------------------------
  28. // by default, options are optional (sic) and each call to AddParam() allows
  29. // one more parameter - this may be changed by giving non-default flags to it
  30. enum wxCmdLineEntryFlags
  31. {
  32. wxCMD_LINE_OPTION_MANDATORY = 0x01, // this option must be given
  33. wxCMD_LINE_PARAM_OPTIONAL = 0x02, // the parameter may be omitted
  34. wxCMD_LINE_PARAM_MULTIPLE = 0x04, // the parameter may be repeated
  35. wxCMD_LINE_OPTION_HELP = 0x08, // this option is a help request
  36. wxCMD_LINE_NEEDS_SEPARATOR = 0x10, // must have sep before the value
  37. wxCMD_LINE_SWITCH_NEGATABLE = 0x20 // this switch can be negated (e.g. /S-)
  38. };
  39. // an option value or parameter may be a string (the most common case), a
  40. // number or a date
  41. enum wxCmdLineParamType
  42. {
  43. wxCMD_LINE_VAL_STRING, // should be 0 (default)
  44. wxCMD_LINE_VAL_NUMBER,
  45. wxCMD_LINE_VAL_DATE,
  46. wxCMD_LINE_VAL_DOUBLE,
  47. wxCMD_LINE_VAL_NONE
  48. };
  49. // for constructing the cmd line description using Init()
  50. enum wxCmdLineEntryType
  51. {
  52. wxCMD_LINE_SWITCH,
  53. wxCMD_LINE_OPTION,
  54. wxCMD_LINE_PARAM,
  55. wxCMD_LINE_USAGE_TEXT,
  56. wxCMD_LINE_NONE // to terminate the list
  57. };
  58. // Possible return values of wxCmdLineParser::FoundSwitch()
  59. enum wxCmdLineSwitchState
  60. {
  61. wxCMD_SWITCH_OFF = -1, // Found but turned off/negated.
  62. wxCMD_SWITCH_NOT_FOUND, // Not found at all.
  63. wxCMD_SWITCH_ON // Found in normal state.
  64. };
  65. // ----------------------------------------------------------------------------
  66. // wxCmdLineEntryDesc is a description of one command line
  67. // switch/option/parameter
  68. // ----------------------------------------------------------------------------
  69. struct wxCmdLineEntryDesc
  70. {
  71. wxCmdLineEntryType kind;
  72. const char *shortName;
  73. const char *longName;
  74. const char *description;
  75. wxCmdLineParamType type;
  76. int flags;
  77. };
  78. // the list of wxCmdLineEntryDesc objects should be terminated with this one
  79. #define wxCMD_LINE_DESC_END \
  80. { wxCMD_LINE_NONE, NULL, NULL, NULL, wxCMD_LINE_VAL_NONE, 0x0 }
  81. // ----------------------------------------------------------------------------
  82. // wxCmdLineParser is a class for parsing command line.
  83. //
  84. // It has the following features:
  85. //
  86. // 1. distinguishes options, switches and parameters; allows option grouping
  87. // 2. allows both short and long options
  88. // 3. automatically generates the usage message from the cmd line description
  89. // 4. does type checks on the options values (number, date, ...)
  90. //
  91. // To use it you should:
  92. //
  93. // 1. construct it giving it the cmd line to parse and optionally its desc
  94. // 2. construct the cmd line description using AddXXX() if not done in (1)
  95. // 3. call Parse()
  96. // 4. use GetXXX() to retrieve the parsed info
  97. // ----------------------------------------------------------------------------
  98. class WXDLLIMPEXP_BASE wxCmdLineParser
  99. {
  100. public:
  101. // ctors and initializers
  102. // ----------------------
  103. // default ctor or ctor giving the cmd line in either Unix or Win form
  104. wxCmdLineParser() { Init(); }
  105. wxCmdLineParser(int argc, char **argv) { Init(); SetCmdLine(argc, argv); }
  106. #if wxUSE_UNICODE
  107. wxCmdLineParser(int argc, wxChar **argv) { Init(); SetCmdLine(argc, argv); }
  108. wxCmdLineParser(int argc, const wxCmdLineArgsArray& argv)
  109. { Init(); SetCmdLine(argc, argv); }
  110. #endif // wxUSE_UNICODE
  111. wxCmdLineParser(const wxString& cmdline) { Init(); SetCmdLine(cmdline); }
  112. // the same as above, but also gives the cmd line description - otherwise,
  113. // use AddXXX() later
  114. wxCmdLineParser(const wxCmdLineEntryDesc *desc)
  115. { Init(); SetDesc(desc); }
  116. wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, char **argv)
  117. { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
  118. #if wxUSE_UNICODE
  119. wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, wxChar **argv)
  120. { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
  121. wxCmdLineParser(const wxCmdLineEntryDesc *desc,
  122. int argc,
  123. const wxCmdLineArgsArray& argv)
  124. { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
  125. #endif // wxUSE_UNICODE
  126. wxCmdLineParser(const wxCmdLineEntryDesc *desc, const wxString& cmdline)
  127. { Init(); SetCmdLine(cmdline); SetDesc(desc); }
  128. // set cmd line to parse after using one of the ctors which don't do it
  129. void SetCmdLine(int argc, char **argv);
  130. #if wxUSE_UNICODE
  131. void SetCmdLine(int argc, wxChar **argv);
  132. void SetCmdLine(int argc, const wxCmdLineArgsArray& argv);
  133. #endif // wxUSE_UNICODE
  134. void SetCmdLine(const wxString& cmdline);
  135. // not virtual, don't use this class polymorphically
  136. ~wxCmdLineParser();
  137. // set different parser options
  138. // ----------------------------
  139. // by default, '-' is switch char under Unix, '-' or '/' under Win:
  140. // switchChars contains all characters with which an option or switch may
  141. // start
  142. void SetSwitchChars(const wxString& switchChars);
  143. // long options are not POSIX-compliant, this option allows to disable them
  144. void EnableLongOptions(bool enable = true);
  145. void DisableLongOptions() { EnableLongOptions(false); }
  146. bool AreLongOptionsEnabled() const;
  147. // extra text may be shown by Usage() method if set by this function
  148. void SetLogo(const wxString& logo);
  149. // construct the cmd line description
  150. // ----------------------------------
  151. // take the cmd line description from the wxCMD_LINE_NONE terminated table
  152. void SetDesc(const wxCmdLineEntryDesc *desc);
  153. // a switch: i.e. an option without value
  154. void AddSwitch(const wxString& name, const wxString& lng = wxEmptyString,
  155. const wxString& desc = wxEmptyString,
  156. int flags = 0);
  157. void AddLongSwitch(const wxString& lng,
  158. const wxString& desc = wxEmptyString,
  159. int flags = 0)
  160. {
  161. AddSwitch(wxString(), lng, desc, flags);
  162. }
  163. // an option taking a value of the given type
  164. void AddOption(const wxString& name, const wxString& lng = wxEmptyString,
  165. const wxString& desc = wxEmptyString,
  166. wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
  167. int flags = 0);
  168. void AddLongOption(const wxString& lng,
  169. const wxString& desc = wxEmptyString,
  170. wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
  171. int flags = 0)
  172. {
  173. AddOption(wxString(), lng, desc, type, flags);
  174. }
  175. // a parameter
  176. void AddParam(const wxString& desc = wxEmptyString,
  177. wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
  178. int flags = 0);
  179. // add an explanatory text to be shown to the user in help
  180. void AddUsageText(const wxString& text);
  181. // actions
  182. // -------
  183. // parse the command line, return 0 if ok, -1 if "-h" or "--help" option
  184. // was encountered and the help message was given or a positive value if a
  185. // syntax error occurred
  186. //
  187. // if showUsage is true, Usage() is called in case of syntax error or if
  188. // help was requested
  189. int Parse(bool showUsage = true);
  190. // give the usage message describing all program options
  191. void Usage() const;
  192. // return the usage string, call Usage() to directly show it to the user
  193. wxString GetUsageString() const;
  194. // get the command line arguments
  195. // ------------------------------
  196. // returns true if the given switch was found
  197. bool Found(const wxString& name) const;
  198. // Returns wxCMD_SWITCH_NOT_FOUND if the switch was not found at all,
  199. // wxCMD_SWITCH_ON if it was found in normal state and wxCMD_SWITCH_OFF if
  200. // it was found but negated (i.e. followed by "-", this can only happen for
  201. // the switches with wxCMD_LINE_SWITCH_NEGATABLE flag).
  202. wxCmdLineSwitchState FoundSwitch(const wxString& name) const;
  203. // returns true if an option taking a string value was found and stores the
  204. // value in the provided pointer
  205. bool Found(const wxString& name, wxString *value) const;
  206. // returns true if an option taking an integer value was found and stores
  207. // the value in the provided pointer
  208. bool Found(const wxString& name, long *value) const;
  209. // returns true if an option taking a double value was found and stores
  210. // the value in the provided pointer
  211. bool Found(const wxString& name, double *value) const;
  212. #if wxUSE_DATETIME
  213. // returns true if an option taking a date value was found and stores the
  214. // value in the provided pointer
  215. bool Found(const wxString& name, wxDateTime *value) const;
  216. #endif // wxUSE_DATETIME
  217. // gets the number of parameters found
  218. size_t GetParamCount() const;
  219. // gets the value of Nth parameter (as string only for now)
  220. wxString GetParam(size_t n = 0u) const;
  221. // Resets switches and options
  222. void Reset();
  223. // break down the command line in arguments
  224. static wxArrayString
  225. ConvertStringToArgs(const wxString& cmdline,
  226. wxCmdLineSplitType type = wxCMD_LINE_SPLIT_DOS);
  227. private:
  228. // common part of all ctors
  229. void Init();
  230. struct wxCmdLineParserData *m_data;
  231. wxDECLARE_NO_COPY_CLASS(wxCmdLineParser);
  232. };
  233. #else // !wxUSE_CMDLINE_PARSER
  234. // this function is always available (even if !wxUSE_CMDLINE_PARSER) because it
  235. // is used by wxWin itself under Windows
  236. class WXDLLIMPEXP_BASE wxCmdLineParser
  237. {
  238. public:
  239. static wxArrayString
  240. ConvertStringToArgs(const wxString& cmdline,
  241. wxCmdLineSplitType type = wxCMD_LINE_SPLIT_DOS);
  242. };
  243. #endif // wxUSE_CMDLINE_PARSER/!wxUSE_CMDLINE_PARSER
  244. #endif // _WX_CMDLINE_H_