]>
Commit | Line | Data |
---|---|---|
20e315e1 RC |
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 | ||
20e315e1 RC |
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. | |
e8bec9e7 CV |
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. | |
20e315e1 RC |
27 | */ |
28 | class DefaultFormatter { | |
e8bec9e7 CV |
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; | |
20f237b4 JT |
34 | std::ostream &theStream; |
35 | StrLookup strLookup; | |
20e315e1 | 36 | public: |
20f237b4 | 37 | DefaultFormatter (std::ostream &aStream, StrLookup aLookup) |
e8bec9e7 CV |
38 | : o_len(35), h_len(45), |
39 | s_lead(" -"), l_lead(" --"), | |
20f237b4 | 40 | theStream(aStream), strLookup(aLookup) |
e8bec9e7 | 41 | {} |
20f237b4 | 42 | DefaultFormatter (std::ostream &aStream, StrLookup aLookup, |
e8bec9e7 CV |
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), | |
20f237b4 | 47 | theStream(aStream), strLookup(aLookup) |
e8bec9e7 | 48 | {} |
20e315e1 | 49 | void operator () (Option *anOption) { |
d32a3004 JT |
50 | if (anOption->shortOption ()[0] == '\0') |
51 | theStream << " "; | |
52 | else | |
53 | theStream << s_lead << anOption->shortOption ()[0]; | |
aa071e60 JT |
54 | |
55 | std::string longOption = anOption->longOptionPrefixes ()[0] + | |
56 | anOption->longOption (); | |
57 | ||
58 | theStream << l_lead << longOption | |
e8bec9e7 CV |
59 | << std::string (o_len |
60 | - s_lead.size () - 1 - l_lead.size () | |
aa071e60 | 61 | - longOption.size (), ' '); |
20f237b4 | 62 | std::string helpmsg = strLookup(anOption->shortHelp()); |
e8bec9e7 | 63 | while (helpmsg.size() > h_len) |
20e315e1 | 64 | { |
0ee13c62 | 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; | |
e8bec9e7 CV |
70 | theStream << helpmsg.substr(0,pos) |
71 | << std::endl << std::string (o_len, ' '); | |
20e315e1 | 72 | helpmsg.erase (0,pos+1); |
20e315e1 | 73 | } |
e8bec9e7 | 74 | theStream << helpmsg << std::endl; |
20e315e1 | 75 | } |
20e315e1 RC |
76 | }; |
77 | ||
20e315e1 | 78 | #endif // _GETOPT___DEFAULTFORMATTER_H_ |