Newer
Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
*/
bool nextRow()
{
if (ptr == 0)
{
restartRow();
return rowstart != 0;
}
while (*ptr != 0 && *ptr != '\n')
++ptr;
if (*ptr == 0)
{
if ((rowdesc + 1)->help == 0) // table break
return false;
++rowdesc;
rowstart = rowdesc->help;
}
else // if (*ptr == '\n')
{
rowstart = ptr + 1;
}
restartRow();
return true;
}
/**
* @brief Reset iteration to the beginning of the current row.
*/
void restartRow()
{
ptr = rowstart;
col = -1;
len = 0;
screenlen = 0;
max_line_in_block = 0;
line_in_block = 0;
target_line_in_block = 0;
hit_target_line = true;
}
/**
* @brief Moves iteration to the next part (if any). Has to be called once after each call to
* @ref nextRow() to move to the 1st part of the row.
* @retval false if moving to next part failed because no further part exists.
*
* See @ref LinePartIterator for details about the iteration.
*/
bool next()
{
if (ptr == 0)
return false;
if (col == -1)
{
col = 0;
update_length();
return true;
}
ptr += len;
while (true)
{
switch (*ptr)
{
case '\v':
upmax(max_line_in_block, ++line_in_block);
++ptr;
break;
case '\t':
if (!hit_target_line) // if previous column did not have the targetline
{ // then "insert" a 0-length part
update_length();
hit_target_line = true;
return true;
}
hit_target_line = false;
line_in_block = 0;
++col;
++ptr;
break;
case 0:
case '\n':
if (!hit_target_line) // if previous column did not have the targetline
{ // then "insert" a 0-length part
update_length();
hit_target_line = true;
return true;
}
if (++target_line_in_block > max_line_in_block)
{
update_length();
return false;
}
hit_target_line = false;
line_in_block = 0;
col = 0;
ptr = rowstart;
continue;
default:
++ptr;
continue;
} // switch
if (line_in_block == target_line_in_block)
{
update_length();
hit_target_line = true;
return true;
}
} // while
}
/**
* @brief Returns the index (counting from 0) of the column in which
* the part pointed to by @ref data() is located.
*/
int column()
{
return col;
}
/**
* @brief Returns the index (counting from 0) of the line within the current column
* this part belongs to.
*/
int line()
{
return target_line_in_block; // NOT line_in_block !!! It would be wrong if !hit_target_line
}
/**
* @brief Returns the length of the part pointed to by @ref data() in raw chars (not UTF-8 characters).
*/
int length()
{
return len;
}
/**
* @brief Returns the width in screen columns of the part pointed to by @ref data().
* Takes multi-byte UTF-8 sequences and wide characters into account.
*/
int screenLength()
{
return screenlen;
}
/**
* @brief Returns the current part of the iteration.
*/
const char* data()
{
return ptr;
}
};
/**
* @internal
* @brief Takes input and line wraps it, writing out one line at a time so that
* it can be interleaved with output from other columns.
*
* The LineWrapper is used to handle the last column of each table as well as interjections.
* The LineWrapper is called once for each line of output. If the data given to it fits
* into the designated width of the last column it is simply written out. If there
* is too much data, an appropriate split point is located and only the data up to this
* split point is written out. The rest of the data is queued for the next line.
* That way the last column can be line wrapped and interleaved with data from
* other columns. The following example makes this clearer:
* @code
* Column 1,1 Column 2,1 This is a long text
* Column 1,2 Column 2,2 that does not fit into
* a single line.
* @endcode
*
* The difficulty in producing this output is that the whole string
* "This is a long text that does not fit into a single line" is the
* 1st and only part of column 3. In order to produce the above
* output the string must be output piecemeal, interleaved with
* the data from the other columns.
*/
class LineWrapper
{
static const int bufmask = 15; //!< Must be a power of 2 minus 1.
/**
* @brief Ring buffer for length component of pair (data, length).
*/
int lenbuf[bufmask + 1];
/**
* @brief Ring buffer for data component of pair (data, length).
*/
const char* datbuf[bufmask + 1];
/**
* @brief The indentation of the column to which the LineBuffer outputs. LineBuffer
* assumes that the indentation has already been written when @ref process()
* is called, so this value is only used when a buffer flush requires writing
* additional lines of output.
*/
int x;
/**
* @brief The width of the column to line wrap.
*/
int width;
int head; //!< @brief index for next write
int tail; //!< @brief index for next read - 1 (i.e. increment tail BEFORE read)
/**
* @brief Multiple methods of LineWrapper may decide to flush part of the buffer to
* free up space. The contract of process() says that only 1 line is output. So
* this variable is used to track whether something has output a line. It is
* reset at the beginning of process() and checked at the end to decide if
* output has already occurred or is still needed.
*/
bool wrote_something;
bool buf_empty()
{
return ((tail + 1) & bufmask) == head;
}
bool buf_full()
{
return tail == head;
}
void buf_store(const char* data, int len)
{
lenbuf[head] = len;
datbuf[head] = data;
head = (head + 1) & bufmask;
}
//! @brief Call BEFORE reading ...buf[tail].
void buf_next()
{
tail = (tail + 1) & bufmask;
}
/**
* @brief Writes (data,len) into the ring buffer. If the buffer is full, a single line
* is flushed out of the buffer into @c write.
*/
void output(IStringWriter& write, const char* data, int len)
{
if (buf_full())
write_one_line(write);
buf_store(data, len);
}
/**
* @brief Writes a single line of output from the buffer to @c write.
*/
void write_one_line(IStringWriter& write)
{
if (wrote_something) // if we already wrote something, we need to start a new line
{
write("\n", 1);
int _ = 0;
indent(write, _, x);
}
if (!buf_empty())
{
buf_next();
write(datbuf[tail], lenbuf[tail]);
}
wrote_something = true;
}
public:
/**
* @brief Writes out all remaining data from the LineWrapper using @c write.
* Unlike @ref process() this method indents all lines including the first and
* will output a \\n at the end (but only if something has been written).
*/
void flush(IStringWriter& write)
{
if (buf_empty())
return;
int _ = 0;
indent(write, _, x);
wrote_something = false;
while (!buf_empty())
write_one_line(write);
write("\n", 1);
}
/**
* @brief Process, wrap and output the next piece of data.
*
* process() will output at least one line of output. This is not necessarily
* the @c data passed in. It may be data queued from a prior call to process().
* If the internal buffer is full, more than 1 line will be output.
*
* process() assumes that the a proper amount of indentation has already been
* output. It won't write any further indentation before the 1st line. If
* more than 1 line is written due to buffer constraints, the lines following
* the first will be indented by this method, though.
*
* No \\n is written by this method after the last line that is written.
*
* @param write where to write the data.
* @param data the new chunk of data to write.
* @param len the length of the chunk of data to write.
*/
void process(IStringWriter& write, const char* data, int len)
{
wrote_something = false;
while (len > 0)
{
if (len <= width) // quick test that works because utf8width <= len (all wide chars have at least 2 bytes)
{
output(write, data, len);
len = 0;
}
else // if (len > width) it's possible (but not guaranteed) that utf8len > width
{
int utf8width = 0;
int maxi = 0;
while (maxi < len && utf8width < width)
{
int charbytes = 1;
unsigned ch = (unsigned char) data[maxi];
if (ch > 0xC1) // everything <= 0xC1 (yes, even 0xC1 itself) is not a valid UTF-8 start byte
{
// int __builtin_clz (unsigned int x)
// Returns the number of leading 0-bits in x, starting at the most significant bit
unsigned mask = (unsigned) -1 >> __builtin_clz(ch ^ 0xff);
ch = ch & mask; // mask out length bits, we don't verify their correctness
while ((maxi + charbytes < len) && //
(((unsigned char) data[maxi + charbytes] ^ 0x80) <= 0x3F)) // while next byte is continuation byte
{
ch = (ch << 6) ^ (unsigned char) data[maxi + charbytes] ^ 0x80; // add continuation to char code
++charbytes;
}
// ch is the decoded unicode code point
if (ch >= 0x1100 && isWideChar(ch)) // the test for 0x1100 is here to avoid the function call in the Latin case
{
if (utf8width + 2 > width)
break;
++utf8width;
}
}
++utf8width;
maxi += charbytes;
}
// data[maxi-1] is the last byte of the UTF-8 sequence of the last character that fits
// onto the 1st line. If maxi == len, all characters fit on the line.
if (maxi == len)
{
output(write, data, len);
len = 0;
}
else // if (maxi < len) at least 1 character (data[maxi] that is) doesn't fit on the line
{
int i;
for (i = maxi; i >= 0; --i)
if (data[i] == ' ')
break;
if (i >= 0)
{
output(write, data, i);
data += i + 1;
len -= i + 1;
}
else // did not find a space to split at => split before data[maxi]
{ // data[maxi] is always the beginning of a character, never a continuation byte
output(write, data, maxi);
data += maxi;
len -= maxi;
}
}
}
}
if (!wrote_something) // if we didn't already write something to make space in the buffer
write_one_line(write); // write at most one line of actual output
}
/**
* @brief Constructs a LineWrapper that wraps its output to fit into
* screen columns @c x1 (incl.) to @c x2 (excl.).
*
* @c x1 gives the indentation LineWrapper uses if it needs to indent.
*/
LineWrapper(int x1, int x2) :
x(x1), width(x2 - x1), head(0), tail(bufmask)
{
if (width < 2) // because of wide characters we need at least width 2 or the code breaks
width = 2;
}
};
/**
* @internal
* @brief This is the implementation that is shared between all printUsage() templates.
* Because all printUsage() templates share this implementation, there is no template bloat.
*/
static void printUsage(IStringWriter& write, const Descriptor usage[], int width = 80, //
int last_column_min_percent = 50, int last_column_own_line_max_percent = 75)
{
if (width < 1) // protect against nonsense values
width = 80;
if (width > 10000) // protect against overflow in the following computation
width = 10000;
int last_column_min_width = ((width * last_column_min_percent) + 50) / 100;
int last_column_own_line_max_width = ((width * last_column_own_line_max_percent) + 50) / 100;
if (last_column_own_line_max_width == 0)
last_column_own_line_max_width = 1;
LinePartIterator part(usage);
while (part.nextTable())
{
/***************** Determine column widths *******************************/
const int maxcolumns = 8; // 8 columns are enough for everyone
int col_width[maxcolumns];
int lastcolumn;
int leftwidth;
int overlong_column_threshold = 10000;
do
{
lastcolumn = 0;
for (int i = 0; i < maxcolumns; ++i)
col_width[i] = 0;
part.restartTable();
while (part.nextRow())
{
while (part.next())
{
if (part.column() < maxcolumns)
{
upmax(lastcolumn, part.column());
if (part.screenLength() < overlong_column_threshold)
// We don't let rows that don't use table separators (\t or \v) influence
// the width of column 0. This allows the user to interject section headers
// or explanatory paragraphs that do not participate in the table layout.
if (part.column() > 0 || part.line() > 0 || part.data()[part.length()] == '\t'
|| part.data()[part.length()] == '\v')
upmax(col_width[part.column()], part.screenLength());
}
}
}
/*
* If the last column doesn't fit on the same
* line as the other columns, we can fix that by starting it on its own line.
* However we can't do this for any of the columns 0..lastcolumn-1.
* If their sum exceeds the maximum width we try to fix this by iteratively
* ignoring the widest line parts in the width determination until
* we arrive at a series of column widths that fit into one line.
* The result is a layout where everything is nicely formatted
* except for a few overlong fragments.
* */
leftwidth = 0;
overlong_column_threshold = 0;
for (int i = 0; i < lastcolumn; ++i)
{
leftwidth += col_width[i];
upmax(overlong_column_threshold, col_width[i]);
}
} while (leftwidth > width);
/**************** Determine tab stops and last column handling **********************/
int tabstop[maxcolumns];
tabstop[0] = 0;
for (int i = 1; i < maxcolumns; ++i)
tabstop[i] = tabstop[i - 1] + col_width[i - 1];
int rightwidth = width - tabstop[lastcolumn];
bool print_last_column_on_own_line = false;
if (rightwidth < last_column_min_width && // if we don't have the minimum requested width for the last column
( col_width[lastcolumn] == 0 || // and all last columns are > overlong_column_threshold
rightwidth < col_width[lastcolumn] // or there is at least one last column that requires more than the space available
)
)
{
print_last_column_on_own_line = true;
rightwidth = last_column_own_line_max_width;
}
// If lastcolumn == 0 we must disable print_last_column_on_own_line because
// otherwise 2 copies of the last (and only) column would be output.
// Actually this is just defensive programming. It is currently not
// possible that lastcolumn==0 and print_last_column_on_own_line==true
// at the same time, because lastcolumn==0 => tabstop[lastcolumn] == 0 =>
// rightwidth==width => rightwidth>=last_column_min_width (unless someone passes
// a bullshit value >100 for last_column_min_percent) => the above if condition
// is false => print_last_column_on_own_line==false
if (lastcolumn == 0)
print_last_column_on_own_line = false;
LineWrapper lastColumnLineWrapper(width - rightwidth, width);
LineWrapper interjectionLineWrapper(0, width);
part.restartTable();
/***************** Print out all rows of the table *************************************/
while (part.nextRow())
{
int x = -1;
while (part.next())
{
if (part.column() > lastcolumn)
continue; // drop excess columns (can happen if lastcolumn == maxcolumns-1)
if (part.column() == 0)
{
if (x >= 0)
write("\n", 1);
x = 0;
}
indent(write, x, tabstop[part.column()]);
if ((part.column() < lastcolumn)
&& (part.column() > 0 || part.line() > 0 || part.data()[part.length()] == '\t'
|| part.data()[part.length()] == '\v'))
{
write(part.data(), part.length());
x += part.screenLength();
}
else // either part.column() == lastcolumn or we are in the special case of
// an interjection that doesn't contain \v or \t
{
// NOTE: This code block is not necessarily executed for
// each line, because some rows may have fewer columns.
LineWrapper& lineWrapper = (part.column() == 0) ? interjectionLineWrapper : lastColumnLineWrapper;
if (!print_last_column_on_own_line || part.column() != lastcolumn)
lineWrapper.process(write, part.data(), part.length());
}
} // while
if (print_last_column_on_own_line)
{
part.restartRow();
while (part.next())
{
if (part.column() == lastcolumn)
{
write("\n", 1);
int _ = 0;
indent(write, _, width - rightwidth);
lastColumnLineWrapper.process(write, part.data(), part.length());
}
}
}
write("\n", 1);
lastColumnLineWrapper.flush(write);
interjectionLineWrapper.flush(write);
}
}
}
}
;
/**
* @brief Outputs a nicely formatted usage string with support for multi-column formatting
* and line-wrapping.
*
* printUsage() takes the @c help texts of a Descriptor[] array and formats them into
* a usage message, wrapping lines to achieve the desired output width.
*
* <b>Table formatting:</b>
*
* Aside from plain strings which are simply line-wrapped, the usage may contain tables. Tables
* are used to align elements in the output.
*
* @code
* // Without a table. The explanatory texts are not aligned.
* -c, --create |Creates something.
* -k, --kill |Destroys something.
*
* // With table formatting. The explanatory texts are aligned.
* -c, --create |Creates something.
* -k, --kill |Destroys something.
* @endcode
*
* Table formatting removes the need to pad help texts manually with spaces to achieve
* alignment. To create a table, simply insert \\t (tab) characters to separate the cells
* within a row.
*
* @code
* const option::Descriptor usage[] = {
* {..., "-c, --create \tCreates something." },
* {..., "-k, --kill \tDestroys something." }, ...
* @endcode
*
* Note that you must include the minimum amount of space desired between cells yourself.
* Table formatting will insert further spaces as needed to achieve alignment.
*
* You can insert line breaks within cells by using \\v (vertical tab).
*
* @code
* const option::Descriptor usage[] = {
* {..., "-c,\v--create \tCreates\vsomething." },
* {..., "-k,\v--kill \tDestroys\vsomething." }, ...
*
* // results in
*
* -c, Creates
* --create something.
* -k, Destroys
* --kill something.
* @endcode
*
* You can mix lines that do not use \\t or \\v with those that do. The plain
* lines will not mess up the table layout. Alignment of the table columns will
* be maintained even across these interjections.
*
* @code
* const option::Descriptor usage[] = {
* {..., "-c, --create \tCreates something." },
* {..., "----------------------------------" },
* {..., "-k, --kill \tDestroys something." }, ...
*
* // results in
*
* -c, --create Creates something.
* ----------------------------------
* -k, --kill Destroys something.
* @endcode
*
* You can have multiple tables within the same usage whose columns are
* aligned independently. Simply insert a dummy Descriptor with @c help==0.
*
* @code
* const option::Descriptor usage[] = {
* {..., "Long options:" },
* {..., "--very-long-option \tDoes something long." },
* {..., "--ultra-super-mega-long-option \tTakes forever to complete." },
* {..., 0 }, // ---------- table break -----------
* {..., "Short options:" },
* {..., "-s \tShort." },
* {..., "-q \tQuick." }, ...
*
* // results in
*
* Long options:
* --very-long-option Does something long.
* --ultra-super-mega-long-option Takes forever to complete.
* Short options:
* -s Short.
* -q Quick.
*
* // Without the table break it would be
*
* Long options:
* --very-long-option Does something long.
* --ultra-super-mega-long-option Takes forever to complete.
* Short options:
* -s Short.
* -q Quick.
* @endcode
*
* <b>Output methods:</b>
*
* Because TheLeanMeanC++Option parser is freestanding, you have to provide the means for
* output in the first argument(s) to printUsage(). Because printUsage() is implemented as
* a set of template functions, you have great flexibility in your choice of output
* method. The following example demonstrates typical uses. Anything that's similar enough
* will work.
*
* @code
* #include <unistd.h> // write()
* #include <iostream> // cout
* #include <sstream> // ostringstream
* #include <cstdio> // fwrite()
* using namespace std;
*
* void my_write(const char* str, int size) {
* fwrite(str, size, 1, stdout);
* }
*
* struct MyWriter {
* void write(const char* buf, size_t size) const {
* fwrite(str, size, 1, stdout);
* }
* };
*
* struct MyWriteFunctor {
* void operator()(const char* buf, size_t size) {
* fwrite(str, size, 1, stdout);
* }
* };
* ...
* printUsage(my_write, usage); // custom write function
* printUsage(MyWriter(), usage); // temporary of a custom class
* MyWriter writer;
* printUsage(writer, usage); // custom class object
* MyWriteFunctor wfunctor;
* printUsage(&wfunctor, usage); // custom functor
* printUsage(write, 1, usage); // write() to file descriptor 1
* printUsage(cout, usage); // an ostream&
* printUsage(fwrite, stdout, usage); // fwrite() to stdout
* ostringstream sstr;
* printUsage(sstr, usage); // an ostringstream&
*
* @endcode
*
* @par Notes:
* @li the @c write() method of a class that is to be passed as a temporary
* as @c MyWriter() is in the example, must be a @c const method, because
* temporary objects are passed as const reference. This only applies to
* temporary objects that are created and destroyed in the same statement.
* If you create an object like @c writer in the example, this restriction
* does not apply.
* @li a functor like @c MyWriteFunctor in the example must be passed as a pointer.
* This differs from the way functors are passed to e.g. the STL algorithms.
* @li All printUsage() templates are tiny wrappers around a shared non-template implementation.
* So there's no penalty for using different versions in the same program.
* @li printUsage() always interprets Descriptor::help as UTF-8 and always produces UTF-8-encoded
* output. If your system uses a different charset, you must do your own conversion. You
* may also need to change the font of the console to see non-ASCII characters properly.
* This is particularly true for Windows.
* @li @b Security @b warning: Do not insert untrusted strings (such as user-supplied arguments)
* into the usage. printUsage() has no protection against malicious UTF-8 sequences.
*
* @param prn The output method to use. See the examples above.
* @param usage the Descriptor[] array whose @c help texts will be formatted.
* @param width the maximum number of characters per output line. Note that this number is
* in actual characters, not bytes. printUsage() supports UTF-8 in @c help and will
* count multi-byte UTF-8 sequences properly. Asian wide characters are counted
* as 2 characters.
* @param last_column_min_percent (0-100) The minimum percentage of @c width that should be available
* for the last column (which typically contains the textual explanation of an option).
* If less space is available, the last column will be printed on its own line, indented
* according to @c last_column_own_line_max_percent.
* @param last_column_own_line_max_percent (0-100) If the last column is printed on its own line due to
* less than @c last_column_min_percent of the width being available, then only
* @c last_column_own_line_max_percent of the extra line(s) will be used for the
* last column's text. This ensures an indentation. See example below.
*
* @code
* // width=20, last_column_min_percent=50 (i.e. last col. min. width=10)
* --3456789 1234567890
* 1234567890
*
* // width=20, last_column_min_percent=75 (i.e. last col. min. width=15)
* // last_column_own_line_max_percent=75
* --3456789
* 123456789012345
* 67890
*
* // width=20, last_column_min_percent=75 (i.e. last col. min. width=15)
* // last_column_own_line_max_percent=33 (i.e. max. 5)
* --3456789
* 12345
* 67890
* 12345
* 67890
* @endcode
*/
template<typename OStream>
void printUsage(OStream& prn, const Descriptor usage[], int width = 80, int last_column_min_percent = 50,
int last_column_own_line_max_percent = 75)
{
PrintUsageImplementation::OStreamWriter<OStream> write(prn);
PrintUsageImplementation::printUsage(write, usage, width, last_column_min_percent, last_column_own_line_max_percent);
}
template<typename Function>
void printUsage(Function* prn, const Descriptor usage[], int width = 80, int last_column_min_percent = 50,
int last_column_own_line_max_percent = 75)
{
PrintUsageImplementation::FunctionWriter<Function> write(prn);
PrintUsageImplementation::printUsage(write, usage, width, last_column_min_percent, last_column_own_line_max_percent);
}
template<typename Temporary>
void printUsage(const Temporary& prn, const Descriptor usage[], int width = 80, int last_column_min_percent = 50,
int last_column_own_line_max_percent = 75)
{
PrintUsageImplementation::TemporaryWriter<Temporary> write(prn);
PrintUsageImplementation::printUsage(write, usage, width, last_column_min_percent, last_column_own_line_max_percent);
}
template<typename Syscall>
void printUsage(Syscall* prn, int fd, const Descriptor usage[], int width = 80, int last_column_min_percent = 50,
int last_column_own_line_max_percent = 75)
{
PrintUsageImplementation::SyscallWriter<Syscall> write(prn, fd);
PrintUsageImplementation::printUsage(write, usage, width, last_column_min_percent, last_column_own_line_max_percent);
}
template<typename Function, typename Stream>
void printUsage(Function* prn, Stream* stream, const Descriptor usage[], int width = 80, int last_column_min_percent =
50,
int last_column_own_line_max_percent = 75)
{
PrintUsageImplementation::StreamWriter<Function, Stream> write(prn, stream);
PrintUsageImplementation::printUsage(write, usage, width, last_column_min_percent, last_column_own_line_max_percent);
}
}
// namespace option
#endif /* OPTIONPARSER_H_ */