]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2003 Robert Collins. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins <robertc@hotmail.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | #ifndef _GETOPT___DEFAULTFORMATTER_H_ | |
17 | #define _GETOPT___DEFAULTFORMATTER_H_ | |
18 | ||
19 | #include <iostream> | |
20 | #include <vector> | |
21 | #include "getopt++/Option.h" | |
22 | ||
23 | /* Show the options on the left, the short description on the right. | |
24 | * Option display must be < o_len characters in length. | |
25 | * Descriptions must be < h_len characters in length. | |
26 | * For compatibility with default terminal width o_len + h_len <= 80. | |
27 | */ | |
28 | class DefaultFormatter { | |
29 | private: | |
30 | const unsigned int o_len; | |
31 | const unsigned int h_len; | |
32 | const std::string s_lead; | |
33 | const std::string l_lead; | |
34 | std::ostream &theStream; | |
35 | StrLookup strLookup; | |
36 | public: | |
37 | DefaultFormatter (std::ostream &aStream, StrLookup aLookup) | |
38 | : o_len(35), h_len(45), | |
39 | s_lead(" -"), l_lead(" --"), | |
40 | theStream(aStream), strLookup(aLookup) | |
41 | {} | |
42 | DefaultFormatter (std::ostream &aStream, StrLookup aLookup, | |
43 | unsigned int o_len, unsigned int h_len, | |
44 | std::string s_lead, std::string l_lead) | |
45 | : o_len(o_len), h_len(h_len), | |
46 | s_lead(s_lead), l_lead(l_lead), | |
47 | theStream(aStream), strLookup(aLookup) | |
48 | {} | |
49 | void operator () (Option *anOption) { | |
50 | if (anOption->shortOption ()[0] == '\0') | |
51 | theStream << " "; | |
52 | else | |
53 | theStream << s_lead << anOption->shortOption ()[0]; | |
54 | ||
55 | std::string longOption = anOption->longOptionPrefixes ()[0] + | |
56 | anOption->longOption (); | |
57 | ||
58 | theStream << l_lead << longOption | |
59 | << std::string (o_len | |
60 | - s_lead.size () - 1 - l_lead.size () | |
61 | - longOption.size (), ' '); | |
62 | std::string helpmsg = strLookup(anOption->shortHelp()); | |
63 | while (helpmsg.size() > h_len) | |
64 | { | |
65 | size_t pos = helpmsg.substr(0,h_len).find_last_of(" "); | |
66 | // if there's no space character, working out where to line-break | |
67 | // composing UTF-8 characters is hard, so don't bother trying... | |
68 | if (pos == std::string::npos) | |
69 | break; | |
70 | theStream << helpmsg.substr(0,pos) | |
71 | << std::endl << std::string (o_len, ' '); | |
72 | helpmsg.erase (0,pos+1); | |
73 | } | |
74 | theStream << helpmsg << std::endl; | |
75 | } | |
76 | }; | |
77 | ||
78 | #endif // _GETOPT___DEFAULTFORMATTER_H_ |