argListHelp.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2018-2021 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "argList.H"
29#include "foamVersion.H"
30#include "stringOps.H"
31#include "IOmanip.H"
32
33// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
34
35namespace Foam
36{
37
38// Convert api number (YYMM) -> 20YY. Eg, 1906 -> 2019
39static inline int apiYear()
40{
41 return 2000 + (foamVersion::api / 100);
42}
43
44
45// Footer for manpage
46static inline void printManFooter()
47{
48 Info<< ".SH \"SEE ALSO\"" << nl
49 << "Online documentation "
50 << "https://www.openfoam.com/documentation/" << nl
51 << ".SH COPYRIGHT" << nl
52 << "Copyright \\(co 2018-" << apiYear() << " OpenCFD Ltd." << nl;
53}
54
55
56// Option output (manpage formatted)
57static inline void printManOption
58(
59 const word& optName,
60 const string& optArg,
61 const string& optUsage
62)
63{
64 Info<< ".TP\n\\fB\\-" << optName << "\\fR";
65
66 if (optArg.size())
67 {
68 Info<< " \\fI" << optArg.c_str() << "\\fR";
69 }
70 Info<< nl;
71
72 if (optUsage.size())
73 {
75 }
76 else
77 {
78 Info<< nl;
79 }
80}
81
82
83// Bool option output (manpage formatted)
84static inline void printManOption
85(
86 const word& optName,
87 const string& optUsage
88)
89{
90 printManOption(optName, string::null, optUsage);
91}
92
93
94// Option output (manpage formatted)
95// - uses static HashTables to obtain values
96static void printManOption(const word& optName)
97{
99 (
100 optName,
103 );
104
105 if (argList::validParOptions.found(optName))
106 {
107 Info<< "\\fB[Parallel option]\\fR" << nl;
108 }
109}
110
111
112// Wrapped output with initial start column
114(
115 std::string::size_type start,
116 const string& str
117)
118{
119 if (str.empty())
120 {
121 Info<< nl;
122 return;
123 }
124
125 // Indent the first line. Min 2 spaces between option and usage
126 if (start + 2 > argList::usageMin)
127 {
128 Info<< nl;
129 start = 0;
130 }
131 while (start < argList::usageMin)
132 {
133 Info<< ' ';
134 ++start;
135 }
136
138 (
139 Info,
140 str,
143 );
144}
145
146
147// Option output (usage formatted)
148static inline void printOption
149(
150 const word& optName,
151 const string& optArg,
152 const string& optUsage
153)
154{
155 Info<< " -" << optName;
156
157 // Length with leading ' -'
158 std::string::size_type len = optName.size() + 3;
159
160 if (optArg.size())
161 {
162 Info<< " <" << optArg.c_str() << '>';
163
164 // Length with space between option/param and '<>'
165 len += optArg.size() + 3;
166 }
167
168 printOptionUsage(len, optUsage);
169}
170
171
172// Bool option output (usage formatted)
173static inline void printOption
174(
175 const word& optName,
176 const string& optUsage
177)
178{
179 printOption(optName, string::null, optUsage);
180}
181
182
183// Option output (usage formatted)
184// - uses static HashTables to obtain values
185static void printOption(const word& optName)
186{
188 (
189 optName,
192 );
193}
194
195} // End namespace Foam
196
197
198// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
199
201{
202 // .TH "<APPLICATION>" 1 "OpenFOAM-<version>" "source" "category"
203
204 Info<< ".TH" << token::SPACE
205 // All uppercase (returns a Foam::string) and thus also quoted
206 << stringOps::upper(executable_) << token::SPACE
207 << 1 << token::SPACE
208 << token::DQUOTE << "OpenFOAM-v" << foamVersion::api << token::DQUOTE
209 << token::SPACE
210 << token::DQUOTE << "www.openfoam.com" << token::DQUOTE
211 << token::SPACE
212 << token::DQUOTE << "OpenFOAM Commands Manual" << token::DQUOTE
213 << nl;
214
215
216 // .SH NAME
217 // <application> \- part of OpenFOAM (The Open Source CFD Toolbox).
218 Info<< ".SH \"NAME\"" << nl
219 << executable_
220 << " \\- part of \\fBOpenFOAM\\fR (The Open Source CFD Toolbox)."
221 << nl;
222
223
224 // .SH SYNOPSIS
225 // .B command [OPTIONS] ...
226
227 Info<< ".SH \"SYNOPSIS\"" << nl
228 << "\\fB" << executable_ << "\\fR [\\fIOPTIONS\\fR]";
229
230 if (validArgs.size())
231 {
232 Info<< ' ';
233
234 if (!argsMandatory_)
235 {
236 Info<< '[';
237 }
238
239 label i = 0;
240 for (const std::string& argName : validArgs)
241 {
242 if (i++) Info<< ' ';
243 Info << "\\fI" << argName.c_str() << "\\fR";
244 }
245
246 if (!argsMandatory_)
247 {
248 Info<< ']';
249 }
250 }
251 Info<< nl;
252
253
254 // .SH DESCRIPTION
255 {
256 Info<< ".SH \"DESCRIPTION\"" << nl;
257
258 Info<< ".nf" << nl; // No fill lines
259
260 if (notes.empty())
261 {
262 Info<< "No description available\n";
263 }
264 else
265 {
266 for (const std::string& note : notes)
267 {
268 if (note.empty())
269 {
270 Info<< nl;
271 }
272 else
273 {
274 stringOps::writeWrapped(Info, note, usageMax, 0, true);
275 }
276 }
277 }
278 Info<< ".fi" << nl; // Fill lines
279 }
280
281
282 // Arguments output
283 if (validArgs.size())
284 {
285 // .SH "ARGUMENTS"
286 Info<< ".SH \"ARGUMENTS\"" << nl;
287
288 label argIndex = 0;
289 for (const std::string& argName : validArgs)
290 {
291 ++argIndex;
292
293 Info<< ".TP\n\\fI" << argName.c_str() << "\\fR";
294 Info<< nl;
295
296 const string& text =
297 argList::argUsage.lookup(argIndex, string::null);
298
299 if (text.size())
300 {
301 stringOps::writeWrapped(Info, text, usageMax, 0, true);
302 }
303 else
304 {
305 Info<< nl;
306 }
307 }
308 }
309
310
311 // .SH "OPTIONS"
312 Info<< ".SH \"OPTIONS\"" << nl;
313
314 for (const word& optName : validOptions.sortedToc())
315 {
316 // Normal (non-advanced) options
317 if (!advancedOptions.found(optName))
318 {
319 printManOption(optName);
320 }
321 }
322
323 // Standard documentation/help options
324
325 printManOption("doc", "Display documentation in browser");
326 printManOption("help", "Display short help and exit");
327 printManOption("help-full", "Display full help and exit");
328
329
330 // .SS "ADVANCED OPTIONS"
331 Info<< ".SS \"ADVANCED OPTIONS\"" << nl;
332
333 for (const word& optName : validOptions.sortedToc())
334 {
335 // Advanced options
336 if (advancedOptions.found(optName))
337 {
338 printManOption(optName);
339 }
340 }
341
342 printManOption("doc-source", "Display source code in browser");
343
344
345 const bool hasCompat =
346 (
349 );
350
351 if (hasCompat)
352 {
353 printManOption("help-compat", "Display compatibility options and exit");
354 }
355
356 printManOption("help-man", "Display full help (manpage format) and exit");
357 printManOption("help-notes", "Display help notes (description) and exit");
358
359 printManCompat();
360
361 // Footer
363}
364
365
366void Foam::argList::printUsage(bool full) const
367{
368 Info<< "\nUsage: " << executable_ << " [OPTIONS]";
369
370 if (validArgs.size())
371 {
372 Info<< ' ';
373
374 if (!argsMandatory_)
375 {
376 Info<< '[';
377 }
378
379 label i = 0;
380 for (const std::string& argName : validArgs)
381 {
382 if (i++) Info<< ' ';
383 Info<< '<' << argName.c_str() << '>';
384 }
385
386 if (!argsMandatory_)
387 {
388 Info<< ']';
389 }
390 }
391 Info<< nl;
392
393 // Arguments output
394 // Currently only if there is also usage information, but may wish to
395 // change this to remind developers to add some description.
396 if (validArgs.size() && argUsage.size())
397 {
398 Info<< "Arguments:\n";
399
400 label argIndex = 0;
401 for (const std::string& argName : validArgs)
402 {
403 ++argIndex;
404
405 Info<< " <" << argName.c_str() << '>';
406
408 (
409 // Length with leading spaces and surround '<>'
410 (argName.size() + 4),
411 argList::argUsage.lookup(argIndex, string::null)
412 );
413 }
414 }
415
416 Info<< "Options:\n";
417
418 for (const word& optName : validOptions.sortedToc())
419 {
420 // Suppress advanced options for regular -help.
421 if (full || !advancedOptions.found(optName))
422 {
423 printOption(optName);
424 }
425 }
426
427
428 // Place documentation/help options at the end
429
430 printOption("doc", "Display documentation in browser");
431 if (full)
432 {
433 printOption("doc-source", "Display source code in browser");
434 }
435 printOption("help", "Display short help and exit");
436
437 if
438 (
441 )
442 {
443 printOption("help-compat", "Display compatibility options and exit");
444 }
445
446 if (full)
447 {
448 printOption("help-man", "Display full help (manpage format) and exit");
449 printOption("help-notes", "Display help notes (description) and exit");
450 }
451
452 printOption("help-full", "Display full help and exit");
453
454 printNotes();
455
456 Info<< nl;
458 Info<< endl;
459}
460
461
463{
464 // Output notes with automatic text wrapping
465 if (!notes.empty())
466 {
467 Info<< nl;
468
469 for (const std::string& note : notes)
470 {
471 if (note.empty())
472 {
473 Info<< nl;
474 }
475 else
476 {
477 stringOps::writeWrapped(Info, note, usageMax);
478 }
479 }
480 }
481}
482
483
484void Foam::argList::printManCompat() const
485{
486 if
487 (
490 )
491 {
492 return;
493 }
494
495
496 // .SS "COMPATIBILITY OPTIONS"
497 Info<< ".SS \"COMPATIBILITY OPTIONS\"" << nl;
498
499 for (const word& k : argList::validOptionsCompat.sortedToc())
500 {
501 const auto& iter = *argList::validOptionsCompat.cfind(k);
502
503 const word& optName = iter.first;
504 const int until = abs(iter.second);
505
506 Info<< ".TP\n\\fB\\-" << k
507 << "\\fR (now \\fB\\-" << optName << "\\fR)" << nl;
508
509 if (until)
510 {
511 Info<< "The option was last used in " << until << "." << nl;
512 }
513 }
514
515 for (const word& k : argList::ignoreOptionsCompat.sortedToc())
516 {
517 const auto& iter = *argList::ignoreOptionsCompat.cfind(k);
518
519 const bool hasArg = iter.first;
520 const int until = abs(iter.second);
521
522 Info<< ".TP\n\\fB\\-" << k << "\\fR";
523
524 if (hasArg)
525 {
526 Info<< " \\fIarg\\fR";
527 }
528
529 Info<< nl << "This option is ignored";
530
531 if (until)
532 {
533 Info<< " after " << until << ".";
534 }
535 Info<< nl;
536 }
537}
538
539
541{
542 const label nopt
543 (
546 );
547
548 Info<< nopt << " compatibility options for " << executable_ << nl;
549
550 if (!nopt)
551 {
552 return;
553 }
554
555 const int col1(32), col2(32);
556
557 Info<< nl
558 << "|" << setf(ios_base::left) << setw(col1) << " Old option"
559 << "|" << setf(ios_base::left) << setw(col2) << " New option"
560 << "| Comment" << nl;
561
562 Info<< setfill('-');
563 Info<< "|" << setf(ios_base::left) << setw(col1) << ""
564 << "|" << setf(ios_base::left) << setw(col2) << ""
565 << "|------------" << nl;
566
568
569 for (const word& k : argList::validOptionsCompat.sortedToc())
570 {
571 const auto& iter = *argList::validOptionsCompat.cfind(k);
572
573 const word& optName = iter.first;
574 const int until = abs(iter.second);
575
576 Info<< "| -" << setf(ios_base::left) << setw(col1-2) << k
577 << "| -" << setf(ios_base::left) << setw(col2-2) << optName
578 << "|";
579
580 if (until)
581 {
582 Info<< " until " << until;
583 }
584 Info<< nl;
585 }
586
587 for (const word& k : argList::ignoreOptionsCompat.sortedToc())
588 {
589 const auto& iter = *argList::ignoreOptionsCompat.cfind(k);
590
591 const bool hasArg = iter.first;
592 const int until = abs(iter.second);
593
594 Info<< "| -" << setf(ios_base::left) << setw(col1-2);
595
596 if (hasArg)
597 {
598 Info<< (k + " <arg>").c_str();
599 }
600 else
601 {
602 Info<< k;
603 }
604
605 Info<< "| ";
606 Info<< setf(ios_base::left) << setw(col2-1) << "ignored" << "|";
607 if (until)
608 {
609 Info<< " after " << until;
610 }
611 Info<< nl;
612 }
613
614 Info<< setfill('-');
615 Info<< "|" << setf(ios_base::left) << setw(col1) << ""
616 << "|" << setf(ios_base::left) << setw(col2) << ""
617 << "|------------" << nl;
618
620}
621
622
623// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
label k
bool found
bool empty() const noexcept
True if the hash table is empty.
Definition: HashTableI.H:59
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition: HashTableI.H:141
static HashTable< string, label, Hash< label > > argUsage
Short description for program arguments.
Definition: argList.H:226
static std::string::size_type usageMin
Min indentation when displaying usage (default: 20)
Definition: argList.H:235
static HashTable< std::pair< word, int > > validOptionsCompat
A list of aliases for options.
Definition: argList.H:219
static HashTable< string > validParOptions
A list of valid parallel options.
Definition: argList.H:215
static std::string::size_type usageMax
Max screen width when displaying usage (default: 80)
Definition: argList.H:238
void printUsage(bool full=true) const
Print usage.
Definition: argListHelp.C:366
static HashTable< string > optionUsage
Short description for validOptions.
Definition: argList.H:229
static HashSet< string > advancedOptions
The "advanced" options are shown with -help-full (not with –help)
Definition: argList.H:209
static SLList< string > notes
General usage notes.
Definition: argList.H:232
void printCompat() const
Print option compatibility.
Definition: argListHelp.C:540
void printMan() const
Print usage as nroff-man format (Experimental)
Definition: argListHelp.C:200
void printNotes() const
Print notes (if any)
Definition: argListHelp.C:462
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:212
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:206
static HashTable< std::pair< bool, int > > ignoreOptionsCompat
A list of options to ignore.
Definition: argList.H:223
std::ostream & stdStream()
Return std::ostream for output operations.
Lookup type of boundary radiation properties.
Definition: lookup.H:66
@ DQUOTE
Double quote.
Definition: token.H:135
@ SPACE
Space [isspace].
Definition: token.H:125
A class for handling words, derived from Foam::string.
Definition: word.H:68
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to 'true' entries.
Definition: BitOps.C:203
void printBuildInfo(std::ostream &os, const bool full=true)
Print information about version, build, arch to output stream.
void writeWrapped(OSstream &os, const std::string &str, const std::string::size_type width, const std::string::size_type indent=0, const bool escape=false)
Output string with text wrapping.
Definition: stringOps.C:1217
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1200
Namespace for OpenFOAM.
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:169
static void printOption(const word &optName, const string &optArg, const string &optUsage)
Definition: argListHelp.C:149
static void printManFooter()
Definition: argListHelp.C:46
static void printManOption(const word &optName, const string &optArg, const string &optUsage)
Definition: argListHelp.C:58
messageStream Info
Information stream (stdout output on master, null elsewhere)
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
static void printOptionUsage(std::string::size_type start, const string &str)
Definition: argListHelp.C:114
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
static int apiYear()
Definition: argListHelp.C:39
Omanip< char > setfill(char fillch)
Definition: IOmanip.H:175
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53