![]() |
|
|||
File indexing completed on 2025-04-19 09:09:53
0001 /* 0002 * The Lean Mean C++ Option Parser 0003 * 0004 * Copyright (C) 2012 Matthias S. Benkmann 0005 * 0006 * The "Software" in the following 2 paragraphs refers to this file containing 0007 * the code to The Lean Mean C++ Option Parser. 0008 * The "Software" does NOT refer to any other files which you 0009 * may have received alongside this file (e.g. as part of a larger project that 0010 * incorporates The Lean Mean C++ Option Parser). 0011 * 0012 * Permission is hereby granted, free of charge, to any person obtaining a copy 0013 * of this software, to deal in the Software without restriction, including 0014 * without limitation the rights to use, copy, modify, merge, publish, 0015 * distribute, sublicense, and/or sell copies of the Software, and to permit 0016 * persons to whom the Software is furnished to do so, subject to the following 0017 * conditions: 0018 * The above copyright notice and this permission notice shall be included in 0019 * all copies or substantial portions of the Software. 0020 * 0021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0022 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0023 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0024 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0025 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0026 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0027 * SOFTWARE. 0028 */ 0029 0030 /* 0031 * NOTE: It is recommended that you read the processed HTML doxygen documentation 0032 * rather than this source. If you don't know doxygen, it's like javadoc for C++. 0033 * If you don't want to install doxygen you can find a copy of the processed 0034 * documentation at 0035 * 0036 * http://optionparser.sourceforge.net/ 0037 * 0038 */ 0039 0040 /** 0041 * @file 0042 * 0043 * @brief This is the only file required to use The Lean Mean C++ Option Parser. 0044 * Just \#include it and you're set. 0045 * 0046 * The Lean Mean C++ Option Parser handles the program's command line arguments 0047 * (argc, argv). 0048 * It supports the short and long option formats of getopt(), getopt_long() 0049 * and getopt_long_only() but has a more convenient interface. 0050 * The following features set it apart from other option parsers: 0051 * 0052 * @par Highlights: 0053 * <ul style="padding-left:1em;margin-left:0"> 0054 * <li> It is a header-only library. Just <code>\#include "optionparser.h"</code> and you're set. 0055 * <li> It is freestanding. There are no dependencies whatsoever, not even the 0056 * C or C++ standard library. 0057 * <li> It has a usage message formatter that supports column alignment and 0058 * line wrapping. This aids localization because it adapts to 0059 * translated strings that are shorter or longer (even if they contain 0060 * Asian wide characters). 0061 * <li> Unlike getopt() and derivatives it doesn't force you to loop through 0062 * options sequentially. Instead you can access options directly like this: 0063 * <ul style="margin-top:.5em"> 0064 * <li> Test for presence of a switch in the argument vector: 0065 * @code if ( options[QUIET] ) ... @endcode 0066 * <li> Evaluate --enable-foo/--disable-foo pair where the last one used wins: 0067 * @code if ( options[FOO].last()->type() == DISABLE ) ... @endcode 0068 * <li> Cumulative option (-v verbose, -vv more verbose, -vvv even more verbose): 0069 * @code int verbosity = options[VERBOSE].count(); @endcode 0070 * <li> Iterate over all --file=<fname> arguments: 0071 * @code for (Option* opt = options[FILE]; opt; opt = opt->next()) 0072 * fname = opt->arg; ... @endcode 0073 * <li> If you really want to, you can still process all arguments in order: 0074 * @code 0075 * for (int i = 0; i < p.optionsCount(); ++i) { 0076 * Option& opt = buffer[i]; 0077 * switch(opt.index()) { 0078 * case HELP: ... 0079 * case VERBOSE: ... 0080 * case FILE: fname = opt.arg; ... 0081 * case UNKNOWN: ... 0082 * @endcode 0083 * </ul> 0084 * </ul> @n 0085 * Despite these features the code size remains tiny. 0086 * It is smaller than <a href="http://uclibc.org">uClibc</a>'s GNU getopt() and just a 0087 * couple 100 bytes larger than uClibc's SUSv3 getopt(). @n 0088 * (This does not include the usage formatter, of course. But you don't have to use that.) 0089 * 0090 * @par Download: 0091 * Tarball with examples and test programs: 0092 * <a style="font-size:larger;font-weight:bold" href="http://sourceforge.net/projects/optionparser/files/optionparser-1.4.tar.gz/download">optionparser-1.4.tar.gz</a> @n 0093 * Just the header (this is all you really need): 0094 * <a style="font-size:larger;font-weight:bold" href="http://optionparser.sourceforge.net/optionparser.h">optionparser.h</a> 0095 * 0096 * @par Changelog: 0097 * <b>Version 1.4:</b> Fixed 2 printUsage() bugs that messed up output with small COLUMNS values @n 0098 * <b>Version 1.3:</b> Compatible with Microsoft Visual C++. @n 0099 * <b>Version 1.2:</b> Added @ref ATOOLS::Option_Parser::Option::namelen "Option::namelen" and removed the extraction 0100 * of short option characters into a special buffer. @n 0101 * Changed @ref ATOOLS::Option_Parser::Arg::Optional "Arg::Optional" to accept arguments if they are attached 0102 * rather than separate. This is what GNU getopt() does and how POSIX recommends 0103 * utilities should interpret their arguments.@n 0104 * <b>Version 1.1:</b> Optional mode with argument reordering as done by GNU getopt(), so that 0105 * options and non-options can be mixed. See 0106 * @ref ATOOLS::Option_Parser::Parser::parse() "Parser::parse()". 0107 * 0108 * @par Feedback: 0109 * Send questions, bug reports, feature requests etc. to: <tt><b>optionparser-feedback<span id="antispam"> (a) </span>lists.sourceforge.net</b></tt> 0110 * @htmlonly <script type="text/javascript">document.getElementById("antispam").innerHTML="@"</script> @endhtmlonly 0111 * 0112 * 0113 * @par Example program: 0114 * (Note: @c option::* identifiers are links that take you to their documentation.) 0115 * @code 0116 * #error EXAMPLE SHORTENED FOR READABILITY. BETTER EXAMPLES ARE IN THE .TAR.GZ! 0117 * #include <iostream> 0118 * #include "optionparser.h" 0119 * 0120 * enum optionIndex { UNKNOWN, HELP, PLUS }; 0121 * const option::Descriptor usage[] = 0122 * { 0123 * {UNKNOWN, 0,"" , "" ,option::Arg::None, "USAGE: example [options]\n\n" 0124 * "Options:" }, 0125 * {HELP, 0,"" , "help",option::Arg::None, " --help \tPrint usage and exit." }, 0126 * {PLUS, 0,"p", "plus",option::Arg::None, " --plus, -p \tIncrement count." }, 0127 * {UNKNOWN, 0,"" , "" ,option::Arg::None, "\nExamples:\n" 0128 * " example --unknown -- --this_is_no_option\n" 0129 * " example -unk --plus -ppp file1 file2\n" }, 0130 * {0,0,0,0,0,0} 0131 * }; 0132 * 0133 * int main(int argc, char* argv[]) 0134 * { 0135 * argc-=(argc>0); argv+=(argc>0); // skip program name argv[0] if present 0136 * option::Stats stats(usage, argc, argv); 0137 * option::Option options[stats.options_max], buffer[stats.buffer_max]; 0138 * option::Parser parse(usage, argc, argv, options, buffer); 0139 * 0140 * if (parse.error()) 0141 * return 1; 0142 * 0143 * if (options[HELP] || argc == 0) { 0144 * option::printUsage(std::cout, usage); 0145 * return 0; 0146 * } 0147 * 0148 * std::cout << "--plus count: " << 0149 * options[PLUS].count() << "\n"; 0150 * 0151 * for (option::Option* opt = options[UNKNOWN]; opt; opt = opt->next()) 0152 * std::cout << "Unknown option: " << opt->name << "\n"; 0153 * 0154 * for (int i = 0; i < parse.nonOptionsCount(); ++i) 0155 * std::cout << "Non-option #" << i << ": " << parse.nonOption(i) << "\n"; 0156 * } 0157 * @endcode 0158 * 0159 * @par Option syntax: 0160 * @li The Lean Mean C++ Option Parser follows POSIX <code>getopt()</code> conventions and supports 0161 * GNU-style <code>getopt_long()</code> long options as well as Perl-style single-minus 0162 * long options (<code>getopt_long_only()</code>). 0163 * @li short options have the format @c -X where @c X is any character that fits in a char. 0164 * @li short options can be grouped, i.e. <code>-X -Y</code> is equivalent to @c -XY. 0165 * @li a short option may take an argument either separate (<code>-X foo</code>) or 0166 * attached (@c -Xfoo). You can make the parser accept the additional format @c -X=foo by 0167 * registering @c X as a long option (in addition to being a short option) and 0168 * enabling single-minus long options. 0169 * @li an argument-taking short option may be grouped if it is the last in the group, e.g. 0170 * @c -ABCXfoo or <code> -ABCX foo </code> (@c foo is the argument to the @c -X option). 0171 * @li a lone minus character @c '-' is not treated as an option. It is customarily used where 0172 * a file name is expected to refer to stdin or stdout. 0173 * @li long options have the format @c --option-name. 0174 * @li the option-name of a long option can be anything and include any characters. 0175 * Even @c = characters will work, but don't do that. 0176 * @li [optional] long options may be abbreviated as long as the abbreviation is unambiguous. 0177 * You can set a minimum length for abbreviations. 0178 * @li [optional] long options may begin with a single minus. The double minus form is always 0179 * accepted, too. 0180 * @li a long option may take an argument either separate (<code> --option arg </code>) or 0181 * attached (<code> --option=arg </code>). In the attached form the equals sign is mandatory. 0182 * @li an empty string can be passed as an attached long option argument: <code> --option-name= </code>. 0183 * Note the distinction between an empty string as argument and no argument at all. 0184 * @li an empty string is permitted as separate argument to both long and short options. 0185 * @li Arguments to both short and long options may start with a @c '-' character. E.g. 0186 * <code> -X-X </code>, <code>-X -X</code> or <code> --long-X=-X </code>. If @c -X 0187 * and @c --long-X take an argument, that argument will be @c "-X" in all 3 cases. 0188 * @li If using the built-in @ref ATOOLS::Option_Parser::Arg::Optional "Arg::Optional", optional arguments must 0189 * be attached. 0190 * @li the special option @c -- (i.e. without a name) terminates the list of 0191 * options. Everything that follows is a non-option argument, even if it starts with 0192 * a @c '-' character. The @c -- itself will not appear in the parse results. 0193 * @li the first argument that doesn't start with @c '-' or @c '--' and does not belong to 0194 * a preceding argument-taking option, will terminate the option list and is the 0195 * first non-option argument. All following command line arguments are treated as 0196 * non-option arguments, even if they start with @c '-' . @n 0197 * NOTE: This behaviour is mandated by POSIX, but GNU getopt() only honours this if it is 0198 * explicitly requested (e.g. by setting POSIXLY_CORRECT). @n 0199 * You can enable the GNU behaviour by passing @c true as first argument to 0200 * e.g. @ref ATOOLS::Option_Parser::Parser::parse() "Parser::parse()". 0201 * @li Arguments that look like options (i.e. @c '-' followed by at least 1 character) but 0202 * aren't, are NOT treated as non-option arguments. They are treated as unknown options and 0203 * are collected into a list of unknown options for error reporting. @n 0204 * This means that in order to pass a first non-option 0205 * argument beginning with the minus character it is required to use the 0206 * @c -- special option, e.g. 0207 * @code 0208 * program -x -- --strange-filename 0209 * @endcode 0210 * In this example, @c --strange-filename is a non-option argument. If the @c -- 0211 * were omitted, it would be treated as an unknown option. @n 0212 * See @ref ATOOLS::Option_Parser::Descriptor::longopt for information on how to collect unknown options. 0213 * 0214 */ 0215 0216 #ifndef ATOOLS_Org_Option_Parser_H 0217 #define ATOOLS_Org_Option_Parser_H 0218 0219 #include <cstdio> 0220 0221 namespace ATOOLS 0222 { 0223 0224 /** @brief The namespace of The Lean Mean C++ Option Parser. */ 0225 namespace Option_Parser 0226 { 0227 0228 #ifdef _MSC_VER 0229 #include <intrin.h> 0230 #pragma intrinsic(_BitScanReverse) 0231 struct MSC_Builtin_CLZ 0232 { 0233 static int builtin_clz(unsigned x) 0234 { 0235 unsigned long index; 0236 _BitScanReverse(&index, x); 0237 return 32-index; // int is always 32bit on Windows, even for target x64 0238 } 0239 }; 0240 #define __builtin_clz(x) MSC_Builtin_CLZ::builtin_clz(x) 0241 #endif 0242 0243 class Option; 0244 0245 /** 0246 * @brief Possible results when checking if an argument is valid for a certain option. 0247 * 0248 * In the case that no argument is provided for an option that takes an 0249 * optional argument, return codes @c ARG_OK and @c ARG_IGNORE are equivalent. 0250 */ 0251 enum ArgStatus 0252 { 0253 //! The option does not take an argument. 0254 ARG_NONE, 0255 //! The argument is acceptable for the option. 0256 ARG_OK, 0257 //! The argument is not acceptable but that's non-fatal because the option's argument is optional. 0258 ARG_IGNORE, 0259 //! The argument is not acceptable and that's fatal. 0260 ARG_ILLEGAL 0261 }; 0262 0263 /** 0264 * @brief Signature of functions that check if an argument is valid for a certain type of option. 0265 * 0266 * Every Option has such a function assigned in its Descriptor. 0267 * @code 0268 * Descriptor usage[] = { {UNKNOWN, 0, "", "", Arg::None, ""}, ... }; 0269 * @endcode 0270 * 0271 * A CheckArg function has the following signature: 0272 * @code ArgStatus CheckArg(const Option& option, bool msg); @endcode 0273 * 0274 * It is used to check if a potential argument would be acceptable for the option. 0275 * It will even be called if there is no argument. In that case @c option.arg will be @c NULL. 0276 * 0277 * If @c msg is @c true and the function determines that an argument is not acceptable and 0278 * that this is a fatal error, it should output a message to the user before 0279 * returning @ref ARG_ILLEGAL. If @c msg is @c false the function should remain silent (or you 0280 * will get duplicate messages). 0281 * 0282 * See @ref ArgStatus for the meaning of the return values. 0283 * 0284 * While you can provide your own functions, 0285 * often the following pre-defined checks (which never return @ref ARG_ILLEGAL) will suffice: 0286 * 0287 * @li @c Arg::None @copybrief Arg::None 0288 * @li @c Arg::Optional @copybrief Arg::Optional 0289 * 0290 */ 0291 typedef ArgStatus (*CheckArg)(const Option& option, bool msg); 0292 0293 /** 0294 * @brief Describes an option, its help text (usage) and how it should be parsed. 0295 * 0296 * The main input when constructing an option::Parser is an array of Descriptors. 0297 0298 * @par Example: 0299 * @code 0300 * enum OptionIndex {CREATE, ...}; 0301 * enum OptionType {DISABLE, ENABLE, OTHER}; 0302 * 0303 * const option::Descriptor usage[] = { 0304 * { CREATE, // index 0305 * OTHER, // type 0306 * "c", // shortopt 0307 * "create", // longopt 0308 * Arg::None, // check_arg 0309 * "--create Tells the program to create something." // help 0310 * } 0311 * , ... 0312 * }; 0313 * @endcode 0314 */ 0315 struct Descriptor 0316 { 0317 /** 0318 * @brief Index of this option's linked list in the array filled in by the parser. 0319 * 0320 * Command line options whose Descriptors have the same index will end up in the same 0321 * linked list in the order in which they appear on the command line. If you have 0322 * multiple long option aliases that refer to the same option, give their descriptors 0323 * the same @c index. 0324 * 0325 * If you have options that mean exactly opposite things 0326 * (e.g. @c --enable-foo and @c --disable-foo ), you should also give them the same 0327 * @c index, but distinguish them through different values for @ref type. 0328 * That way they end up in the same list and you can just take the last element of the 0329 * list and use its type. This way you get the usual behaviour where switches later 0330 * on the command line override earlier ones without having to code it manually. 0331 * 0332 * @par Tip: 0333 * Use an enum rather than plain ints for better readability, as shown in the example 0334 * at Descriptor. 0335 */ 0336 const unsigned index; 0337 0338 /** 0339 * @brief Used to distinguish between options with the same @ref index. 0340 * See @ref index for details. 0341 * 0342 * It is recommended that you use an enum rather than a plain int to make your 0343 * code more readable. 0344 */ 0345 const int type; 0346 0347 /** 0348 * @brief Each char in this string will be accepted as a short option character. 0349 * 0350 * The string must not include the minus character @c '-' or you'll get undefined 0351 * behaviour. 0352 * 0353 * If this Descriptor should not have short option characters, use the empty 0354 * string "". NULL is not permitted here! 0355 * 0356 * See @ref longopt for more information. 0357 */ 0358 const char* const shortopt; 0359 0360 /** 0361 * @brief The long option name (without the leading @c -- ). 0362 * 0363 * If this Descriptor should not have a long option name, use the empty 0364 * string "". NULL is not permitted here! 0365 * 0366 * While @ref shortopt allows multiple short option characters, each 0367 * Descriptor can have only a single long option name. If you have multiple 0368 * long option names referring to the same option use separate Descriptors 0369 * that have the same @ref index and @ref type. You may repeat 0370 * short option characters in such an alias Descriptor but there's no need to. 0371 * 0372 * @par Dummy Descriptors: 0373 * You can use dummy Descriptors with an 0374 * empty string for both @ref shortopt and @ref longopt to add text to 0375 * the usage that is not related to a specific option. See @ref help. 0376 * The first dummy Descriptor will be used for unknown options (see below). 0377 * 0378 * @par Unknown Option Descriptor: 0379 * The first dummy Descriptor in the list of Descriptors, 0380 * whose @ref shortopt and @ref longopt are both the empty string, will be used 0381 * as the Descriptor for unknown options. An unknown option is a string in 0382 * the argument vector that is not a lone minus @c '-' but starts with a minus 0383 * character and does not match any Descriptor's @ref shortopt or @ref longopt. @n 0384 * Note that the dummy descriptor's @ref check_arg function @e will be called and 0385 * its return value will be evaluated as usual. I.e. if it returns @ref ARG_ILLEGAL 0386 * the parsing will be aborted with <code>Parser::error()==true</code>. @n 0387 * if @c check_arg does not return @ref ARG_ILLEGAL the descriptor's 0388 * @ref index @e will be used to pick the linked list into which 0389 * to put the unknown option. @n 0390 * If there is no dummy descriptor, unknown options will be dropped silently. 0391 * 0392 */ 0393 const char* const longopt; 0394 0395 /** 0396 * @brief For each option that matches @ref shortopt or @ref longopt this function 0397 * will be called to check a potential argument to the option. 0398 * 0399 * This function will be called even if there is no potential argument. In that case 0400 * it will be passed @c NULL as @c arg parameter. Do not confuse this with the empty 0401 * string. 0402 * 0403 * See @ref CheckArg for more information. 0404 */ 0405 const CheckArg check_arg; 0406 0407 /** 0408 * @brief The usage text associated with the options in this Descriptor. 0409 * 0410 * You can use option::printUsage() to format your usage message based on 0411 * the @c help texts. You can use dummy Descriptors where 0412 * @ref shortopt and @ref longopt are both the empty string to add text to 0413 * the usage that is not related to a specific option. 0414 * 0415 * See option::printUsage() for special formatting characters you can use in 0416 * @c help to get a column layout. 0417 * 0418 * @attention 0419 * Must be UTF-8-encoded. If your compiler supports C++11 you can use the "u8" 0420 * prefix to make sure string literals are properly encoded. 0421 */ 0422 const char* help; 0423 }; 0424 0425 /** 0426 * @brief A parsed option from the command line together with its argument if it has one. 0427 * 0428 * The Parser chains all parsed options with the same Descriptor::index together 0429 * to form a linked list. This allows you to easily implement all of the common ways 0430 * of handling repeated options and enable/disable pairs. 0431 * 0432 * @li Test for presence of a switch in the argument vector: 0433 * @code if ( options[QUIET] ) ... @endcode 0434 * @li Evaluate --enable-foo/--disable-foo pair where the last one used wins: 0435 * @code if ( options[FOO].last()->type() == DISABLE ) ... @endcode 0436 * @li Cumulative option (-v verbose, -vv more verbose, -vvv even more verbose): 0437 * @code int verbosity = options[VERBOSE].count(); @endcode 0438 * @li Iterate over all --file=<fname> arguments: 0439 * @code for (Option* opt = options[FILE]; opt; opt = opt->next()) 0440 * fname = opt->arg; ... @endcode 0441 */ 0442 class Option 0443 { 0444 Option* next_; 0445 Option* prev_; 0446 public: 0447 /** 0448 * @brief Pointer to this Option's Descriptor. 0449 * 0450 * Remember that the first dummy descriptor (see @ref Descriptor::longopt) is used 0451 * for unknown options. 0452 * 0453 * @attention 0454 * @c desc==NULL signals that this Option is unused. This is the default state of 0455 * elements in the result array. You don't need to test @c desc explicitly. You 0456 * can simply write something like this: 0457 * @code 0458 * if (options[CREATE]) 0459 * { 0460 * ... 0461 * } 0462 * @endcode 0463 * This works because of <code> operator const Option*() </code>. 0464 */ 0465 const Descriptor* desc; 0466 0467 /** 0468 * @brief The name of the option as used on the command line. 0469 * 0470 * The main purpose of this string is to be presented to the user in messages. 0471 * 0472 * In the case of a long option, this is the actual @c argv pointer, i.e. the first 0473 * character is a '-'. In the case of a short option this points to the option 0474 * character within the @c argv string. 0475 * 0476 * Note that in the case of a short option group or an attached option argument, this 0477 * string will contain additional characters following the actual name. Use @ref namelen 0478 * to filter out the actual option name only. 0479 * 0480 */ 0481 const char* name; 0482 0483 /** 0484 * @brief Pointer to this Option's argument (if any). 0485 * 0486 * NULL if this option has no argument. Do not confuse this with the empty string which 0487 * is a valid argument. 0488 */ 0489 const char* arg; 0490 0491 /** 0492 * @brief The length of the option @ref name. 0493 * 0494 * Because @ref name points into the actual @c argv string, the option name may be 0495 * followed by more characters (e.g. other short options in the same short option group). 0496 * This value is the number of bytes (not characters!) that are part of the actual name. 0497 * 0498 * For a short option, this length is always 1. For a long option this length is always 0499 * at least 2 if single minus long options are permitted and at least 3 if they are disabled. 0500 * 0501 * @note 0502 * In the pathological case of a minus within a short option group (e.g. @c -xf-z), this 0503 * length is incorrect, because this case will be misinterpreted as a long option and the 0504 * name will therefore extend to the string's 0-terminator or a following '=" character 0505 * if there is one. This is irrelevant for most uses of @ref name and @c namelen. If you 0506 * really need to distinguish the case of a long and a short option, compare @ref name to 0507 * the @c argv pointers. A long option's @c name is always identical to one of them, 0508 * whereas a short option's is never. 0509 */ 0510 int namelen; 0511 0512 /** 0513 * @brief Returns Descriptor::type of this Option's Descriptor, or 0 if this Option 0514 * is invalid (unused). 0515 * 0516 * Because this method (and last(), too) can be used even on unused Options with desc==0, you can (provided 0517 * you arrange your types properly) switch on type() without testing validity first. 0518 * @code 0519 * enum OptionType { UNUSED=0, DISABLED=0, ENABLED=1 }; 0520 * enum OptionIndex { FOO }; 0521 * const Descriptor usage[] = { 0522 * { FOO, ENABLED, "", "enable-foo", Arg::None, 0 }, 0523 * { FOO, DISABLED, "", "disable-foo", Arg::None, 0 }, 0524 * { 0, 0, 0, 0, 0, 0 } }; 0525 * ... 0526 * switch(options[FOO].last()->type()) // no validity check required! 0527 * { 0528 * case ENABLED: ... 0529 * case DISABLED: ... // UNUSED==DISABLED ! 0530 * } 0531 * @endcode 0532 */ 0533 int type() const 0534 { 0535 return desc == 0 ? 0 : desc->type; 0536 } 0537 0538 /** 0539 * @brief Returns Descriptor::index of this Option's Descriptor, or -1 if this Option 0540 * is invalid (unused). 0541 */ 0542 int index() const 0543 { 0544 return desc == 0 ? -1 : (int)desc->index; 0545 } 0546 0547 /** 0548 * @brief Returns the number of times this Option (or others with the same Descriptor::index) 0549 * occurs in the argument vector. 0550 * 0551 * This corresponds to the number of elements in the linked list this Option is part of. 0552 * It doesn't matter on which element you call count(). The return value is always the same. 0553 * 0554 * Use this to implement cumulative options, such as -v, -vv, -vvv for 0555 * different verbosity levels. 0556 * 0557 * Returns 0 when called for an unused/invalid option. 0558 */ 0559 int count() 0560 { 0561 int c = (desc == 0 ? 0 : 1); 0562 Option* p = first(); 0563 while (!p->isLast()) 0564 { 0565 ++c; 0566 p = p->next_; 0567 }; 0568 return c; 0569 } 0570 0571 /** 0572 * @brief Returns true iff this is the first element of the linked list. 0573 * 0574 * The first element in the linked list is the first option on the command line 0575 * that has the respective Descriptor::index value. 0576 * 0577 * Returns true for an unused/invalid option. 0578 */ 0579 bool isFirst() const 0580 { 0581 return isTagged(prev_); 0582 } 0583 0584 /** 0585 * @brief Returns true iff this is the last element of the linked list. 0586 * 0587 * The last element in the linked list is the last option on the command line 0588 * that has the respective Descriptor::index value. 0589 * 0590 * Returns true for an unused/invalid option. 0591 */ 0592 bool isLast() const 0593 { 0594 return isTagged(next_); 0595 } 0596 0597 /** 0598 * @brief Returns a pointer to the first element of the linked list. 0599 * 0600 * Use this when you want the first occurrence of an option on the command line to 0601 * take precedence. Note that this is not the way most programs handle options. 0602 * You should probably be using last() instead. 0603 * 0604 * @note 0605 * This method may be called on an unused/invalid option and will return a pointer to the 0606 * option itself. 0607 */ 0608 Option* first() 0609 { 0610 Option* p = this; 0611 while (!p->isFirst()) 0612 p = p->prev_; 0613 return p; 0614 } 0615 0616 /** 0617 * @brief Returns a pointer to the last element of the linked list. 0618 * 0619 * Use this when you want the last occurrence of an option on the command line to 0620 * take precedence. This is the most common way of handling conflicting options. 0621 * 0622 * @note 0623 * This method may be called on an unused/invalid option and will return a pointer to the 0624 * option itself. 0625 * 0626 * @par Tip: 0627 * If you have options with opposite meanings (e.g. @c --enable-foo and @c --disable-foo), you 0628 * can assign them the same Descriptor::index to get them into the same list. Distinguish them by 0629 * Descriptor::type and all you have to do is check <code> last()->type() </code> to get 0630 * the state listed last on the command line. 0631 */ 0632 Option* last() 0633 { 0634 return first()->prevwrap(); 0635 } 0636 0637 /** 0638 * @brief Returns a pointer to the previous element of the linked list or NULL if 0639 * called on first(). 0640 * 0641 * If called on first() this method returns NULL. Otherwise it will return the 0642 * option with the same Descriptor::index that precedes this option on the command 0643 * line. 0644 */ 0645 Option* prev() 0646 { 0647 return isFirst() ? 0 : prev_; 0648 } 0649 0650 /** 0651 * @brief Returns a pointer to the previous element of the linked list with wrap-around from 0652 * first() to last(). 0653 * 0654 * If called on first() this method returns last(). Otherwise it will return the 0655 * option with the same Descriptor::index that precedes this option on the command 0656 * line. 0657 */ 0658 Option* prevwrap() 0659 { 0660 return untag(prev_); 0661 } 0662 0663 /** 0664 * @brief Returns a pointer to the next element of the linked list or NULL if called 0665 * on last(). 0666 * 0667 * If called on last() this method returns NULL. Otherwise it will return the 0668 * option with the same Descriptor::index that follows this option on the command 0669 * line. 0670 */ 0671 Option* next() 0672 { 0673 return isLast() ? 0 : next_; 0674 } 0675 0676 /** 0677 * @brief Returns a pointer to the next element of the linked list with wrap-around from 0678 * last() to first(). 0679 * 0680 * If called on last() this method returns first(). Otherwise it will return the 0681 * option with the same Descriptor::index that follows this option on the command 0682 * line. 0683 */ 0684 Option* nextwrap() 0685 { 0686 return untag(next_); 0687 } 0688 0689 /** 0690 * @brief Makes @c new_last the new last() by chaining it into the list after last(). 0691 * 0692 * It doesn't matter which element you call append() on. The new element will always 0693 * be appended to last(). 0694 * 0695 * @attention 0696 * @c new_last must not yet be part of a list, or that list will become corrupted, because 0697 * this method does not unchain @c new_last from an existing list. 0698 */ 0699 void append(Option* new_last) 0700 { 0701 Option* p = last(); 0702 Option* f = first(); 0703 p->next_ = new_last; 0704 new_last->prev_ = p; 0705 new_last->next_ = tag(f); 0706 f->prev_ = tag(new_last); 0707 } 0708 0709 /** 0710 * @brief Casts from Option to const Option* but only if this Option is valid. 0711 * 0712 * If this Option is valid (i.e. @c desc!=NULL), returns this. 0713 * Otherwise returns NULL. This allows testing an Option directly 0714 * in an if-clause to see if it is used: 0715 * @code 0716 * if (options[CREATE]) 0717 * { 0718 * ... 0719 * } 0720 * @endcode 0721 * It also allows you to write loops like this: 0722 * @code for (Option* opt = options[FILE]; opt; opt = opt->next()) 0723 * fname = opt->arg; ... @endcode 0724 */ 0725 operator const Option*() const 0726 { 0727 return desc ? this : 0; 0728 } 0729 0730 /** 0731 * @brief Casts from Option to Option* but only if this Option is valid. 0732 * 0733 * If this Option is valid (i.e. @c desc!=NULL), returns this. 0734 * Otherwise returns NULL. This allows testing an Option directly 0735 * in an if-clause to see if it is used: 0736 * @code 0737 * if (options[CREATE]) 0738 * { 0739 * ... 0740 * } 0741 * @endcode 0742 * It also allows you to write loops like this: 0743 * @code for (Option* opt = options[FILE]; opt; opt = opt->next()) 0744 * fname = opt->arg; ... @endcode 0745 */ 0746 operator Option*() 0747 { 0748 return desc ? this : 0; 0749 } 0750 0751 /** 0752 * @brief Creates a new Option that is a one-element linked list and has NULL 0753 * @ref desc, @ref name, @ref arg and @ref namelen. 0754 */ 0755 Option() : 0756 desc(0), name(0), arg(0), namelen(0) 0757 { 0758 prev_ = tag(this); 0759 next_ = tag(this); 0760 } 0761 0762 /** 0763 * @brief Creates a new Option that is a one-element linked list and has the given 0764 * values for @ref desc, @ref name and @ref arg. 0765 * 0766 * If @c name_ points at a character other than '-' it will be assumed to refer to a 0767 * short option and @ref namelen will be set to 1. Otherwise the length will extend to 0768 * the first '=' character or the string's 0-terminator. 0769 */ 0770 Option(const Descriptor* desc_, const char* name_, const char* arg_) 0771 { 0772 init(desc_, name_, arg_); 0773 } 0774 0775 /** 0776 * @brief Makes @c *this a copy of @c orig except for the linked list pointers. 0777 * 0778 * After this operation @c *this will be a one-element linked list. 0779 */ 0780 void operator=(const Option& orig) 0781 { 0782 init(orig.desc, orig.name, orig.arg); 0783 } 0784 0785 /** 0786 * @brief Makes @c *this a copy of @c orig except for the linked list pointers. 0787 * 0788 * After this operation @c *this will be a one-element linked list. 0789 */ 0790 Option(const Option& orig) 0791 { 0792 init(orig.desc, orig.name, orig.arg); 0793 } 0794 0795 private: 0796 /** 0797 * @internal 0798 * @brief Sets the fields of this Option to the given values (extracting @c name if necessary). 0799 * 0800 * If @c name_ points at a character other than '-' it will be assumed to refer to a 0801 * short option and @ref namelen will be set to 1. Otherwise the length will extend to 0802 * the first '=' character or the string's 0-terminator. 0803 */ 0804 void init(const Descriptor* desc_, const char* name_, const char* arg_) 0805 { 0806 desc = desc_; 0807 name = name_; 0808 arg = arg_; 0809 prev_ = tag(this); 0810 next_ = tag(this); 0811 namelen = 0; 0812 if (name == 0) 0813 return; 0814 namelen = 1; 0815 if (name[0] != '-') 0816 return; 0817 while (name[namelen] != 0 && name[namelen] != '=') 0818 ++namelen; 0819 } 0820 0821 static Option* tag(Option* ptr) 0822 { 0823 return (Option*) ((unsigned long long) ptr | 1); 0824 } 0825 0826 static Option* untag(Option* ptr) 0827 { 0828 return (Option*) ((unsigned long long) ptr & ~1ull); 0829 } 0830 0831 static bool isTagged(Option* ptr) 0832 { 0833 return ((unsigned long long) ptr & 1); 0834 } 0835 }; 0836 0837 /** 0838 * @brief Functions for checking the validity of option arguments. 0839 * 0840 * @copydetails CheckArg 0841 * 0842 * The following example code 0843 * can serve as starting place for writing your own more complex CheckArg functions: 0844 * @code 0845 * struct Arg: public option::Arg 0846 * { 0847 * static void printError(const char* msg1, const option::Option& opt, const char* msg2) 0848 * { 0849 * fprintf(stderr, "ERROR: %s", msg1); 0850 * fwrite(opt.name, opt.namelen, 1, stderr); 0851 * fprintf(stderr, "%s", msg2); 0852 * } 0853 * 0854 * static option::ArgStatus Unknown(const option::Option& option, bool msg) 0855 * { 0856 * if (msg) printError("Unknown option '", option, "'\n"); 0857 * return option::ARG_ILLEGAL; 0858 * } 0859 * 0860 * static option::ArgStatus Required(const option::Option& option, bool msg) 0861 * { 0862 * if (option.arg != 0) 0863 * return option::ARG_OK; 0864 * 0865 * if (msg) printError("Option '", option, "' requires an argument\n"); 0866 * return option::ARG_ILLEGAL; 0867 * } 0868 * 0869 * static option::ArgStatus NonEmpty(const option::Option& option, bool msg) 0870 * { 0871 * if (option.arg != 0 && option.arg[0] != 0) 0872 * return option::ARG_OK; 0873 * 0874 * if (msg) printError("Option '", option, "' requires a non-empty argument\n"); 0875 * return option::ARG_ILLEGAL; 0876 * } 0877 * 0878 * static option::ArgStatus Numeric(const option::Option& option, bool msg) 0879 * { 0880 * char* endptr = 0; 0881 * if (option.arg != 0 && strtol(option.arg, &endptr, 10)){}; 0882 * if (endptr != option.arg && *endptr == 0) 0883 * return option::ARG_OK; 0884 * 0885 * if (msg) printError("Option '", option, "' requires a numeric argument\n"); 0886 * return option::ARG_ILLEGAL; 0887 * } 0888 * }; 0889 * @endcode 0890 */ 0891 struct Arg 0892 { 0893 //! @brief For options that don't take an argument: Returns ARG_NONE. 0894 static ArgStatus None(const Option&, bool) 0895 { 0896 return ARG_NONE; 0897 } 0898 0899 //! @brief Returns ARG_OK if the argument is attached and ARG_IGNORE otherwise. 0900 static ArgStatus Optional(const Option& option, bool) 0901 { 0902 if (option.arg && option.name[option.namelen] != 0) 0903 return ARG_OK; 0904 else 0905 return ARG_IGNORE; 0906 } 0907 0908 //! @brief Returns ARG_OK only if an argument is attached and ARG_ILLEGAL otherwise. 0909 static ArgStatus Required(const Option& option, bool msg) 0910 { 0911 if (option.arg != 0) 0912 return ARG_OK; 0913 0914 if (msg) printError("Option '", option, "' requires an argument\n"); 0915 return ARG_ILLEGAL; 0916 } 0917 static void printError(const char* msg1, const Option& opt, const char* msg2) 0918 { 0919 fprintf(stderr, "ERROR: %s", msg1); 0920 fwrite(opt.name, opt.namelen, 1, stderr); 0921 fprintf(stderr, "%s", msg2); 0922 } 0923 }; 0924 0925 /** 0926 * @brief Determines the minimum lengths of the buffer and options arrays used for Parser. 0927 * 0928 * Because Parser doesn't use dynamic memory its output arrays have to be pre-allocated. 0929 * If you don't want to use fixed size arrays (which may turn out too small, causing 0930 * command line arguments to be dropped), you can use Stats to determine the correct sizes. 0931 * Stats work cumulative. You can first pass in your default options and then the real 0932 * options and afterwards the counts will reflect the union. 0933 */ 0934 struct Stats 0935 { 0936 /** 0937 * @brief Number of elements needed for a @c buffer[] array to be used for 0938 * @ref Parser::parse() "parsing" the same argument vectors that were fed 0939 * into this Stats object. 0940 * 0941 * @note 0942 * This number is always 1 greater than the actual number needed, to give 0943 * you a sentinel element. 0944 */ 0945 unsigned buffer_max; 0946 0947 /** 0948 * @brief Number of elements needed for an @c options[] array to be used for 0949 * @ref Parser::parse() "parsing" the same argument vectors that were fed 0950 * into this Stats object. 0951 * 0952 * @note 0953 * @li This number is always 1 greater than the actual number needed, to give 0954 * you a sentinel element. 0955 * @li This number depends only on the @c usage, not the argument vectors, because 0956 * the @c options array needs exactly one slot for each possible Descriptor::index. 0957 */ 0958 unsigned options_max; 0959 0960 /** 0961 * @brief Creates a Stats object with counts set to 1 (for the sentinel element). 0962 */ 0963 Stats() : 0964 buffer_max(1), options_max(1) // 1 more than necessary as sentinel 0965 { 0966 } 0967 0968 /** 0969 * @brief Creates a new Stats object and immediately updates it for the 0970 * given @c usage and argument vector. You may pass 0 for @c argc and/or @c argv, 0971 * if you just want to update @ref options_max. 0972 * 0973 * @note 0974 * The calls to Stats methods must match the later calls to Parser methods. 0975 * See Parser::parse() for the meaning of the arguments. 0976 */ 0977 Stats(bool gnu, const Descriptor usage[], int argc, const char** argv, int min_abbr_len = 0, // 0978 bool single_minus_longopt = false) : 0979 buffer_max(1), options_max(1) // 1 more than necessary as sentinel 0980 { 0981 add(gnu, usage, argc, argv, min_abbr_len, single_minus_longopt); 0982 } 0983 0984 //! @brief Stats(...) with non-const argv. 0985 Stats(bool gnu, const Descriptor usage[], int argc, char** argv, int min_abbr_len = 0, // 0986 bool single_minus_longopt = false) : 0987 buffer_max(1), options_max(1) // 1 more than necessary as sentinel 0988 { 0989 add(gnu, usage, argc, (const char**) argv, min_abbr_len, single_minus_longopt); 0990 } 0991 0992 //! @brief POSIX Stats(...) (gnu==false). 0993 Stats(const Descriptor usage[], int argc, const char** argv, int min_abbr_len = 0, // 0994 bool single_minus_longopt = false) : 0995 buffer_max(1), options_max(1) // 1 more than necessary as sentinel 0996 { 0997 add(false, usage, argc, argv, min_abbr_len, single_minus_longopt); 0998 } 0999 1000 //! @brief POSIX Stats(...) (gnu==false) with non-const argv. 1001 Stats(const Descriptor usage[], int argc, char** argv, int min_abbr_len = 0, // 1002 bool single_minus_longopt = false) : 1003 buffer_max(1), options_max(1) // 1 more than necessary as sentinel 1004 { 1005 add(false, usage, argc, (const char**) argv, min_abbr_len, single_minus_longopt); 1006 } 1007 1008 /** 1009 * @brief Updates this Stats object for the 1010 * given @c usage and argument vector. You may pass 0 for @c argc and/or @c argv, 1011 * if you just want to update @ref options_max. 1012 * 1013 * @note 1014 * The calls to Stats methods must match the later calls to Parser methods. 1015 * See Parser::parse() for the meaning of the arguments. 1016 */ 1017 void add(bool gnu, const Descriptor usage[], int argc, const char** argv, int min_abbr_len = 0, // 1018 bool single_minus_longopt = false); 1019 1020 //! @brief add() with non-const argv. 1021 void add(bool gnu, const Descriptor usage[], int argc, char** argv, int min_abbr_len = 0, // 1022 bool single_minus_longopt = false) 1023 { 1024 add(gnu, usage, argc, (const char**) argv, min_abbr_len, single_minus_longopt); 1025 } 1026 1027 //! @brief POSIX add() (gnu==false). 1028 void add(const Descriptor usage[], int argc, const char** argv, int min_abbr_len = 0, // 1029 bool single_minus_longopt = false) 1030 { 1031 add(false, usage, argc, argv, min_abbr_len, single_minus_longopt); 1032 } 1033 1034 //! @brief POSIX add() (gnu==false) with non-const argv. 1035 void add(const Descriptor usage[], int argc, char** argv, int min_abbr_len = 0, // 1036 bool single_minus_longopt = false) 1037 { 1038 add(false, usage, argc, (const char**) argv, min_abbr_len, single_minus_longopt); 1039 } 1040 private: 1041 class CountOptionsAction; 1042 }; 1043 1044 /** 1045 * @brief Checks argument vectors for validity and parses them into data 1046 * structures that are easier to work with. 1047 * 1048 * @par Example: 1049 * @code 1050 * int main(int argc, char* argv[]) 1051 * { 1052 * argc-=(argc>0); argv+=(argc>0); // skip program name argv[0] if present 1053 * option::Stats stats(usage, argc, argv); 1054 * option::Option options[stats.options_max], buffer[stats.buffer_max]; 1055 * option::Parser parse(usage, argc, argv, options, buffer); 1056 * 1057 * if (parse.error()) 1058 * return 1; 1059 * 1060 * if (options[HELP]) 1061 * ... 1062 * @endcode 1063 */ 1064 class Parser 1065 { 1066 int op_count; //!< @internal @brief see optionsCount() 1067 int nonop_count; //!< @internal @brief see nonOptionsCount() 1068 const char** nonop_args; //!< @internal @brief see nonOptions() 1069 bool err; //!< @internal @brief see error() 1070 public: 1071 1072 /** 1073 * @brief Creates a new Parser. 1074 */ 1075 Parser() : 1076 op_count(0), nonop_count(0), nonop_args(0), err(false) 1077 { 1078 } 1079 1080 /** 1081 * @brief Creates a new Parser and immediately parses the given argument vector. 1082 * @copydetails parse() 1083 */ 1084 Parser(bool gnu, const Descriptor usage[], int argc, const char** argv, Option options[], Option buffer[], 1085 int min_abbr_len = 0, bool single_minus_longopt = false, int bufmax = -1) : 1086 op_count(0), nonop_count(0), nonop_args(0), err(false) 1087 { 1088 parse(gnu, usage, argc, argv, options, buffer, min_abbr_len, single_minus_longopt, bufmax); 1089 } 1090 1091 //! @brief Parser(...) with non-const argv. 1092 Parser(bool gnu, const Descriptor usage[], int argc, char** argv, Option options[], Option buffer[], 1093 int min_abbr_len = 0, bool single_minus_longopt = false, int bufmax = -1) : 1094 op_count(0), nonop_count(0), nonop_args(0), err(false) 1095 { 1096 parse(gnu, usage, argc, (const char**) argv, options, buffer, min_abbr_len, single_minus_longopt, bufmax); 1097 } 1098 1099 //! @brief POSIX Parser(...) (gnu==false). 1100 Parser(const Descriptor usage[], int argc, const char** argv, Option options[], Option buffer[], int min_abbr_len = 0, 1101 bool single_minus_longopt = false, int bufmax = -1) : 1102 op_count(0), nonop_count(0), nonop_args(0), err(false) 1103 { 1104 parse(false, usage, argc, argv, options, buffer, min_abbr_len, single_minus_longopt, bufmax); 1105 } 1106 1107 //! @brief POSIX Parser(...) (gnu==false) with non-const argv. 1108 Parser(const Descriptor usage[], int argc, char** argv, Option options[], Option buffer[], int min_abbr_len = 0, 1109 bool single_minus_longopt = false, int bufmax = -1) : 1110 op_count(0), nonop_count(0), nonop_args(0), err(false) 1111 { 1112 parse(false, usage, argc, (const char**) argv, options, buffer, min_abbr_len, single_minus_longopt, bufmax); 1113 } 1114 1115 /** 1116 * @brief Parses the given argument vector. 1117 * 1118 * @param gnu if true, parse() will not stop at the first non-option argument. Instead it will 1119 * reorder arguments so that all non-options are at the end. This is the default behaviour 1120 * of GNU getopt() but is not conforming to POSIX. @n 1121 * Note, that once the argument vector has been reordered, the @c gnu flag will have 1122 * no further effect on this argument vector. So it is enough to pass @c gnu==true when 1123 * creating Stats. 1124 * @param usage Array of Descriptor objects that describe the options to support. The last entry 1125 * of this array must have 0 in all fields. 1126 * @param argc The number of elements from @c argv that are to be parsed. If you pass -1, the number 1127 * will be determined automatically. In that case the @c argv list must end with a NULL 1128 * pointer. 1129 * @param argv The arguments to be parsed. If you pass -1 as @c argc the last pointer in the @c argv 1130 * list must be NULL to mark the end. 1131 * @param options Each entry is the first element of a linked list of Options. Each new option 1132 * that is parsed will be appended to the list specified by that Option's 1133 * Descriptor::index. If an entry is not yet used (i.e. the Option is invalid), 1134 * it will be replaced rather than appended to. @n 1135 * The minimum length of this array is the greatest Descriptor::index value that 1136 * occurs in @c usage @e PLUS ONE. 1137 * @param buffer Each argument that is successfully parsed (including unknown arguments, if they 1138 * have a Descriptor whose CheckArg does not return @ref ARG_ILLEGAL) will be stored in this 1139 * array. parse() scans the array for the first invalid entry and begins writing at that 1140 * index. You can pass @c bufmax to limit the number of options stored. 1141 * @param min_abbr_len Passing a value <code> min_abbr_len > 0 </code> enables abbreviated long 1142 * options. The parser will match a prefix of a long option as if it was 1143 * the full long option (e.g. @c --foob=10 will be interpreted as if it was 1144 * @c --foobar=10 ), as long as the prefix has at least @c min_abbr_len characters 1145 * (not counting the @c -- ) and is unambiguous. 1146 * @n Be careful if combining @c min_abbr_len=1 with @c single_minus_longopt=true 1147 * because the ambiguity check does not consider short options and abbreviated 1148 * single minus long options will take precedence over short options. 1149 * @param single_minus_longopt Passing @c true for this option allows long options to begin with 1150 * a single minus. The double minus form will still be recognized. Note that 1151 * single minus long options take precedence over short options and short option 1152 * groups. E.g. @c -file would be interpreted as @c --file and not as 1153 * <code> -f -i -l -e </code> (assuming a long option named @c "file" exists). 1154 * @param bufmax The greatest index in the @c buffer[] array that parse() will write to is 1155 * @c bufmax-1. If there are more options, they will be processed (in particular 1156 * their CheckArg will be called) but not stored. @n 1157 * If you used Stats::buffer_max to dimension this array, you can pass 1158 * -1 (or not pass @c bufmax at all) which tells parse() that the buffer is 1159 * "large enough". 1160 * @attention 1161 * Remember that @c options and @c buffer store Option @e objects, not pointers. Therefore it 1162 * is not possible for the same object to be in both arrays. For those options that are found in 1163 * both @c buffer[] and @c options[] the respective objects are independent copies. And only the 1164 * objects in @c options[] are properly linked via Option::next() and Option::prev(). 1165 * You can iterate over @c buffer[] to 1166 * process all options in the order they appear in the argument vector, but if you want access to 1167 * the other Options with the same Descriptor::index, then you @e must access the linked list via 1168 * @c options[]. You can get the linked list in options from a buffer object via something like 1169 * @c options[buffer[i].index()]. 1170 */ 1171 void parse(bool gnu, const Descriptor usage[], int argc, const char** argv, Option options[], Option buffer[], 1172 int min_abbr_len = 0, bool single_minus_longopt = false, int bufmax = -1); 1173 1174 //! @brief parse() with non-const argv. 1175 void parse(bool gnu, const Descriptor usage[], int argc, char** argv, Option options[], Option buffer[], 1176 int min_abbr_len = 0, bool single_minus_longopt = false, int bufmax = -1) 1177 { 1178 parse(gnu, usage, argc, (const char**) argv, options, buffer, min_abbr_len, single_minus_longopt, bufmax); 1179 } 1180 1181 //! @brief POSIX parse() (gnu==false). 1182 void parse(const Descriptor usage[], int argc, const char** argv, Option options[], Option buffer[], 1183 int min_abbr_len = 0, bool single_minus_longopt = false, int bufmax = -1) 1184 { 1185 parse(false, usage, argc, argv, options, buffer, min_abbr_len, single_minus_longopt, bufmax); 1186 } 1187 1188 //! @brief POSIX parse() (gnu==false) with non-const argv. 1189 void parse(const Descriptor usage[], int argc, char** argv, Option options[], Option buffer[], int min_abbr_len = 0, 1190 bool single_minus_longopt = false, int bufmax = -1) 1191 { 1192 parse(false, usage, argc, (const char**) argv, options, buffer, min_abbr_len, single_minus_longopt, bufmax); 1193 } 1194 1195 /** 1196 * @brief Returns the number of valid Option objects in @c buffer[]. 1197 * 1198 * @note 1199 * @li The returned value always reflects the number of Options in the buffer[] array used for 1200 * the most recent call to parse(). 1201 * @li The count (and the buffer[]) includes unknown options if they are collected 1202 * (see Descriptor::longopt). 1203 */ 1204 int optionsCount() 1205 { 1206 return op_count; 1207 } 1208 1209 /** 1210 * @brief Returns the number of non-option arguments that remained at the end of the 1211 * most recent parse() that actually encountered non-option arguments. 1212 * 1213 * @note 1214 * A parse() that does not encounter non-option arguments will leave this value 1215 * as well as nonOptions() undisturbed. This means you can feed the Parser a 1216 * default argument vector that contains non-option arguments (e.g. a default filename). 1217 * Then you feed it the actual arguments from the user. If the user has supplied at 1218 * least one non-option argument, all of the non-option arguments from the default 1219 * disappear and are replaced by the user's non-option arguments. However, if the 1220 * user does not supply any non-option arguments the defaults will still be in 1221 * effect. 1222 */ 1223 int nonOptionsCount() 1224 { 1225 return nonop_count; 1226 } 1227 1228 /** 1229 * @brief Returns a pointer to an array of non-option arguments (only valid 1230 * if <code>nonOptionsCount() >0 </code>). 1231 * 1232 * @note 1233 * @li parse() does not copy arguments, so this pointer points into the actual argument 1234 * vector as passed to parse(). 1235 * @li As explained at nonOptionsCount() this pointer is only changed by parse() calls 1236 * that actually encounter non-option arguments. A parse() call that encounters only 1237 * options, will not change nonOptions(). 1238 */ 1239 const char** nonOptions() 1240 { 1241 return nonop_args; 1242 } 1243 1244 /** 1245 * @brief Returns <b><code>nonOptions()[i]</code></b> (@e without checking if i is in range!). 1246 */ 1247 const char* nonOption(int i) 1248 { 1249 return nonOptions()[i]; 1250 } 1251 1252 /** 1253 * @brief Returns @c true if an unrecoverable error occurred while parsing options. 1254 * 1255 * An illegal argument to an option (i.e. CheckArg returns @ref ARG_ILLEGAL) is an 1256 * unrecoverable error that aborts the parse. Unknown options are only an error if 1257 * their CheckArg function returns @ref ARG_ILLEGAL. Otherwise they are collected. 1258 * In that case if you want to exit the program if either an illegal argument 1259 * or an unknown option has been passed, use code like this 1260 * 1261 * @code 1262 * if (parser.error() || options[UNKNOWN]) 1263 * exit(1); 1264 * @endcode 1265 * 1266 */ 1267 bool error() 1268 { 1269 return err; 1270 } 1271 1272 private: 1273 friend struct Stats; 1274 class StoreOptionAction; 1275 struct Action; 1276 1277 /** 1278 * @internal 1279 * @brief This is the core function that does all the parsing. 1280 * @retval false iff an unrecoverable error occurred. 1281 */ 1282 static bool workhorse(bool gnu, const Descriptor usage[], int numargs, const char** args, Action& action, 1283 bool single_minus_longopt, bool print_errors, int min_abbr_len); 1284 1285 /** 1286 * @internal 1287 * @brief Returns true iff @c st1 is a prefix of @c st2 and 1288 * in case @c st2 is longer than @c st1, then 1289 * the first additional character is '='. 1290 * 1291 * @par Examples: 1292 * @code 1293 * streq("foo", "foo=bar") == true 1294 * streq("foo", "foobar") == false 1295 * streq("foo", "foo") == true 1296 * streq("foo=bar", "foo") == false 1297 * @endcode 1298 */ 1299 static bool streq(const char* st1, const char* st2) 1300 { 1301 while (*st1 != 0) 1302 if (*st1++ != *st2++) 1303 return false; 1304 return (*st2 == 0 || *st2 == '='); 1305 } 1306 1307 /** 1308 * @internal 1309 * @brief Like streq() but handles abbreviations. 1310 * 1311 * Returns true iff @c st1 and @c st2 have a common 1312 * prefix with the following properties: 1313 * @li (if min > 0) its length is at least @c min characters or the same length as @c st1 (whichever is smaller). 1314 * @li (if min <= 0) its length is the same as that of @c st1 1315 * @li within @c st2 the character following the common prefix is either '=' or end-of-string. 1316 * 1317 * Examples: 1318 * @code 1319 * streqabbr("foo", "foo=bar",<anything>) == true 1320 * streqabbr("foo", "fo=bar" , 2) == true 1321 * streqabbr("foo", "fo" , 2) == true 1322 * streqabbr("foo", "fo" , 0) == false 1323 * streqabbr("foo", "f=bar" , 2) == false 1324 * streqabbr("foo", "f" , 2) == false 1325 * streqabbr("fo" , "foo=bar",<anything>) == false 1326 * streqabbr("foo", "foobar" ,<anything>) == false 1327 * streqabbr("foo", "fobar" ,<anything>) == false 1328 * streqabbr("foo", "foo" ,<anything>) == true 1329 * @endcode 1330 */ 1331 static bool streqabbr(const char* st1, const char* st2, long long min) 1332 { 1333 const char* st1start = st1; 1334 while (*st1 != 0 && (*st1 == *st2)) 1335 { 1336 ++st1; 1337 ++st2; 1338 } 1339 1340 return (*st1 == 0 || (min > 0 && (st1 - st1start) >= min)) && (*st2 == 0 || *st2 == '='); 1341 } 1342 1343 /** 1344 * @internal 1345 * @brief Returns true iff character @c ch is contained in the string @c st. 1346 * 1347 * Returns @c true for @c ch==0 . 1348 */ 1349 static bool instr(char ch, const char* st) 1350 { 1351 while (*st != 0 && *st != ch) 1352 ++st; 1353 return *st == ch; 1354 } 1355 1356 /** 1357 * @internal 1358 * @brief Rotates <code>args[-count],...,args[-1],args[0]</code> to become 1359 * <code>args[0],args[-count],...,args[-1]</code>. 1360 */ 1361 static void shift(const char** args, int count) 1362 { 1363 for (int i = 0; i > -count; --i) 1364 { 1365 const char* temp = args[i]; 1366 args[i] = args[i - 1]; 1367 args[i - 1] = temp; 1368 } 1369 } 1370 }; 1371 1372 /** 1373 * @internal 1374 * @brief Interface for actions Parser::workhorse() should perform for each Option it 1375 * parses. 1376 */ 1377 struct Parser::Action 1378 { 1379 /** 1380 * @brief Called by Parser::workhorse() for each Option that has been successfully 1381 * parsed (including unknown 1382 * options if they have a Descriptor whose Descriptor::check_arg does not return 1383 * @ref ARG_ILLEGAL. 1384 * 1385 * Returns @c false iff a fatal error has occured and the parse should be aborted. 1386 */ 1387 virtual bool perform(Option&) 1388 { 1389 return true; 1390 } 1391 1392 /** 1393 * @brief Called by Parser::workhorse() after finishing the parse. 1394 * @param numargs the number of non-option arguments remaining 1395 * @param args pointer to the first remaining non-option argument (if numargs > 0). 1396 * 1397 * @return 1398 * @c false iff a fatal error has occurred. 1399 */ 1400 virtual bool finished(int numargs, const char** args) 1401 { 1402 (void) numargs; 1403 (void) args; 1404 return true; 1405 } 1406 }; 1407 1408 /** 1409 * @internal 1410 * @brief An Action to pass to Parser::workhorse() that will increment a counter for 1411 * each parsed Option. 1412 */ 1413 class Stats::CountOptionsAction: public Parser::Action 1414 { 1415 unsigned* buffer_max; 1416 public: 1417 /** 1418 * Creates a new CountOptionsAction that will increase @c *buffer_max_ for each 1419 * parsed Option. 1420 */ 1421 CountOptionsAction(unsigned* buffer_max_) : 1422 buffer_max(buffer_max_) 1423 { 1424 } 1425 1426 bool perform(Option&) 1427 { 1428 if (*buffer_max == 0x7fffffff) 1429 return false; // overflow protection: don't accept number of options that doesn't fit signed int 1430 ++*buffer_max; 1431 return true; 1432 } 1433 }; 1434 1435 /** 1436 * @internal 1437 * @brief An Action to pass to Parser::workhorse() that will store each parsed Option in 1438 * appropriate arrays (see Parser::parse()). 1439 */ 1440 class Parser::StoreOptionAction: public Parser::Action 1441 { 1442 Parser& parser; 1443 Option* options; 1444 Option* buffer; 1445 int bufmax; //! Number of slots in @c buffer. @c -1 means "large enough". 1446 public: 1447 /** 1448 * @brief Creates a new StoreOption action. 1449 * @param parser_ the parser whose op_count should be updated. 1450 * @param options_ each Option @c o is chained into the linked list @c options_[o.desc->index] 1451 * @param buffer_ each Option is appended to this array as long as there's a free slot. 1452 * @param bufmax_ number of slots in @c buffer_. @c -1 means "large enough". 1453 */ 1454 StoreOptionAction(Parser& parser_, Option options_[], Option buffer_[], int bufmax_) : 1455 parser(parser_), options(options_), buffer(buffer_), bufmax(bufmax_) 1456 { 1457 // find first empty slot in buffer (if any) 1458 int bufidx = 0; 1459 while ((bufmax < 0 || bufidx < bufmax) && buffer[bufidx]) 1460 ++bufidx; 1461 1462 // set parser's optionCount 1463 parser.op_count = bufidx; 1464 } 1465 1466 bool perform(Option& option) 1467 { 1468 if (bufmax < 0 || parser.op_count < bufmax) 1469 { 1470 if (parser.op_count == 0x7fffffff) 1471 return false; // overflow protection: don't accept number of options that doesn't fit signed int 1472 1473 buffer[parser.op_count] = option; 1474 int idx = buffer[parser.op_count].desc->index; 1475 if (options[idx]) 1476 options[idx].append(buffer[parser.op_count]); 1477 else 1478 options[idx] = buffer[parser.op_count]; 1479 ++parser.op_count; 1480 } 1481 return true; // NOTE: an option that is discarded because of a full buffer is not fatal 1482 } 1483 1484 bool finished(int numargs, const char** args) 1485 { 1486 // only overwrite non-option argument list if there's at least 1 1487 // new non-option argument. Otherwise we keep the old list. This 1488 // makes it easy to use default non-option arguments. 1489 if (numargs > 0) 1490 { 1491 parser.nonop_count = numargs; 1492 parser.nonop_args = args; 1493 } 1494 1495 return true; 1496 } 1497 }; 1498 1499 inline void Parser::parse(bool gnu, const Descriptor usage[], int argc, const char** argv, Option options[], 1500 Option buffer[], int min_abbr_len, bool single_minus_longopt, int bufmax) 1501 { 1502 StoreOptionAction action(*this, options, buffer, bufmax); 1503 err = !workhorse(gnu, usage, argc, argv, action, single_minus_longopt, true, min_abbr_len); 1504 } 1505 1506 inline void Stats::add(bool gnu, const Descriptor usage[], int argc, const char** argv, int min_abbr_len, 1507 bool single_minus_longopt) 1508 { 1509 // determine size of options array. This is the greatest index used in the usage + 1 1510 int i = 0; 1511 while (usage[i].shortopt != 0) 1512 { 1513 if (usage[i].index + 1 >= options_max) 1514 options_max = (usage[i].index + 1) + 1; // 1 more than necessary as sentinel 1515 1516 ++i; 1517 } 1518 1519 CountOptionsAction action(&buffer_max); 1520 Parser::workhorse(gnu, usage, argc, argv, action, single_minus_longopt, false, min_abbr_len); 1521 } 1522 1523 inline bool Parser::workhorse(bool gnu, const Descriptor usage[], int numargs, const char** args, Action& action, 1524 bool single_minus_longopt, bool print_errors, int min_abbr_len) 1525 { 1526 // protect against NULL pointer 1527 if (args == 0) 1528 numargs = 0; 1529 1530 int nonops = 0; 1531 1532 while (numargs != 0 && *args != 0) 1533 { 1534 const char* param = *args; // param can be --long-option, -srto or non-option argument 1535 1536 // in POSIX mode the first non-option argument terminates the option list 1537 // a lone minus character is a non-option argument 1538 if (param[0] != '-' || param[1] == 0) 1539 { 1540 if (gnu) 1541 { 1542 ++nonops; 1543 ++args; 1544 if (numargs > 0) 1545 --numargs; 1546 continue; 1547 } 1548 else 1549 break; 1550 } 1551 1552 // -- terminates the option list. The -- itself is skipped. 1553 if (param[1] == '-' && param[2] == 0) 1554 { 1555 shift(args, nonops); 1556 ++args; 1557 if (numargs > 0) 1558 --numargs; 1559 break; 1560 } 1561 1562 bool handle_short_options; 1563 const char* longopt_name; 1564 if (param[1] == '-') // if --long-option 1565 { 1566 handle_short_options = false; 1567 longopt_name = param + 2; 1568 } 1569 else 1570 { 1571 handle_short_options = true; 1572 longopt_name = param + 1; //for testing a potential -long-option 1573 } 1574 1575 bool try_single_minus_longopt = single_minus_longopt; 1576 bool have_more_args = (numargs > 1 || numargs < 0); // is referencing argv[1] valid? 1577 1578 do // loop over short options in group, for long options the body is executed only once 1579 { 1580 int idx; 1581 1582 const char* optarg; 1583 1584 /******************** long option **********************/ 1585 if (handle_short_options == false || try_single_minus_longopt) 1586 { 1587 idx = 0; 1588 while (usage[idx].longopt != 0 && !streq(usage[idx].longopt, longopt_name)) 1589 ++idx; 1590 1591 if (usage[idx].longopt == 0 && min_abbr_len > 0) // if we should try to match abbreviated long options 1592 { 1593 int i1 = 0; 1594 while (usage[i1].longopt != 0 && !streqabbr(usage[i1].longopt, longopt_name, min_abbr_len)) 1595 ++i1; 1596 if (usage[i1].longopt != 0) 1597 { // now test if the match is unambiguous by checking for another match 1598 int i2 = i1 + 1; 1599 while (usage[i2].longopt != 0 && !streqabbr(usage[i2].longopt, longopt_name, min_abbr_len)) 1600 ++i2; 1601 1602 if (usage[i2].longopt == 0) // if there was no second match it's unambiguous, so accept i1 as idx 1603 idx = i1; 1604 } 1605 } 1606 1607 // if we found something, disable handle_short_options (only relevant if single_minus_longopt) 1608 if (usage[idx].longopt != 0) 1609 handle_short_options = false; 1610 1611 try_single_minus_longopt = false; // prevent looking for longopt in the middle of shortopt group 1612 1613 optarg = longopt_name; 1614 while (*optarg != 0 && *optarg != '=') 1615 ++optarg; 1616 if (*optarg == '=') // attached argument 1617 ++optarg; 1618 else 1619 // possibly detached argument 1620 optarg = (have_more_args ? args[1] : 0); 1621 } 1622 1623 /************************ short option ***********************************/ 1624 if (handle_short_options) 1625 { 1626 if (*++param == 0) // point at the 1st/next option character 1627 break; // end of short option group 1628 1629 idx = 0; 1630 while (usage[idx].shortopt != 0 && !instr(*param, usage[idx].shortopt)) 1631 ++idx; 1632 1633 if (param[1] == 0) // if the potential argument is separate 1634 optarg = (have_more_args ? args[1] : 0); 1635 else 1636 // if the potential argument is attached 1637 optarg = param + 1; 1638 } 1639 1640 const Descriptor* descriptor = &usage[idx]; 1641 1642 if (descriptor->shortopt == 0) /************** unknown option ********************/ 1643 { 1644 // look for dummy entry (shortopt == "" and longopt == "") to use as Descriptor for unknown options 1645 idx = 0; 1646 while (usage[idx].shortopt != 0 && (usage[idx].shortopt[0] != 0 || usage[idx].longopt[0] != 0)) 1647 ++idx; 1648 descriptor = (usage[idx].shortopt == 0 ? 0 : &usage[idx]); 1649 } 1650 1651 if (descriptor != 0) 1652 { 1653 Option option(descriptor, param, optarg); 1654 switch (descriptor->check_arg(option, print_errors)) 1655 { 1656 case ARG_ILLEGAL: 1657 return false; // fatal 1658 case ARG_OK: 1659 // skip one element of the argument vector, if it's a separated argument 1660 if (optarg != 0 && have_more_args && optarg == args[1]) 1661 { 1662 shift(args, nonops); 1663 if (numargs > 0) 1664 --numargs; 1665 ++args; 1666 } 1667 1668 // No further short options are possible after an argument 1669 handle_short_options = false; 1670 1671 break; 1672 case ARG_IGNORE: 1673 case ARG_NONE: 1674 option.arg = 0; 1675 break; 1676 } 1677 1678 if (!action.perform(option)) 1679 return false; 1680 } 1681 1682 } while (handle_short_options); 1683 1684 shift(args, nonops); 1685 ++args; 1686 if (numargs > 0) 1687 --numargs; 1688 1689 } // while 1690 1691 if (numargs > 0 && *args == 0) // It's a bug in the caller if numargs is greater than the actual number 1692 numargs = 0; // of arguments, but as a service to the user we fix this if we spot it. 1693 1694 if (numargs < 0) // if we don't know the number of remaining non-option arguments 1695 { // we need to count them 1696 numargs = 0; 1697 while (args[numargs] != 0) 1698 ++numargs; 1699 } 1700 1701 return action.finished(numargs + nonops, args - nonops); 1702 } 1703 1704 /** 1705 * @internal 1706 * @brief The implementation of option::printUsage(). 1707 */ 1708 struct PrintUsageImplementation 1709 { 1710 /** 1711 * @internal 1712 * @brief Interface for Functors that write (part of) a string somewhere. 1713 */ 1714 struct IStringWriter 1715 { 1716 /** 1717 * @brief Writes the given number of chars beginning at the given pointer somewhere. 1718 */ 1719 virtual void operator()(const char*, int) 1720 { 1721 } 1722 }; 1723 1724 /** 1725 * @internal 1726 * @brief Encapsulates a function with signature <code>func(string, size)</code> where 1727 * string can be initialized with a const char* and size with an int. 1728 */ 1729 template<typename Function> 1730 struct FunctionWriter: public IStringWriter 1731 { 1732 Function* write; 1733 1734 virtual void operator()(const char* str, int size) 1735 { 1736 (*write)(str, size); 1737 } 1738 1739 FunctionWriter(Function* w) : 1740 write(w) 1741 { 1742 } 1743 }; 1744 1745 /** 1746 * @internal 1747 * @brief Encapsulates a reference to an object with a <code>write(string, size)</code> 1748 * method like that of @c std::ostream. 1749 */ 1750 template<typename OStream> 1751 struct OStreamWriter: public IStringWriter 1752 { 1753 OStream& ostream; 1754 1755 virtual void operator()(const char* str, int size) 1756 { 1757 ostream.write(str, size); 1758 } 1759 1760 OStreamWriter(OStream& o) : 1761 ostream(o) 1762 { 1763 } 1764 }; 1765 1766 /** 1767 * @internal 1768 * @brief Like OStreamWriter but encapsulates a @c const reference, which is 1769 * typically a temporary object of a user class. 1770 */ 1771 template<typename Temporary> 1772 struct TemporaryWriter: public IStringWriter 1773 { 1774 const Temporary& userstream; 1775 1776 virtual void operator()(const char* str, int size) 1777 { 1778 userstream.write(str, size); 1779 } 1780 1781 TemporaryWriter(const Temporary& u) : 1782 userstream(u) 1783 { 1784 } 1785 }; 1786 1787 /** 1788 * @internal 1789 * @brief Encapsulates a function with the signature <code>func(fd, string, size)</code> (the 1790 * signature of the @c write() system call) 1791 * where fd can be initialized from an int, string from a const char* and size from an int. 1792 */ 1793 template<typename Syscall> 1794 struct SyscallWriter: public IStringWriter 1795 { 1796 Syscall* write; 1797 int fd; 1798 1799 virtual void operator()(const char* str, int size) 1800 { 1801 (*write)(fd, str, size); 1802 } 1803 1804 SyscallWriter(Syscall* w, int f) : 1805 write(w), fd(f) 1806 { 1807 } 1808 }; 1809 1810 /** 1811 * @internal 1812 * @brief Encapsulates a function with the same signature as @c std::fwrite(). 1813 */ 1814 template<typename Function, typename Stream> 1815 struct StreamWriter: public IStringWriter 1816 { 1817 Function* fwrite; 1818 Stream* stream; 1819 1820 virtual void operator()(const char* str, int size) 1821 { 1822 (*fwrite)(str, size, 1, stream); 1823 } 1824 1825 StreamWriter(Function* w, Stream* s) : 1826 fwrite(w), stream(s) 1827 { 1828 } 1829 }; 1830 1831 /** 1832 * @internal 1833 * @brief Sets <code> i1 = max(i1, i2) </code> 1834 */ 1835 static void upmax(int& i1, int i2) 1836 { 1837 i1 = (i1 >= i2 ? i1 : i2); 1838 } 1839 1840 /** 1841 * @internal 1842 * @brief Moves the "cursor" to column @c want_x assuming it is currently at column @c x 1843 * and sets @c x=want_x . 1844 * If <code> x > want_x </code>, a line break is output before indenting. 1845 * 1846 * @param write Spaces and possibly a line break are written via this functor to get 1847 * the desired indentation @c want_x . 1848 * @param[in,out] x the current indentation. Set to @c want_x by this method. 1849 * @param want_x the desired indentation. 1850 */ 1851 static void indent(IStringWriter& write, int& x, int want_x) 1852 { 1853 int indent = want_x - x; 1854 if (indent < 0) 1855 { 1856 write("\n", 1); 1857 indent = want_x; 1858 } 1859 1860 if (indent > 0) 1861 { 1862 char space = ' '; 1863 for (int i = 0; i < indent; ++i) 1864 write(&space, 1); 1865 x = want_x; 1866 } 1867 } 1868 1869 /** 1870 * @brief Returns true if ch is the unicode code point of a wide character. 1871 * 1872 * @note 1873 * The following character ranges are treated as wide 1874 * @code 1875 * 1100..115F 1876 * 2329..232A (just 2 characters!) 1877 * 2E80..A4C6 except for 303F 1878 * A960..A97C 1879 * AC00..D7FB 1880 * F900..FAFF 1881 * FE10..FE6B 1882 * FF01..FF60 1883 * FFE0..FFE6 1884 * 1B000...... 1885 * @endcode 1886 */ 1887 static bool isWideChar(unsigned ch) 1888 { 1889 if (ch == 0x303F) 1890 return false; 1891 1892 return ((0x1100 <= ch && ch <= 0x115F) || (0x2329 <= ch && ch <= 0x232A) || (0x2E80 <= ch && ch <= 0xA4C6) 1893 || (0xA960 <= ch && ch <= 0xA97C) || (0xAC00 <= ch && ch <= 0xD7FB) || (0xF900 <= ch && ch <= 0xFAFF) 1894 || (0xFE10 <= ch && ch <= 0xFE6B) || (0xFF01 <= ch && ch <= 0xFF60) || (0xFFE0 <= ch && ch <= 0xFFE6) 1895 || (0x1B000 <= ch)); 1896 } 1897 1898 /** 1899 * @internal 1900 * @brief Splits a @c Descriptor[] array into tables, rows, lines and columns and 1901 * iterates over these components. 1902 * 1903 * The top-level organizational unit is the @e table. 1904 * A table begins at a Descriptor with @c help!=NULL and extends up to 1905 * a Descriptor with @c help==NULL. 1906 * 1907 * A table consists of @e rows. Due to line-wrapping and explicit breaks 1908 * a row may take multiple lines on screen. Rows within the table are separated 1909 * by \\n. They never cross Descriptor boundaries. This means a row ends either 1910 * at \\n or the 0 at the end of the help string. 1911 * 1912 * A row consists of columns/cells. Columns/cells within a row are separated by \\t. 1913 * Line breaks within a cell are marked by \\v. 1914 * 1915 * Rows in the same table need not have the same number of columns/cells. The 1916 * extreme case are interjections, which are rows that contain neither \\t nor \\v. 1917 * These are NOT treated specially by LinePartIterator, but they are treated 1918 * specially by printUsage(). 1919 * 1920 * LinePartIterator iterates through the usage at 3 levels: table, row and part. 1921 * Tables and rows are as described above. A @e part is a line within a cell. 1922 * LinePartIterator iterates through 1st parts of all cells, then through the 2nd 1923 * parts of all cells (if any),... @n 1924 * Example: The row <code> "1 \v 3 \t 2 \v 4" </code> has 2 cells/columns and 4 parts. 1925 * The parts will be returned in the order 1, 2, 3, 4. 1926 * 1927 * It is possible that some cells have fewer parts than others. In this case 1928 * LinePartIterator will "fill up" these cells with 0-length parts. IOW, LinePartIterator 1929 * always returns the same number of parts for each column. Note that this is different 1930 * from the way rows and columns are handled. LinePartIterator does @e not guarantee that 1931 * the same number of columns will be returned for each row. 1932 * 1933 */ 1934 class LinePartIterator 1935 { 1936 const Descriptor* tablestart; //!< The 1st descriptor of the current table. 1937 const Descriptor* rowdesc; //!< The Descriptor that contains the current row. 1938 const char* rowstart; //!< Ptr to 1st character of current row within rowdesc->help. 1939 const char* ptr; //!< Ptr to current part within the current row. 1940 int col; //!< Index of current column. 1941 int len; //!< Length of the current part (that ptr points at) in BYTES 1942 int screenlen; //!< Length of the current part in screen columns (taking narrow/wide chars into account). 1943 int max_line_in_block; //!< Greatest index of a line within the block. This is the number of \\v within the cell with the most \\vs. 1944 int line_in_block; //!< Line index within the current cell of the current part. 1945 int target_line_in_block; //!< Line index of the parts we should return to the user on this iteration. 1946 bool hit_target_line; //!< Flag whether we encountered a part with line index target_line_in_block in the current cell. 1947 1948 /** 1949 * @brief Determines the byte and character lengths of the part at @ref ptr and 1950 * stores them in @ref len and @ref screenlen respectively. 1951 */ 1952 void update_length() 1953 { 1954 screenlen = 0; 1955 for (len = 0; ptr[len] != 0 && ptr[len] != '\v' && ptr[len] != '\t' && ptr[len] != '\n'; ++len) 1956 { 1957 ++screenlen; 1958 unsigned ch = (unsigned char) ptr[len]; 1959 if (ch > 0xC1) // everything <= 0xC1 (yes, even 0xC1 itself) is not a valid UTF-8 start byte 1960 { 1961 // int __builtin_clz (unsigned int x) 1962 // Returns the number of leading 0-bits in x, starting at the most significant bit 1963 unsigned mask = (unsigned) -1 >> __builtin_clz(ch ^ 0xff); 1964 ch = ch & mask; // mask out length bits, we don't verify their correctness 1965 while (((unsigned char) ptr[len + 1] ^ 0x80) <= 0x3F) // while next byte is continuation byte 1966 { 1967 ch = (ch << 6) ^ (unsigned char) ptr[len + 1] ^ 0x80; // add continuation to char code 1968 ++len; 1969 } 1970 // ch is the decoded unicode code point 1971 if (ch >= 0x1100 && isWideChar(ch)) // the test for 0x1100 is here to avoid the function call in the Latin case 1972 ++screenlen; 1973 } 1974 } 1975 } 1976 1977 public: 1978 //! @brief Creates an iterator for @c usage. 1979 LinePartIterator(const Descriptor usage[]) : 1980 tablestart(usage), rowdesc(0), rowstart(0), ptr(0), col(-1), len(0), max_line_in_block(0), line_in_block(0), 1981 target_line_in_block(0), hit_target_line(true) 1982 { 1983 } 1984 1985 /** 1986 * @brief Moves iteration to the next table (if any). Has to be called once on a new 1987 * LinePartIterator to move to the 1st table. 1988 * @retval false if moving to next table failed because no further table exists. 1989 */ 1990 bool nextTable() 1991 { 1992 // If this is NOT the first time nextTable() is called after the constructor, 1993 // then skip to the next table break (i.e. a Descriptor with help == 0) 1994 if (rowdesc != 0) 1995 { 1996 while (tablestart->help != 0 && tablestart->shortopt != 0) 1997 ++tablestart; 1998 } 1999 2000 // Find the next table after the break (if any) 2001 while (tablestart->help == 0 && tablestart->shortopt != 0) 2002 ++tablestart; 2003 2004 restartTable(); 2005 return rowstart != 0; 2006 } 2007 2008 /** 2009 * @brief Reset iteration to the beginning of the current table. 2010 */ 2011 void restartTable() 2012 { 2013 rowdesc = tablestart; 2014 rowstart = tablestart->help; 2015 ptr = 0; 2016 } 2017 2018 /** 2019 * @brief Moves iteration to the next row (if any). Has to be called once after each call to 2020 * @ref nextTable() to move to the 1st row of the table. 2021 * @retval false if moving to next row failed because no further row exists. 2022 */ 2023 bool nextRow() 2024 { 2025 if (ptr == 0) 2026 { 2027 restartRow(); 2028 return rowstart != 0; 2029 } 2030 2031 while (*ptr != 0 && *ptr != '\n') 2032 ++ptr; 2033 2034 if (*ptr == 0) 2035 { 2036 if ((rowdesc + 1)->help == 0) // table break 2037 return false; 2038 2039 ++rowdesc; 2040 rowstart = rowdesc->help; 2041 } 2042 else // if (*ptr == '\n') 2043 { 2044 rowstart = ptr + 1; 2045 } 2046 2047 restartRow(); 2048 return true; 2049 } 2050 2051 /** 2052 * @brief Reset iteration to the beginning of the current row. 2053 */ 2054 void restartRow() 2055 { 2056 ptr = rowstart; 2057 col = -1; 2058 len = 0; 2059 screenlen = 0; 2060 max_line_in_block = 0; 2061 line_in_block = 0; 2062 target_line_in_block = 0; 2063 hit_target_line = true; 2064 } 2065 2066 /** 2067 * @brief Moves iteration to the next part (if any). Has to be called once after each call to 2068 * @ref nextRow() to move to the 1st part of the row. 2069 * @retval false if moving to next part failed because no further part exists. 2070 * 2071 * See @ref LinePartIterator for details about the iteration. 2072 */ 2073 bool next() 2074 { 2075 if (ptr == 0) 2076 return false; 2077 2078 if (col == -1) 2079 { 2080 col = 0; 2081 update_length(); 2082 return true; 2083 } 2084 2085 ptr += len; 2086 while (true) 2087 { 2088 switch (*ptr) 2089 { 2090 case '\v': 2091 upmax(max_line_in_block, ++line_in_block); 2092 ++ptr; 2093 break; 2094 case '\t': 2095 if (!hit_target_line) // if previous column did not have the targetline 2096 { // then "insert" a 0-length part 2097 update_length(); 2098 hit_target_line = true; 2099 return true; 2100 } 2101 2102 hit_target_line = false; 2103 line_in_block = 0; 2104 ++col; 2105 ++ptr; 2106 break; 2107 case 0: 2108 case '\n': 2109 if (!hit_target_line) // if previous column did not have the targetline 2110 { // then "insert" a 0-length part 2111 update_length(); 2112 hit_target_line = true; 2113 return true; 2114 } 2115 2116 if (++target_line_in_block > max_line_in_block) 2117 { 2118 update_length(); 2119 return false; 2120 } 2121 2122 hit_target_line = false; 2123 line_in_block = 0; 2124 col = 0; 2125 ptr = rowstart; 2126 continue; 2127 default: 2128 ++ptr; 2129 continue; 2130 } // switch 2131 2132 if (line_in_block == target_line_in_block) 2133 { 2134 update_length(); 2135 hit_target_line = true; 2136 return true; 2137 } 2138 } // while 2139 } 2140 2141 /** 2142 * @brief Returns the index (counting from 0) of the column in which 2143 * the part pointed to by @ref data() is located. 2144 */ 2145 int column() 2146 { 2147 return col; 2148 } 2149 2150 /** 2151 * @brief Returns the index (counting from 0) of the line within the current column 2152 * this part belongs to. 2153 */ 2154 int line() 2155 { 2156 return target_line_in_block; // NOT line_in_block !!! It would be wrong if !hit_target_line 2157 } 2158 2159 /** 2160 * @brief Returns the length of the part pointed to by @ref data() in raw chars (not UTF-8 characters). 2161 */ 2162 int length() 2163 { 2164 return len; 2165 } 2166 2167 /** 2168 * @brief Returns the width in screen columns of the part pointed to by @ref data(). 2169 * Takes multi-byte UTF-8 sequences and wide characters into account. 2170 */ 2171 int screenLength() 2172 { 2173 return screenlen; 2174 } 2175 2176 /** 2177 * @brief Returns the current part of the iteration. 2178 */ 2179 const char* data() 2180 { 2181 return ptr; 2182 } 2183 }; 2184 2185 /** 2186 * @internal 2187 * @brief Takes input and line wraps it, writing out one line at a time so that 2188 * it can be interleaved with output from other columns. 2189 * 2190 * The LineWrapper is used to handle the last column of each table as well as interjections. 2191 * The LineWrapper is called once for each line of output. If the data given to it fits 2192 * into the designated width of the last column it is simply written out. If there 2193 * is too much data, an appropriate split point is located and only the data up to this 2194 * split point is written out. The rest of the data is queued for the next line. 2195 * That way the last column can be line wrapped and interleaved with data from 2196 * other columns. The following example makes this clearer: 2197 * @code 2198 * Column 1,1 Column 2,1 This is a long text 2199 * Column 1,2 Column 2,2 that does not fit into 2200 * a single line. 2201 * @endcode 2202 * 2203 * The difficulty in producing this output is that the whole string 2204 * "This is a long text that does not fit into a single line" is the 2205 * 1st and only part of column 3. In order to produce the above 2206 * output the string must be output piecemeal, interleaved with 2207 * the data from the other columns. 2208 */ 2209 class LineWrapper 2210 { 2211 static const int bufmask = 15; //!< Must be a power of 2 minus 1. 2212 /** 2213 * @brief Ring buffer for length component of pair (data, length). 2214 */ 2215 int lenbuf[bufmask + 1]; 2216 /** 2217 * @brief Ring buffer for data component of pair (data, length). 2218 */ 2219 const char* datbuf[bufmask + 1]; 2220 /** 2221 * @brief The indentation of the column to which the LineBuffer outputs. LineBuffer 2222 * assumes that the indentation has already been written when @ref process() 2223 * is called, so this value is only used when a buffer flush requires writing 2224 * additional lines of output. 2225 */ 2226 int x; 2227 /** 2228 * @brief The width of the column to line wrap. 2229 */ 2230 int width; 2231 int head; //!< @brief index for next write 2232 int tail; //!< @brief index for next read - 1 (i.e. increment tail BEFORE read) 2233 2234 /** 2235 * @brief Multiple methods of LineWrapper may decide to flush part of the buffer to 2236 * free up space. The contract of process() says that only 1 line is output. So 2237 * this variable is used to track whether something has output a line. It is 2238 * reset at the beginning of process() and checked at the end to decide if 2239 * output has already occurred or is still needed. 2240 */ 2241 bool wrote_something; 2242 2243 bool buf_empty() 2244 { 2245 return ((tail + 1) & bufmask) == head; 2246 } 2247 2248 bool buf_full() 2249 { 2250 return tail == head; 2251 } 2252 2253 void buf_store(const char* data, int len) 2254 { 2255 lenbuf[head] = len; 2256 datbuf[head] = data; 2257 head = (head + 1) & bufmask; 2258 } 2259 2260 //! @brief Call BEFORE reading ...buf[tail]. 2261 void buf_next() 2262 { 2263 tail = (tail + 1) & bufmask; 2264 } 2265 2266 /** 2267 * @brief Writes (data,len) into the ring buffer. If the buffer is full, a single line 2268 * is flushed out of the buffer into @c write. 2269 */ 2270 void output(IStringWriter& write, const char* data, int len) 2271 { 2272 if (buf_full()) 2273 write_one_line(write); 2274 2275 buf_store(data, len); 2276 } 2277 2278 /** 2279 * @brief Writes a single line of output from the buffer to @c write. 2280 */ 2281 void write_one_line(IStringWriter& write) 2282 { 2283 if (wrote_something) // if we already wrote something, we need to start a new line 2284 { 2285 write("\n", 1); 2286 int _ = 0; 2287 indent(write, _, x); 2288 } 2289 2290 if (!buf_empty()) 2291 { 2292 buf_next(); 2293 write(datbuf[tail], lenbuf[tail]); 2294 } 2295 2296 wrote_something = true; 2297 } 2298 public: 2299 2300 /** 2301 * @brief Writes out all remaining data from the LineWrapper using @c write. 2302 * Unlike @ref process() this method indents all lines including the first and 2303 * will output a \\n at the end (but only if something has been written). 2304 */ 2305 void flush(IStringWriter& write) 2306 { 2307 if (buf_empty()) 2308 return; 2309 int _ = 0; 2310 indent(write, _, x); 2311 wrote_something = false; 2312 while (!buf_empty()) 2313 write_one_line(write); 2314 write("\n", 1); 2315 } 2316 2317 /** 2318 * @brief Process, wrap and output the next piece of data. 2319 * 2320 * process() will output at least one line of output. This is not necessarily 2321 * the @c data passed in. It may be data queued from a prior call to process(). 2322 * If the internal buffer is full, more than 1 line will be output. 2323 * 2324 * process() assumes that the a proper amount of indentation has already been 2325 * output. It won't write any further indentation before the 1st line. If 2326 * more than 1 line is written due to buffer constraints, the lines following 2327 * the first will be indented by this method, though. 2328 * 2329 * No \\n is written by this method after the last line that is written. 2330 * 2331 * @param write where to write the data. 2332 * @param data the new chunk of data to write. 2333 * @param len the length of the chunk of data to write. 2334 */ 2335 void process(IStringWriter& write, const char* data, int len) 2336 { 2337 wrote_something = false; 2338 2339 while (len > 0) 2340 { 2341 if (len <= width) // quick test that works because utf8width <= len (all wide chars have at least 2 bytes) 2342 { 2343 output(write, data, len); 2344 len = 0; 2345 } 2346 else // if (len > width) it's possible (but not guaranteed) that utf8len > width 2347 { 2348 int utf8width = 0; 2349 int maxi = 0; 2350 while (maxi < len && utf8width < width) 2351 { 2352 int charbytes = 1; 2353 unsigned ch = (unsigned char) data[maxi]; 2354 if (ch > 0xC1) // everything <= 0xC1 (yes, even 0xC1 itself) is not a valid UTF-8 start byte 2355 { 2356 // int __builtin_clz (unsigned int x) 2357 // Returns the number of leading 0-bits in x, starting at the most significant bit 2358 unsigned mask = (unsigned) -1 >> __builtin_clz(ch ^ 0xff); 2359 ch = ch & mask; // mask out length bits, we don't verify their correctness 2360 while ((maxi + charbytes < len) && // 2361 (((unsigned char) data[maxi + charbytes] ^ 0x80) <= 0x3F)) // while next byte is continuation byte 2362 { 2363 ch = (ch << 6) ^ (unsigned char) data[maxi + charbytes] ^ 0x80; // add continuation to char code 2364 ++charbytes; 2365 } 2366 // ch is the decoded unicode code point 2367 if (ch >= 0x1100 && isWideChar(ch)) // the test for 0x1100 is here to avoid the function call in the Latin case 2368 { 2369 if (utf8width + 2 > width) 2370 break; 2371 ++utf8width; 2372 } 2373 } 2374 ++utf8width; 2375 maxi += charbytes; 2376 } 2377 2378 // data[maxi-1] is the last byte of the UTF-8 sequence of the last character that fits 2379 // onto the 1st line. If maxi == len, all characters fit on the line. 2380 2381 if (maxi == len) 2382 { 2383 output(write, data, len); 2384 len = 0; 2385 } 2386 else // if (maxi < len) at least 1 character (data[maxi] that is) doesn't fit on the line 2387 { 2388 int i; 2389 for (i = maxi; i >= 0; --i) 2390 if (data[i] == ' ') 2391 break; 2392 2393 if (i >= 0) 2394 { 2395 output(write, data, i); 2396 data += i + 1; 2397 len -= i + 1; 2398 } 2399 else // did not find a space to split at => split before data[maxi] 2400 { // data[maxi] is always the beginning of a character, never a continuation byte 2401 output(write, data, maxi); 2402 data += maxi; 2403 len -= maxi; 2404 } 2405 } 2406 } 2407 } 2408 if (!wrote_something) // if we didn't already write something to make space in the buffer 2409 write_one_line(write); // write at most one line of actual output 2410 } 2411 2412 /** 2413 * @brief Constructs a LineWrapper that wraps its output to fit into 2414 * screen columns @c x1 (incl.) to @c x2 (excl.). 2415 * 2416 * @c x1 gives the indentation LineWrapper uses if it needs to indent. 2417 */ 2418 LineWrapper(int x1, int x2) : 2419 x(x1), width(x2 - x1), head(0), tail(bufmask) 2420 { 2421 if (width < 2) // because of wide characters we need at least width 2 or the code breaks 2422 width = 2; 2423 } 2424 }; 2425 2426 /** 2427 * @internal 2428 * @brief This is the implementation that is shared between all printUsage() templates. 2429 * Because all printUsage() templates share this implementation, there is no template bloat. 2430 */ 2431 static void printUsage(IStringWriter& write, const Descriptor usage[], int width = 80, // 2432 int last_column_min_percent = 50, int last_column_own_line_max_percent = 75) 2433 { 2434 if (width < 1) // protect against nonsense values 2435 width = 80; 2436 2437 if (width > 10000) // protect against overflow in the following computation 2438 width = 10000; 2439 2440 int last_column_min_width = ((width * last_column_min_percent) + 50) / 100; 2441 int last_column_own_line_max_width = ((width * last_column_own_line_max_percent) + 50) / 100; 2442 if (last_column_own_line_max_width == 0) 2443 last_column_own_line_max_width = 1; 2444 2445 LinePartIterator part(usage); 2446 while (part.nextTable()) 2447 { 2448 2449 /***************** Determine column widths *******************************/ 2450 2451 const int maxcolumns = 8; // 8 columns are enough for everyone 2452 int col_width[maxcolumns]; 2453 int lastcolumn; 2454 int leftwidth; 2455 int overlong_column_threshold = 10000; 2456 do 2457 { 2458 lastcolumn = 0; 2459 for (int i = 0; i < maxcolumns; ++i) 2460 col_width[i] = 0; 2461 2462 part.restartTable(); 2463 while (part.nextRow()) 2464 { 2465 while (part.next()) 2466 { 2467 if (part.column() < maxcolumns) 2468 { 2469 upmax(lastcolumn, part.column()); 2470 if (part.screenLength() < overlong_column_threshold) 2471 // We don't let rows that don't use table separators (\t or \v) influence 2472 // the width of column 0. This allows the user to interject section headers 2473 // or explanatory paragraphs that do not participate in the table layout. 2474 if (part.column() > 0 || part.line() > 0 || part.data()[part.length()] == '\t' 2475 || part.data()[part.length()] == '\v') 2476 upmax(col_width[part.column()], part.screenLength()); 2477 } 2478 } 2479 } 2480 2481 /* 2482 * If the last column doesn't fit on the same 2483 * line as the other columns, we can fix that by starting it on its own line. 2484 * However we can't do this for any of the columns 0..lastcolumn-1. 2485 * If their sum exceeds the maximum width we try to fix this by iteratively 2486 * ignoring the widest line parts in the width determination until 2487 * we arrive at a series of column widths that fit into one line. 2488 * The result is a layout where everything is nicely formatted 2489 * except for a few overlong fragments. 2490 * */ 2491 2492 leftwidth = 0; 2493 overlong_column_threshold = 0; 2494 for (int i = 0; i < lastcolumn; ++i) 2495 { 2496 leftwidth += col_width[i]; 2497 upmax(overlong_column_threshold, col_width[i]); 2498 } 2499 2500 } while (leftwidth > width); 2501 2502 /**************** Determine tab stops and last column handling **********************/ 2503 2504 int tabstop[maxcolumns]; 2505 tabstop[0] = 0; 2506 for (int i = 1; i < maxcolumns; ++i) 2507 tabstop[i] = tabstop[i - 1] + col_width[i - 1]; 2508 2509 int rightwidth = width - tabstop[lastcolumn]; 2510 bool print_last_column_on_own_line = false; 2511 if (rightwidth < last_column_min_width && // if we don't have the minimum requested width for the last column 2512 ( col_width[lastcolumn] == 0 || // and all last columns are > overlong_column_threshold 2513 rightwidth < col_width[lastcolumn] // or there is at least one last column that requires more than the space available 2514 ) 2515 ) 2516 { 2517 print_last_column_on_own_line = true; 2518 rightwidth = last_column_own_line_max_width; 2519 } 2520 2521 // If lastcolumn == 0 we must disable print_last_column_on_own_line because 2522 // otherwise 2 copies of the last (and only) column would be output. 2523 // Actually this is just defensive programming. It is currently not 2524 // possible that lastcolumn==0 and print_last_column_on_own_line==true 2525 // at the same time, because lastcolumn==0 => tabstop[lastcolumn] == 0 => 2526 // rightwidth==width => rightwidth>=last_column_min_width (unless someone passes 2527 // a bullshit value >100 for last_column_min_percent) => the above if condition 2528 // is false => print_last_column_on_own_line==false 2529 if (lastcolumn == 0) 2530 print_last_column_on_own_line = false; 2531 2532 LineWrapper lastColumnLineWrapper(width - rightwidth, width); 2533 LineWrapper interjectionLineWrapper(0, width); 2534 2535 part.restartTable(); 2536 2537 /***************** Print out all rows of the table *************************************/ 2538 2539 while (part.nextRow()) 2540 { 2541 int x = -1; 2542 while (part.next()) 2543 { 2544 if (part.column() > lastcolumn) 2545 continue; // drop excess columns (can happen if lastcolumn == maxcolumns-1) 2546 2547 if (part.column() == 0) 2548 { 2549 if (x >= 0) 2550 write("\n", 1); 2551 x = 0; 2552 } 2553 2554 indent(write, x, tabstop[part.column()]); 2555 2556 if ((part.column() < lastcolumn) 2557 && (part.column() > 0 || part.line() > 0 || part.data()[part.length()] == '\t' 2558 || part.data()[part.length()] == '\v')) 2559 { 2560 write(part.data(), part.length()); 2561 x += part.screenLength(); 2562 } 2563 else // either part.column() == lastcolumn or we are in the special case of 2564 // an interjection that doesn't contain \v or \t 2565 { 2566 // NOTE: This code block is not necessarily executed for 2567 // each line, because some rows may have fewer columns. 2568 2569 LineWrapper& lineWrapper = (part.column() == 0) ? interjectionLineWrapper : lastColumnLineWrapper; 2570 2571 if (!print_last_column_on_own_line || part.column() != lastcolumn) 2572 lineWrapper.process(write, part.data(), part.length()); 2573 } 2574 } // while 2575 2576 if (print_last_column_on_own_line) 2577 { 2578 part.restartRow(); 2579 while (part.next()) 2580 { 2581 if (part.column() == lastcolumn) 2582 { 2583 write("\n", 1); 2584 int _ = 0; 2585 indent(write, _, width - rightwidth); 2586 lastColumnLineWrapper.process(write, part.data(), part.length()); 2587 } 2588 } 2589 } 2590 2591 write("\n", 1); 2592 lastColumnLineWrapper.flush(write); 2593 interjectionLineWrapper.flush(write); 2594 } 2595 } 2596 } 2597 2598 } 2599 ; 2600 2601 /** 2602 * @brief Outputs a nicely formatted usage string with support for multi-column formatting 2603 * and line-wrapping. 2604 * 2605 * printUsage() takes the @c help texts of a Descriptor[] array and formats them into 2606 * a usage message, wrapping lines to achieve the desired output width. 2607 * 2608 * <b>Table formatting:</b> 2609 * 2610 * Aside from plain strings which are simply line-wrapped, the usage may contain tables. Tables 2611 * are used to align elements in the output. 2612 * 2613 * @code 2614 * // Without a table. The explanatory texts are not aligned. 2615 * -c, --create |Creates something. 2616 * -k, --kill |Destroys something. 2617 * 2618 * // With table formatting. The explanatory texts are aligned. 2619 * -c, --create |Creates something. 2620 * -k, --kill |Destroys something. 2621 * @endcode 2622 * 2623 * Table formatting removes the need to pad help texts manually with spaces to achieve 2624 * alignment. To create a table, simply insert \\t (tab) characters to separate the cells 2625 * within a row. 2626 * 2627 * @code 2628 * const option::Descriptor usage[] = { 2629 * {..., "-c, --create \tCreates something." }, 2630 * {..., "-k, --kill \tDestroys something." }, ... 2631 * @endcode 2632 * 2633 * Note that you must include the minimum amount of space desired between cells yourself. 2634 * Table formatting will insert further spaces as needed to achieve alignment. 2635 * 2636 * You can insert line breaks within cells by using \\v (vertical tab). 2637 * 2638 * @code 2639 * const option::Descriptor usage[] = { 2640 * {..., "-c,\v--create \tCreates\vsomething." }, 2641 * {..., "-k,\v--kill \tDestroys\vsomething." }, ... 2642 * 2643 * // results in 2644 * 2645 * -c, Creates 2646 * --create something. 2647 * -k, Destroys 2648 * --kill something. 2649 * @endcode 2650 * 2651 * You can mix lines that do not use \\t or \\v with those that do. The plain 2652 * lines will not mess up the table layout. Alignment of the table columns will 2653 * be maintained even across these interjections. 2654 * 2655 * @code 2656 * const option::Descriptor usage[] = { 2657 * {..., "-c, --create \tCreates something." }, 2658 * {..., "----------------------------------" }, 2659 * {..., "-k, --kill \tDestroys something." }, ... 2660 * 2661 * // results in 2662 * 2663 * -c, --create Creates something. 2664 * ---------------------------------- 2665 * -k, --kill Destroys something. 2666 * @endcode 2667 * 2668 * You can have multiple tables within the same usage whose columns are 2669 * aligned independently. Simply insert a dummy Descriptor with @c help==0. 2670 * 2671 * @code 2672 * const option::Descriptor usage[] = { 2673 * {..., "Long options:" }, 2674 * {..., "--very-long-option \tDoes something long." }, 2675 * {..., "--ultra-super-mega-long-option \tTakes forever to complete." }, 2676 * {..., 0 }, // ---------- table break ----------- 2677 * {..., "Short options:" }, 2678 * {..., "-s \tShort." }, 2679 * {..., "-q \tQuick." }, ... 2680 * 2681 * // results in 2682 * 2683 * Long options: 2684 * --very-long-option Does something long. 2685 * --ultra-super-mega-long-option Takes forever to complete. 2686 * Short options: 2687 * -s Short. 2688 * -q Quick. 2689 * 2690 * // Without the table break it would be 2691 * 2692 * Long options: 2693 * --very-long-option Does something long. 2694 * --ultra-super-mega-long-option Takes forever to complete. 2695 * Short options: 2696 * -s Short. 2697 * -q Quick. 2698 * @endcode 2699 * 2700 * <b>Output methods:</b> 2701 * 2702 * Because TheLeanMeanC++Option parser is freestanding, you have to provide the means for 2703 * output in the first argument(s) to printUsage(). Because printUsage() is implemented as 2704 * a set of template functions, you have great flexibility in your choice of output 2705 * method. The following example demonstrates typical uses. Anything that's similar enough 2706 * will work. 2707 * 2708 * @code 2709 * #include <unistd.h> // write() 2710 * #include <iostream> // cout 2711 * #include <sstream> // ostringstream 2712 * #include <cstdio> // fwrite() 2713 * using namespace std; 2714 * 2715 * void my_write(const char* str, int size) { 2716 * fwrite(str, size, 1, stdout); 2717 * } 2718 * 2719 * struct MyWriter { 2720 * void write(const char* buf, size_t size) const { 2721 * fwrite(str, size, 1, stdout); 2722 * } 2723 * }; 2724 * 2725 * struct MyWriteFunctor { 2726 * void operator()(const char* buf, size_t size) { 2727 * fwrite(str, size, 1, stdout); 2728 * } 2729 * }; 2730 * ... 2731 * printUsage(my_write, usage); // custom write function 2732 * printUsage(MyWriter(), usage); // temporary of a custom class 2733 * MyWriter writer; 2734 * printUsage(writer, usage); // custom class object 2735 * MyWriteFunctor wfunctor; 2736 * printUsage(&wfunctor, usage); // custom functor 2737 * printUsage(write, 1, usage); // write() to file descriptor 1 2738 * printUsage(cout, usage); // an ostream& 2739 * printUsage(fwrite, stdout, usage); // fwrite() to stdout 2740 * ostringstream sstr; 2741 * printUsage(sstr, usage); // an ostringstream& 2742 * 2743 * @endcode 2744 * 2745 * @par Notes: 2746 * @li the @c write() method of a class that is to be passed as a temporary 2747 * as @c MyWriter() is in the example, must be a @c const method, because 2748 * temporary objects are passed as const reference. This only applies to 2749 * temporary objects that are created and destroyed in the same statement. 2750 * If you create an object like @c writer in the example, this restriction 2751 * does not apply. 2752 * @li a functor like @c MyWriteFunctor in the example must be passed as a pointer. 2753 * This differs from the way functors are passed to e.g. the STL algorithms. 2754 * @li All printUsage() templates are tiny wrappers around a shared non-template implementation. 2755 * So there's no penalty for using different versions in the same program. 2756 * @li printUsage() always interprets Descriptor::help as UTF-8 and always produces UTF-8-encoded 2757 * output. If your system uses a different charset, you must do your own conversion. You 2758 * may also need to change the font of the console to see non-ASCII characters properly. 2759 * This is particularly true for Windows. 2760 * @li @b Security @b warning: Do not insert untrusted strings (such as user-supplied arguments) 2761 * into the usage. printUsage() has no protection against malicious UTF-8 sequences. 2762 * 2763 * @param prn The output method to use. See the examples above. 2764 * @param usage the Descriptor[] array whose @c help texts will be formatted. 2765 * @param width the maximum number of characters per output line. Note that this number is 2766 * in actual characters, not bytes. printUsage() supports UTF-8 in @c help and will 2767 * count multi-byte UTF-8 sequences properly. Asian wide characters are counted 2768 * as 2 characters. 2769 * @param last_column_min_percent (0-100) The minimum percentage of @c width that should be available 2770 * for the last column (which typically contains the textual explanation of an option). 2771 * If less space is available, the last column will be printed on its own line, indented 2772 * according to @c last_column_own_line_max_percent. 2773 * @param last_column_own_line_max_percent (0-100) If the last column is printed on its own line due to 2774 * less than @c last_column_min_percent of the width being available, then only 2775 * @c last_column_own_line_max_percent of the extra line(s) will be used for the 2776 * last column's text. This ensures an indentation. See example below. 2777 * 2778 * @code 2779 * // width=20, last_column_min_percent=50 (i.e. last col. min. width=10) 2780 * --3456789 1234567890 2781 * 1234567890 2782 * 2783 * // width=20, last_column_min_percent=75 (i.e. last col. min. width=15) 2784 * // last_column_own_line_max_percent=75 2785 * --3456789 2786 * 123456789012345 2787 * 67890 2788 * 2789 * // width=20, last_column_min_percent=75 (i.e. last col. min. width=15) 2790 * // last_column_own_line_max_percent=33 (i.e. max. 5) 2791 * --3456789 2792 * 12345 2793 * 67890 2794 * 12345 2795 * 67890 2796 * @endcode 2797 */ 2798 template<typename OStream> 2799 void printUsage(OStream& prn, const Descriptor usage[], int width = 80, int last_column_min_percent = 50, 2800 int last_column_own_line_max_percent = 75) 2801 { 2802 PrintUsageImplementation::OStreamWriter<OStream> write(prn); 2803 PrintUsageImplementation::printUsage(write, usage, width, last_column_min_percent, last_column_own_line_max_percent); 2804 } 2805 2806 template<typename Function> 2807 void printUsage(Function* prn, const Descriptor usage[], int width = 80, int last_column_min_percent = 50, 2808 int last_column_own_line_max_percent = 75) 2809 { 2810 PrintUsageImplementation::FunctionWriter<Function> write(prn); 2811 PrintUsageImplementation::printUsage(write, usage, width, last_column_min_percent, last_column_own_line_max_percent); 2812 } 2813 2814 template<typename Temporary> 2815 void printUsage(const Temporary& prn, const Descriptor usage[], int width = 80, int last_column_min_percent = 50, 2816 int last_column_own_line_max_percent = 75) 2817 { 2818 PrintUsageImplementation::TemporaryWriter<Temporary> write(prn); 2819 PrintUsageImplementation::printUsage(write, usage, width, last_column_min_percent, last_column_own_line_max_percent); 2820 } 2821 2822 template<typename Syscall> 2823 void printUsage(Syscall* prn, int fd, const Descriptor usage[], int width = 80, int last_column_min_percent = 50, 2824 int last_column_own_line_max_percent = 75) 2825 { 2826 PrintUsageImplementation::SyscallWriter<Syscall> write(prn, fd); 2827 PrintUsageImplementation::printUsage(write, usage, width, last_column_min_percent, last_column_own_line_max_percent); 2828 } 2829 2830 template<typename Function, typename Stream> 2831 void printUsage(Function* prn, Stream* stream, const Descriptor usage[], int width = 80, int last_column_min_percent = 2832 50, 2833 int last_column_own_line_max_percent = 75) 2834 { 2835 PrintUsageImplementation::StreamWriter<Function, Stream> write(prn, stream); 2836 PrintUsageImplementation::printUsage(write, usage, width, last_column_min_percent, last_column_own_line_max_percent); 2837 } 2838 2839 } 2840 // namespace Option_Parser 2841 2842 } 2843 // namespace ATOOLS 2844 2845 #endif /* ATOOLS_Org_Option_Parser.H */ 2846
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |