From 83418d96cf3a1f6b0861490221e8ed136a3a524b Mon Sep 17 00:00:00 2001 From: Igor Mironchik Date: Sat, 7 Aug 2021 08:36:03 +0300 Subject: [PATCH] Add possibility to set up custom help printer in help. --- README.md | 41 ++++++++++++++++++++++++++++++++++++----- args-parser/help.hpp | 9 +++++++++ doc/Doxyfile | 2 +- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6e001f45..02fd95d1 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,13 @@ as `StringList` # Q/A -Why not to add description, long description, etc into constructors of -arguments, so it will be possible to initialize argument in one line? +**Why not to add description, long description, etc into constructors of +arguments, so it will be possible to initialize argument in one line?** * This is impossible because constructors will be ambiguous but you can use auxiliary API that allows to define arguments in one line of code. -How can I add args-parser to my project? +**How can I add args-parser to my project?** * The simplest way is just copy args-parser directory with headers to any location in your project. With CMake you can clone entire `args-parser` project somewhere in your @@ -75,7 +75,7 @@ add include directory path to your project with case it will be possible to use `find_package( args-parser )` in CMakeLists.txt of your project. -Why should I use this library? +**Why should I use this library?** * There are tons of libraries for parsing command line arguments on the Internet. But args-parser also provides the possibility to define commands, like @@ -97,7 +97,7 @@ Internet. But args-parser also provides the possibility to define commands, like that library can do? This library is very flexible with powerfull help printing. What do you need more? -How to print help programmatically? +**How to print help programmatically?** * For those, who use old style syntax the answer should be on the plate, as developer can look at the code of `Help` class, that implements help argument. But for those, who @@ -124,6 +124,33 @@ what syntax you use, new (one-line) syntax is just a wrapper for old one. I.e. l printer.print( outStream() ); ``` +**Why doesn't args-parser provide bindings of variables to arguments?** + + * This is a question of why doesn't `args-parser` provide validators? It's the same. +I decided that this aspect is very application specific. There is no need for such library +to do any conversions of arguments' values, to bind them to some variables. This will +do API very complex only. I know what you will say: this is very nice feature, it helps... +Really? How often and hom much it helped you? Arguments parser should handle the string +that user provided to the application, it should separate arguments, commands, values, +store it in internal state of parser, and allow to developer just write some `if` +operators to choose branch of the programm logic. What will give you, as to developer, +if values will be bind to concrete variables? Will not you write the same code with `if` +operators? So why I should do the library more complex? + +**Why don't you provide comparisons with othe CLI libraries?** + + * I found only one library at GitHub that can compete with `args-parser`, and this is +[CLI11](https://github.com/CLIUtils/CLI11). And here is the question of the taste more. +But `CLI11` can handle commands as usual arguments, it's doesn't matter how much times +they presend in command line, whereas 'args-parser' handles commands as commands. Theirs +approach possibly more flexible, but when I designed `args-parser` I thought on commands +as on some action to do in application's logic, whereas arguments are for data. I can do +the same, but is it needed? + + `CLI11` has possibility to set formatter of the help, `args-parser` allow to set + custom `HelpPrinterIface` on the `Help` argument. But who and when will do it? + And I believe that help in `args-parser` is a little better than in `CLI11`. + # Changelog * 6.0.0.0 @@ -142,6 +169,10 @@ what syntax you use, new (one-line) syntax is just a wrapper for old one. I.e. l Improved API with new syntax, now it's impossible to mess with end() methods. Fixed issue with printing help of global argument under command. + * 6.1.1.1 + + Added possibility to set up custom help printer in help argument. + # Example First of all you must know that practically all classes of the args-parser throws exceptions on errors diff --git a/args-parser/help.hpp b/args-parser/help.hpp index ae39e199..df8ce0fe 100644 --- a/args-parser/help.hpp +++ b/args-parser/help.hpp @@ -86,6 +86,9 @@ class Help final //! Set line length for the help. void setLineLength( String::size_type length ); + //! Set printer. + void setPrinter( std::unique_ptr< HelpPrinterIface > p ); + protected: /*! Process argument's staff, for example take values from @@ -134,6 +137,12 @@ Help::setLineLength( String::size_type length ) m_printer->setLineLength( length ); } +inline void +Help::setPrinter( std::unique_ptr< HelpPrinterIface > p ) +{ + m_printer.reset( p.release() ); +} + inline void Help::process( Context & context ) { diff --git a/doc/Doxyfile b/doc/Doxyfile index 10c15622..e8bba503 100644 --- a/doc/Doxyfile +++ b/doc/Doxyfile @@ -38,7 +38,7 @@ PROJECT_NAME = args-parser # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 6.1.1.0 +PROJECT_NUMBER = 6.1.1.1 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a