The Lean Mean C++ Option Parser
testprintusage.cpp
Go to the documentation of this file.
1 /* Written 2012 by Matthias S. Benkmann
2  *
3  * The author hereby waives all copyright and related rights to the contents
4  * of this example file (testprintusage.cpp) to the extent possible under the law.
5  */
6 
16 #include <cstdio>
17 #include <iostream>
18 #include <sstream>
19 #include <unistd.h>
20 
21 #include "optionparser.h"
22 
23 using namespace std;
24 using option::Descriptor;
25 using option::Arg;
26 
27 const Descriptor test_vtabs[] = {
28  {0,0,"","",Arg::None, "Cölüümn 1 line ı\vColumn 1 line 2\vColumn 1 line 3 \t\vColumn 2 line 2 \tColumn 3 line 1\v\vColumn 3 line 3 "},
29  {0,0,0,0,0,0}
30 };
31 
32 const Descriptor test_columns[] = {
33  {0,0,"","",Arg::None, "Column 1 line 1 \t\tColumn 3 line 1\n"
34  "Column 1 line 2 \tColumn 2 line 2 \tColumn 3 line 2\n"
35  "Column 1 line 3 \t\tColumn 3 line 3" },
36  {0,0,0,0,0,0}
37 };
38 
39 const Descriptor test_column1[] = {
40  {0,0,"","",Arg::None, "11 \t21\v22\v23\t 31\nxx" },
41  {0,0,0,0,0,0}
42 };
43 
44 
45 const Descriptor test_tables[] = {
46  {0,0,"","",Arg::None,0}, // table break
47  {0,0,"","",Arg::None,0}, // table break
48  {0,0,"","",Arg::None, "Each table has its own column widths and is not aligned with other tables."},
49  {0,0,"","",Arg::None, "Table 1 Column 1 Line 1 \tTable 1 Column 2 Line 1 \tTable 1 Column 3 Line 1\n"
50  "Table 1 Col 1 Line 2 \tTable 1 Col 2 Line 2 \tTable 1 Col 3 Line 2"
51  },
52  {0,0,"","",Arg::None, "Table 1 Col 1 Line 3 \tTable 1 Col 2 Line 3 \tTable 1 Column 3 Line 3\n"
53  "Table 1 Col 1 Line 4 \tTable 1 Column 2 Line 4 \tTable 1 Column 3 Line 4"
54  },
55  {0,0,"","",Arg::None,0}, // table break
56  {0,0,"","",Arg::None,0}, // table break
57  {0,0,"","",Arg::None, "This is the only line of table 2." },
58  {0,0,"","",Arg::None,0}, // table break
59  {0,0,"","",Arg::None, "This is the very long 1st line of table 3. It is more than 80 characters in length and therefore needs to be wrapped. In fact it is so long that it needs to be wrapped multiple times to fit into a normal 80 characters terminal.\v"
60  "This is the very long 2nd line of table 3. It is more than 80 characters in length and therefore needs to be wrapped. In fact it is so long that it needs to be wrapped multiple times to fit into a normal 80 characters terminal.\v"
61  "This is a reasonably sized line 3 of table 3."
62  },
63  {0,0,"","",Arg::None,0}, // table break
64  {0,0,"","",Arg::None, "Table 4:\n"
65  " \tTable 4 C 1 L 1 \tTable 4 C 2 L 1 \tTable 4 C 3 L 1\n"
66  "\tTable 4 C 1 L 2 \tTable 4 C 2 L 2 \tTable 4 C 3 L 2"
67  },
68  {0,0,"","",Arg::None,0}, // table break
69  {0,0,"","",Arg::None, "This is the only line of table 5"},
70  {0,0,"","",Arg::None,0}, // table break
71  {0,0,"","",Arg::None, "Table 6 C 1 L 1 \tTable 6 C 2 L 1 \tTable 6 C 3 L 1\n"
72  "Table 6 C 1 L 2 \tTable 6 C 2 L 2 \tTable 6 C 3 L 2"
73  },
74  {0,0,"","",Arg::None,0 }, // table break
75  {0,0,"","",Arg::None, "Table 7 Column 1 Line 1 \tTable 7 Column 2 Line 1 \tTable 7 Column 3 Line 1\n"
76  "Table 7 Column 1 Line 2 \tTable 7 Column 2 Line 2 \tTable 7 Column 3 Line 2\n"
77  },
78  {0,0,0,0,0,0}
79 };
80 
81 const Descriptor test_nohelp[] = {
82  {0,0,"","",Arg::None, 0 },
83  {0,0,"","",Arg::None, 0 },
84  {0,0,"","",Arg::None, 0 },
85  {0,0,0,0,0,0}
86 };
87 
88 const Descriptor test_wide[] = {
89  {0,0,"","",Arg::None, "Roma\t|x漢" },
90  {0,0,"","",Arg::None, "カタカナ\t|漢字" },
91  {0,0,"","",Arg::None, "漢字\t|漢カ " },
92  {0,0,"","",Arg::None, "漢字\t|カナ 漢字" },
93  {0,0,0,0,0,0}
94 };
95 
96 const Descriptor test_overlong[] = {
97  {0,0,"","",Arg::None, "Good \t| Good \t| This is good." },
98  {0,0,"","",Arg::None, "Good \t| This is an overlong cell. \t| This is good." },
99  {0,0,"","",Arg::None, "Good \t| Good \t| This is good." },
100  {0,0,0,0,0,0}
101 };
102 
103 const Descriptor test_toomanycolumns[] = {
104  {0,0,"","",Arg::None, "This \ttable \thas \ttoo \tmany \tcolumns. \tThe \tlast \tcolumns \tare \tdiscarded." },
105  {0,0,"","",Arg::None, "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11" },
106  {0,0,0,0,0,0}
107 };
108 
109 const Descriptor test_ownline[] = {
110  {0,0,"","",Arg::None, "1234567890AB\vBA0987654321\tStarts on its own line and is indented somewhat.\vThis one, too." },
111  {0,0,0,0,0,0}
112 };
113 
114 const Descriptor test_overflow[] = {
115  {0,0,"","",Arg::None, "漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字" },
116  {0,0,0,0,0,0}
117 };
118 
119 void stderr_write(const char* str, int size)
120 {
121  fwrite(str, size, 1, stderr);
122 }
123 
124 struct stderr_writer
125 {
126  void write(const char* buf, size_t size) const
127  {
128  ::write(2, buf, size);
129  }
130 };
131 
132 struct stderr_write_functor
133 {
134  void operator()(const char* buf, size_t size)
135  {
136  ::write(2, buf, size);
137  }
138 };
139 
140 int main()
141 {
142  fputs("---------------------------------------------------------------\n",stderr);
143  option::printUsage(stderr_write, test_overflow, 1);
144  fputs("---------------------------------------------------------------\n",stderr);
145  option::printUsage(stderr_write, test_vtabs);
146  fputs("---------------------------------------------------------------\n",stderr);
147  option::printUsage(stderr_writer(), test_columns);
148  fputs("---------------------------------------------------------------\n",stderr);
149  option::printUsage(write, 2, test_column1);
150  fputs("---------------------------------------------------------------\n",stderr);
151  option::printUsage(cerr, test_tables);
152  fputs("---------------------------------------------------------------\n",stderr);
153  option::printUsage(fwrite, stderr, test_nohelp);
154  fputs("---------------------------------------------------------------\n",stderr);
155  ostringstream sst;
156  option::printUsage(sst, test_wide, 8);
157  cerr<<sst.str();
158  fputs("---------------------------------------------------------------\n",stderr);
159  stderr_write_functor stderr_write_f;
160  option::printUsage(&stderr_write_f, test_overlong, 30);
161  fputs("---------------------------------------------------------------\n",stderr);
162  option::printUsage(stderr_write, test_toomanycolumns);
163  fputs("---------------------------------------------------------------\n",stderr);
164  option::printUsage(stderr_write, test_ownline, 20);
165  fputs("---------------------------------------------------------------\n",stderr);
166  return 0;
167 }
This is the only file required to use The Lean Mean C++ Option Parser. Just #include it and you&#39;re se...
static ArgStatus None(const Option &, bool)
For options that don&#39;t take an argument: Returns ARG_NONE.
Definition: optionparser.h:926
Functions for checking the validity of option arguments.
Definition: optionparser.h:923
Describes an option, its help text (usage) and how it should be parsed.
Definition: optionparser.h:315
void printUsage(OStream &prn, const Descriptor usage[], int width=80, int last_column_min_percent=50, int last_column_own_line_max_percent=75)
Outputs a nicely formatted usage string with support for multi-column formatting and line-wrapping...