The Lean Mean C++ Option Parser
src/testprintusage.cpp
Go to the documentation of this file.
00001 /* Written 2012 by Matthias S. Benkmann
00002  *
00003  * The author hereby waives all copyright and related rights to the contents
00004  * of this example file (testprintusage.cpp) to the extent possible under the law.
00005  */
00006 
00016 #include <cstdio>
00017 #include <iostream>
00018 #include <sstream>
00019 #include <unistd.h>
00020 
00021 #include "optionparser.h"
00022 
00023 using namespace std;
00024 using option::Descriptor;
00025 using option::Arg;
00026 
00027 const Descriptor test_vtabs[] = {
00028     {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  "},
00029     {0,0,0,0,0,0}
00030 };
00031 
00032 const Descriptor test_columns[] = {
00033     {0,0,"","",Arg::None, "Column 1 line 1  \t\tColumn 3 line 1\n"
00034                           "Column 1 line 2  \tColumn 2 line 2   \tColumn 3 line 2\n"
00035                           "Column 1 line 3  \t\tColumn 3 line 3" },
00036     {0,0,0,0,0,0}
00037 };
00038 
00039 const Descriptor test_column1[] = {
00040     {0,0,"","",Arg::None, "11 \t21\v22\v23\t 31\nxx" },
00041     {0,0,0,0,0,0}
00042 };
00043 
00044 
00045 const Descriptor test_tables[] = {
00046     {0,0,"","",Arg::None,0}, // table break
00047     {0,0,"","",Arg::None,0}, // table break
00048     {0,0,"","",Arg::None, "Each table has its own column widths and is not aligned with other tables."},
00049     {0,0,"","",Arg::None, "Table 1 Column 1 Line 1 \tTable 1 Column 2 Line 1 \tTable 1 Column 3 Line 1\n"
00050                           "Table 1 Col 1 Line 2 \tTable 1 Col 2 Line 2 \tTable 1 Col 3 Line 2"
00051                            },
00052     {0,0,"","",Arg::None, "Table 1 Col 1 Line 3 \tTable 1 Col 2 Line 3 \tTable 1 Column 3 Line 3\n"
00053                           "Table 1 Col 1 Line 4 \tTable 1 Column 2 Line 4 \tTable 1 Column 3 Line 4"
00054                            },
00055     {0,0,"","",Arg::None,0}, // table break
00056     {0,0,"","",Arg::None,0}, // table break
00057     {0,0,"","",Arg::None,  "This is the only line of table 2." },
00058     {0,0,"","",Arg::None,0}, // table break
00059     {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"
00060                            "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"
00061                            "This is a reasonably sized line 3 of table 3."
00062                           },
00063     {0,0,"","",Arg::None,0}, // table break
00064     {0,0,"","",Arg::None, "Table 4:\n"
00065                           "  \tTable 4 C 1 L 1 \tTable 4 C 2 L 1 \tTable 4 C 3 L 1\n"
00066                             "\tTable 4 C 1 L 2 \tTable 4 C 2 L 2 \tTable 4 C 3 L 2"
00067                            },
00068     {0,0,"","",Arg::None,0}, // table break
00069     {0,0,"","",Arg::None, "This is the only line of table 5"},
00070     {0,0,"","",Arg::None,0}, // table break
00071     {0,0,"","",Arg::None, "Table 6 C 1 L 1 \tTable 6 C 2 L 1 \tTable 6 C 3 L 1\n"
00072                           "Table 6 C 1 L 2 \tTable 6 C 2 L 2 \tTable 6 C 3 L 2"
00073                           },
00074     {0,0,"","",Arg::None,0 }, // table break
00075     {0,0,"","",Arg::None, "Table 7 Column 1 Line 1 \tTable 7 Column 2 Line 1 \tTable 7 Column 3 Line 1\n"
00076                           "Table 7 Column 1 Line 2 \tTable 7 Column 2 Line 2 \tTable 7 Column 3 Line 2\n"
00077                           },
00078     {0,0,0,0,0,0}
00079 };
00080 
00081 const Descriptor test_nohelp[] = {
00082     {0,0,"","",Arg::None, 0 },
00083     {0,0,"","",Arg::None, 0 },
00084     {0,0,"","",Arg::None, 0 },
00085     {0,0,0,0,0,0}
00086 };
00087 
00088 const Descriptor test_wide[] = {
00089     {0,0,"","",Arg::None, "Roma\t|x漢" },
00090     {0,0,"","",Arg::None, "カタカナ\t|漢字" },
00091     {0,0,"","",Arg::None, "漢字\t|漢カ " },
00092     {0,0,"","",Arg::None, "漢字\t|カナ 漢字" },
00093     {0,0,0,0,0,0}
00094 };
00095 
00096 const Descriptor test_overlong[] = {
00097     {0,0,"","",Arg::None, "Good \t| Good \t| This is good." },
00098     {0,0,"","",Arg::None, "Good \t| This is an overlong cell. \t| This is good." },
00099     {0,0,"","",Arg::None, "Good \t| Good \t| This is good." },
00100     {0,0,0,0,0,0}
00101 };
00102 
00103 const Descriptor test_toomanycolumns[] = {
00104     {0,0,"","",Arg::None, "This \ttable \thas \ttoo \tmany \tcolumns. \tThe \tlast \tcolumns \tare \tdiscarded." },
00105     {0,0,"","",Arg::None, "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11" },
00106     {0,0,0,0,0,0}
00107 };
00108 
00109 const Descriptor test_ownline[] = {
00110     {0,0,"","",Arg::None, "1234567890AB\vBA0987654321\tStarts on its own line and is indented somewhat.\vThis one, too." },
00111     {0,0,0,0,0,0}
00112 };
00113 
00114 const Descriptor test_overflow[] = {
00115     {0,0,"","",Arg::None, "漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字漢字" },
00116     {0,0,0,0,0,0}
00117 };
00118 
00119 void stderr_write(const char* str, int size)
00120 {
00121    fwrite(str, size, 1, stderr);
00122 }
00123 
00124 struct stderr_writer
00125 {
00126   void write(const char* buf, size_t size) const
00127   {
00128     ::write(2, buf, size);
00129   }
00130 };
00131 
00132 struct stderr_write_functor
00133 {
00134   void operator()(const char* buf, size_t size)
00135   {
00136     ::write(2, buf, size);
00137   }
00138 };
00139 
00140 int main()
00141 {
00142   fputs("---------------------------------------------------------------\n",stderr);
00143   option::printUsage(stderr_write, test_overflow, 1);
00144   fputs("---------------------------------------------------------------\n",stderr);
00145   option::printUsage(stderr_write, test_vtabs);
00146   fputs("---------------------------------------------------------------\n",stderr);
00147   option::printUsage(stderr_writer(), test_columns);
00148   fputs("---------------------------------------------------------------\n",stderr);
00149   option::printUsage(write, 2, test_column1);
00150   fputs("---------------------------------------------------------------\n",stderr);
00151   option::printUsage(cerr, test_tables);
00152   fputs("---------------------------------------------------------------\n",stderr);
00153   option::printUsage(fwrite, stderr, test_nohelp);
00154   fputs("---------------------------------------------------------------\n",stderr);
00155   ostringstream sst;
00156   option::printUsage(sst, test_wide, 8);
00157   cerr<<sst.str();
00158   fputs("---------------------------------------------------------------\n",stderr);
00159   stderr_write_functor stderr_write_f;
00160   option::printUsage(&stderr_write_f, test_overlong, 30);
00161   fputs("---------------------------------------------------------------\n",stderr);
00162   option::printUsage(stderr_write, test_toomanycolumns);
00163   fputs("---------------------------------------------------------------\n",stderr);
00164   option::printUsage(stderr_write, test_ownline, 20);
00165   fputs("---------------------------------------------------------------\n",stderr);
00166   return 0;
00167 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator