cmdline.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: cmdline.h
  3. // Purpose: interface of wxCmdLineParser
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. wxCmdLineEntryDesc::flags field is a combination of these bit masks.
  9. Notice that by default (i.e. if flags are just 0), options are optional
  10. (sic) and each call to wxCmdLineParser::AddParam() allows one more
  11. parameter - this may be changed by giving non-default flags to it, i.e. use
  12. @c wxCMD_LINE_OPTION_MANDATORY to require that the option is given and
  13. @c wxCMD_LINE_PARAM_OPTIONAL to make a parameter optional.
  14. Also, @c wxCMD_LINE_PARAM_MULTIPLE may be specified if the programs accepts a
  15. variable number of parameters - but it only can be given for the last
  16. parameter in the command line description. If you use this flag, you will
  17. probably need to use wxCmdLineEntryDesc::GetParamCount() to retrieve the
  18. number of parameters effectively specified after calling
  19. wxCmdLineEntryDesc::Parse().
  20. @c wxCMD_LINE_NEEDS_SEPARATOR can be specified to require a separator (either
  21. a colon, an equal sign or white space) between the option name and its
  22. value. By default, no separator is required.
  23. @c wxCMD_LINE_SWITCH_NEGATABLE can be specified if you want to allow the
  24. user to specify the switch in both normal form and in negated one (e.g.
  25. /R-). You will need to use wxCmdLineParser::FoundSwitch() to distinguish
  26. between the normal and negated forms of the switch. This flag is new since
  27. wxWidgets 2.9.2.
  28. */
  29. enum wxCmdLineEntryFlags
  30. {
  31. wxCMD_LINE_OPTION_MANDATORY = 0x01, ///< This option must be given.
  32. wxCMD_LINE_PARAM_OPTIONAL = 0x02, ///< The parameter may be omitted.
  33. wxCMD_LINE_PARAM_MULTIPLE = 0x04, ///< The parameter may be repeated.
  34. wxCMD_LINE_OPTION_HELP = 0x08, ///< This option is a help request.
  35. wxCMD_LINE_NEEDS_SEPARATOR = 0x10, ///< Must have a separator before the value.
  36. wxCMD_LINE_SWITCH_NEGATABLE = 0x20 ///< This switch can be negated (e.g. /S-)
  37. };
  38. /**
  39. The possible values of wxCmdLineEntryDesc::type which specify the type of
  40. the value accepted by an option.
  41. */
  42. enum wxCmdLineParamType
  43. {
  44. wxCMD_LINE_VAL_STRING,
  45. wxCMD_LINE_VAL_NUMBER,
  46. wxCMD_LINE_VAL_DATE,
  47. wxCMD_LINE_VAL_DOUBLE,
  48. wxCMD_LINE_VAL_NONE
  49. };
  50. /**
  51. The type of a command line entity used for wxCmdLineEntryDesc::kind.
  52. */
  53. enum wxCmdLineEntryType
  54. {
  55. /// A boolean argument of the program; e.g. @c -v to enable verbose mode.
  56. wxCMD_LINE_SWITCH,
  57. /// An argument with an associated value; e.g. @c "-o filename" to specify
  58. /// an optional output filename.
  59. wxCMD_LINE_OPTION,
  60. /// A parameter: a required program argument.
  61. wxCMD_LINE_PARAM,
  62. /// Additional usage text. See wxCmdLineParser::AddUsageText.
  63. wxCMD_LINE_USAGE_TEXT,
  64. wxCMD_LINE_NONE ///< Use this to terminate the list.
  65. };
  66. /**
  67. The state of a switch as returned by wxCmdLineParser::FoundSwitch().
  68. @since 2.9.2
  69. */
  70. enum wxCmdLineSwitchState
  71. {
  72. /// The switch was found in negated form, i.e. followed by a '-'.
  73. wxCMD_SWITCH_OFF,
  74. /// The switch was not found at all on the command line.
  75. wxCMD_SWITCH_NOT_FOUND
  76. /// The switch was found (and was not negated)
  77. wxCMD_SWITCH_ON
  78. };
  79. /**
  80. Flags determining wxCmdLineParser::ConvertStringToArgs() behaviour.
  81. */
  82. enum wxCmdLineSplitType
  83. {
  84. wxCMD_LINE_SPLIT_DOS,
  85. wxCMD_LINE_SPLIT_UNIX
  86. };
  87. /**
  88. The structure wxCmdLineEntryDesc is used to describe a command line
  89. switch, option or parameter. An array of such structures should be passed
  90. to wxCmdLineParser::SetDesc().
  91. Note that the meanings of parameters of the wxCmdLineParser::AddXXX() functions
  92. are the same as of the corresponding fields in this structure.
  93. */
  94. struct wxCmdLineEntryDesc
  95. {
  96. /**
  97. The kind of this program argument.
  98. See ::wxCmdLineEntryType for more info.
  99. */
  100. wxCmdLineEntryType kind;
  101. /**
  102. The usual, short, name of the switch or the option.
  103. It may contain only letters, digits and the underscores.
  104. This field is unused if <tt>kind == wxCMD_LINE_PARAM</tt>.
  105. */
  106. const char *shortName;
  107. /**
  108. The long name for this program argument (may be empty if the option
  109. has no long name).
  110. It may contain only letters, digits and the underscores.
  111. This field is unused if <tt>kind == wxCMD_LINE_PARAM</tt>.
  112. */
  113. const char *longName;
  114. /**
  115. This description is used by the wxCmdLineParser::Usage() method to
  116. construct a help message explaining the syntax of the program.
  117. */
  118. const char *description;
  119. /**
  120. The type associated with this option (ignored if <tt>kind != wxCMD_LINE_OPTION</tt>).
  121. See ::wxCmdLineParamType for more info.
  122. */
  123. wxCmdLineParamType type;
  124. /**
  125. A combination of one or more ::wxCmdLineEntryFlags enum values.
  126. */
  127. int flags;
  128. };
  129. /**
  130. @class wxCmdLineParser
  131. wxCmdLineParser is a class for parsing the command line.
  132. It has the following features:
  133. - distinguishes options, switches and parameters
  134. - allows option grouping
  135. - allows both short and long options
  136. - automatically generates the usage message from the command line description
  137. - checks types of the options values (number, date, ...).
  138. To use it you should follow these steps:
  139. -# @ref cmdlineparser_construction "Construct" an object of this class
  140. giving it the command line to parse and optionally its description or
  141. use the @c AddXXX() functions later.
  142. -# Call Parse().
  143. -# Use Found() to retrieve the results.
  144. You can also use wxApp's default command line processing just overriding
  145. wxAppConsole::OnInitCmdLine() and wxAppConsole::OnCmdLineParsed().
  146. In the documentation below the following terminology is used:
  147. - @b switch: a boolean option which can be given or not, but which doesn't have
  148. any value. We use the word @e switch to distinguish
  149. such boolean options from more generic options like those
  150. described below. For example, @c "-v" might be a switch
  151. meaning "enable verbose mode".
  152. - @b option: a switch with a value associated to it.
  153. For example, @c "-o filename" might be an
  154. option for specifying the name of the output file.
  155. - @b parameter: a required program argument.
  156. @section cmdlineparser_construction Construction
  157. Before Parse() can be called, the command line parser object must have the
  158. command line to parse and also the rules saying which switches, options and
  159. parameters are valid - this is called command line description in what
  160. follows.
  161. You have complete freedom of choice as to when specify the required
  162. information, the only restriction is that it must be done before calling
  163. Parse().
  164. To specify the command line to parse you may use either one of constructors
  165. accepting it (wxCmdLineParser(int, char**) or
  166. wxCmdLineParser(const wxString&) usually) or, if you use the default
  167. constructor, you can do it later by calling SetCmdLine().
  168. The same holds for command line description: it can be specified either in
  169. the constructor (with or without the command line itself) or constructed
  170. later using either SetDesc() or combination of AddSwitch(), AddOption(),
  171. AddParam() and AddUsageText() methods.
  172. Using constructors or SetDesc() uses a (usually const static) table
  173. containing the command line description. If you want to decide which
  174. options to accept during the run-time, using one of the AddXXX() functions
  175. above might be preferable.
  176. @section cmdlineparser_customization Customization
  177. wxCmdLineParser has several global options which may be changed by the
  178. application. All of the functions described in this section should be
  179. called before Parse().
  180. First global option is the support for long (also known as GNU-style)
  181. options. The long options are the ones which start with two dashes and look
  182. like "\--verbose", i.e. they generally are complete words and not some
  183. abbreviations of them. As long options are used by more and more
  184. applications, they are enabled by default, but may be disabled with
  185. DisableLongOptions().
  186. Another global option is the set of characters which may be used to start
  187. an option (otherwise, the word on the command line is assumed to be a
  188. parameter). Under Unix, @c "-" is always used, but Windows has at least two
  189. common choices for this: @c "-" and @c "/". Some programs also use "+". The
  190. default is to use what suits most the current platform, but may be changed
  191. with SetSwitchChars() method.
  192. Finally, SetLogo() can be used to show some application-specific text
  193. before the explanation given by Usage() function.
  194. @section cmdlineparser_parsing Parsing the Command Line
  195. After the command line description was constructed and the desired options
  196. were set, you can finally call Parse() method. It returns 0 if the command
  197. line was correct and was parsed, -1 if the help option was specified (this
  198. is a separate case as, normally, the program will terminate after this) or
  199. a positive number if there was an error during the command line parsing.
  200. In the latter case, the appropriate error message and usage information are
  201. logged by wxCmdLineParser itself using the standard wxWidgets logging
  202. functions.
  203. @section cmdlineparser_results Getting Results
  204. After calling Parse() (and if it returned 0), you may access the results of
  205. parsing using one of overloaded Found() methods.
  206. For a simple switch, you will simply call Found to determine if the switch
  207. was given or not, for an option or a parameter, you will call a version of
  208. Found() which also returns the associated value in the provided variable.
  209. All Found() functions return true if the switch or option were found in the
  210. command line or false if they were not specified.
  211. @library{wxbase}
  212. @category{appmanagement}
  213. @see wxApp::argc, wxApp::argv, @ref page_samples_console
  214. */
  215. class wxCmdLineParser
  216. {
  217. public:
  218. /**
  219. Default constructor, you must use SetCmdLine() later.
  220. */
  221. wxCmdLineParser();
  222. /**
  223. Constructor which specifies the command line to parse. This is the
  224. traditional (Unix) command line format. The parameters @a argc and
  225. @a argv have the same meaning as the typical @c main() function.
  226. This constructor is available in both ANSI and Unicode modes because under
  227. some platforms the command line arguments are passed as ASCII strings
  228. even to Unicode programs.
  229. */
  230. wxCmdLineParser(int argc, char** argv);
  231. /**
  232. Constructor which specifies the command line to parse.
  233. This is the traditional (Unix) command line format.
  234. The parameters @a argc and @a argv have the same meaning as the typical
  235. @c main() function.
  236. This constructor is only available in Unicode build.
  237. */
  238. wxCmdLineParser(int argc, wchar_t** argv);
  239. /**
  240. Constructor which specify the command line to parse in Windows format.
  241. The parameter cmdline has the same meaning as the corresponding
  242. parameter of @c WinMain().
  243. */
  244. wxCmdLineParser(const wxString& cmdline);
  245. /**
  246. Specifies the @ref SetDesc() "command line description" but not the
  247. command line. You must use SetCmdLine() later.
  248. */
  249. wxCmdLineParser(const wxCmdLineEntryDesc* desc);
  250. /**
  251. Specifies both the command line (in Unix format) and the
  252. @ref SetDesc() "command line description".
  253. */
  254. wxCmdLineParser(const wxCmdLineEntryDesc* desc, int argc, char** argv);
  255. /**
  256. Specifies both the command line (in Windows format) and the
  257. @ref SetDesc() "command line description".
  258. */
  259. wxCmdLineParser(const wxCmdLineEntryDesc* desc,
  260. const wxString& cmdline);
  261. /**
  262. Frees resources allocated by the object.
  263. @note This destructor is not virtual, don't use this class
  264. polymorphically.
  265. */
  266. ~wxCmdLineParser();
  267. /**
  268. Adds an option with only long form.
  269. This is just a convenient wrapper for AddOption() passing an empty
  270. string as short option name.
  271. @since 2.9.3
  272. */
  273. void AddLongOption(const wxString& lng,
  274. const wxString& desc = wxEmptyString,
  275. wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
  276. int flags = 0);
  277. /**
  278. Adds a switch with only long form.
  279. This is just a convenient wrapper for AddSwitch() passing an empty
  280. string as short switch name.
  281. @since 2.9.3
  282. */
  283. void AddLongSwitch(const wxString& lng,
  284. const wxString& desc = wxEmptyString,
  285. int flags = 0);
  286. /**
  287. Add an option @a name with an optional long name @a lng (no long name
  288. if it is empty, which is default) taking a value of the given type
  289. (string by default) to the command line description.
  290. */
  291. void AddOption(const wxString& name,
  292. const wxString& lng = wxEmptyString,
  293. const wxString& desc = wxEmptyString,
  294. wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
  295. int flags = 0);
  296. /**
  297. Add a parameter of the given @a type to the command line description.
  298. */
  299. void AddParam(const wxString& desc = wxEmptyString,
  300. wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
  301. int flags = 0);
  302. /**
  303. Add a switch @a name with an optional long name @a lng (no long name if
  304. it is empty, which is default), description @a desc and flags @a flags
  305. to the command line description.
  306. */
  307. void AddSwitch(const wxString& name,
  308. const wxString& lng = wxEmptyString,
  309. const wxString& desc = wxEmptyString,
  310. int flags = 0);
  311. /**
  312. Add a string @a text to the command line description shown by Usage().
  313. @since 2.9.0
  314. */
  315. void AddUsageText(const wxString& text);
  316. /**
  317. Returns @true if long options are enabled, otherwise @false.
  318. @see EnableLongOptions()
  319. */
  320. bool AreLongOptionsEnabled() const;
  321. /**
  322. Breaks down the string containing the full command line in words.
  323. Words are separated by whitespace and double quotes can be used to
  324. preserve the spaces inside the words.
  325. By default, this function uses Windows-like word splitting algorithm,
  326. i.e. single quotes have no special meaning and backslash can't be used
  327. to escape spaces neither. With @c wxCMD_LINE_SPLIT_UNIX flag Unix
  328. semantics is used, i.e. both single and double quotes can be used and
  329. backslash can be used to escape all the other special characters.
  330. */
  331. static wxArrayString
  332. ConvertStringToArgs(const wxString& cmdline,
  333. wxCmdLineSplitType flags = wxCMD_LINE_SPLIT_DOS);
  334. /**
  335. Identical to EnableLongOptions(@false).
  336. */
  337. void DisableLongOptions();
  338. /**
  339. Enable or disable support for the long options.
  340. As long options are not (yet) POSIX-compliant, this option allows to
  341. disable them.
  342. @see @ref cmdlineparser_customization and AreLongOptionsEnabled()
  343. */
  344. void EnableLongOptions(bool enable = true);
  345. /**
  346. Returns @true if the given switch was found, @false otherwise.
  347. */
  348. bool Found(const wxString& name) const;
  349. /**
  350. Returns whether the switch was found on the command line and whether it
  351. was negated.
  352. This method can be used for any kind of switch but is especially useful
  353. for switches that can be negated, i.e. were added with
  354. wxCMD_LINE_SWITCH_NEGATABLE flag, as otherwise Found() is simpler to
  355. use.
  356. However Found() doesn't allow to distinguish between switch specified
  357. normally, i.e. without dash following it, and negated switch, i.e. with
  358. the following dash. This method will return @c wxCMD_SWITCH_ON or @c
  359. wxCMD_SWITCH_OFF depending on whether the switch was negated or not.
  360. And if the switch was not found at all, @c wxCMD_SWITCH_NOT_FOUND is
  361. returned.
  362. @since 2.9.2
  363. */
  364. wxCmdLineSwitchState FoundSwitch(const wxString& name) const;
  365. /**
  366. Returns true if an option taking a string value was found and stores
  367. the value in the provided pointer (which should not be @NULL).
  368. */
  369. bool Found(const wxString& name, wxString* value) const;
  370. /**
  371. Returns @true if an option taking an integer value was found and stores
  372. the value in the provided pointer (which should not be @NULL).
  373. */
  374. bool Found(const wxString& name, long* value) const;
  375. /**
  376. Returns @true if an option taking a float value was found and stores
  377. the value in the provided pointer (which should not be @NULL).
  378. */
  379. bool Found(const wxString& name, double* value) const;
  380. /**
  381. Returns @true if an option taking a date value was found and stores the
  382. value in the provided pointer (which should not be @NULL).
  383. */
  384. bool Found(const wxString& name, wxDateTime* value) const;
  385. /**
  386. Returns the value of Nth parameter (as string only).
  387. */
  388. wxString GetParam(size_t n = 0) const;
  389. /**
  390. Returns the number of parameters found. This function makes sense
  391. mostly if you had used @c wxCMD_LINE_PARAM_MULTIPLE flag.
  392. */
  393. size_t GetParamCount() const;
  394. /**
  395. Parse the command line, return 0 if ok, -1 if @c "-h" or @c "\--help"
  396. option was encountered and the help message was given or a positive
  397. value if a syntax error occurred.
  398. @param giveUsage
  399. If @true (default), the usage message is given if a syntax error
  400. was encountered while parsing the command line or if help was
  401. requested. If @false, only error messages about possible syntax
  402. errors are given, use Usage to show the usage message from the
  403. caller if needed.
  404. */
  405. int Parse(bool giveUsage = true);
  406. //@{
  407. /**
  408. Set the command line to parse after using one of the constructors which
  409. don't do it.
  410. */
  411. void SetCmdLine(int argc, char** argv);
  412. void SetCmdLine(int argc, wchar_t** argv);
  413. void SetCmdLine(const wxString& cmdline);
  414. //@}
  415. /**
  416. Constructs the command line description.
  417. Take the command line description from the wxCMD_LINE_NONE terminated
  418. table.
  419. Example of usage:
  420. @code
  421. static const wxCmdLineEntryDesc cmdLineDesc[] =
  422. {
  423. { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
  424. { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
  425. { wxCMD_LINE_OPTION, "o", "output", "output file" },
  426. { wxCMD_LINE_OPTION, "i", "input", "input dir" },
  427. { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER },
  428. { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE },
  429. { wxCMD_LINE_PARAM, NULL, NULL, "input file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
  430. { wxCMD_LINE_NONE }
  431. };
  432. wxCmdLineParser parser;
  433. parser.SetDesc(cmdLineDesc);
  434. @endcode
  435. */
  436. void SetDesc(const wxCmdLineEntryDesc* desc);
  437. /**
  438. The @a logo is some extra text which will be shown by Usage() method.
  439. */
  440. void SetLogo(const wxString& logo);
  441. /**
  442. @a switchChars contains all characters with which an option or switch
  443. may start. Default is @c "-" for Unix, @c "-/" for Windows.
  444. */
  445. void SetSwitchChars(const wxString& switchChars);
  446. /**
  447. Give the standard usage message describing all program options. It will
  448. use the options and parameters descriptions specified earlier, so the
  449. resulting message will not be helpful to the user unless the
  450. descriptions were indeed specified.
  451. @see SetLogo()
  452. */
  453. void Usage() const;
  454. /**
  455. Return the string containing the program usage description.
  456. Call Usage() to directly show this string to the user.
  457. */
  458. wxString GetUsageString() const;
  459. };