]>
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; | |
20e315e1 | 34 | public: |
e8bec9e7 CV |
35 | DefaultFormatter (std::ostream &aStream) |
36 | : o_len(35), h_len(45), | |
37 | s_lead(" -"), l_lead(" --"), | |
38 | theStream(aStream) | |
39 | {} | |
40 | DefaultFormatter (std::ostream &aStream, | |
41 | unsigned int o_len, unsigned int h_len, | |
42 | std::string s_lead, std::string l_lead) | |
43 | : o_len(o_len), h_len(h_len), | |
44 | s_lead(s_lead), l_lead(l_lead), | |
45 | theStream(aStream) | |
46 | {} | |
20e315e1 | 47 | void operator () (Option *anOption) { |
d32a3004 JT |
48 | if (anOption->shortOption ()[0] == '\0') |
49 | theStream << " "; | |
50 | else | |
51 | theStream << s_lead << anOption->shortOption ()[0]; | |
52 | theStream << l_lead << anOption->longOption () | |
e8bec9e7 CV |
53 | << std::string (o_len |
54 | - s_lead.size () - 1 - l_lead.size () | |
55 | - anOption->longOption ().size (), ' '); | |
20e315e1 | 56 | std::string helpmsg = anOption->shortHelp(); |
e8bec9e7 | 57 | while (helpmsg.size() > h_len) |
20e315e1 RC |
58 | { |
59 | // TODO: consider using a line breaking strategy here. | |
e8bec9e7 CV |
60 | int pos = helpmsg.substr(0,h_len).find_last_of(" "); |
61 | theStream << helpmsg.substr(0,pos) | |
62 | << std::endl << std::string (o_len, ' '); | |
20e315e1 | 63 | helpmsg.erase (0,pos+1); |
20e315e1 | 64 | } |
e8bec9e7 | 65 | theStream << helpmsg << std::endl; |
20e315e1 RC |
66 | } |
67 | std::ostream &theStream; | |
68 | }; | |
69 | ||
20e315e1 | 70 | #endif // _GETOPT___DEFAULTFORMATTER_H_ |