An easy-to-use C++ command line analyzer, SmpCommandLine
Everything starts with simplicity. This article introduces a simple command line analyzer, SmpCommandLine.
SmpCommandLine is a lightweight C++ package that extracts parameters that users enter on the command line.
There are already many command line analysis tools in the world, such as getopt from the C standard library, gflags from Google's C++ library, and many of them are easy-to-use libraries, but many C/C++ programmers have a "quirk" that likes to reinvent and make wheels.
SmpCommandLine is easy to use, but it currently only supports C++ languages (and has only been tested on Ubuntu for the time being, but is likely to work on Windows as well). This tool only contains a short header file, the code can be used in Get on github
1. Usage of minimalist version
What is really simple and easy to use, the explanatory documents should also be concise and understandable, otherwise you can write your own plan just by reading and understanding the explanatory documents. For a simple enough tool program, the best documentation is to go straight to a sample code.
Example usage of the minimalist version of SmpCommandLine:
#include "SmpCommandLine.hpp" int main( int argc, char *argv[] ) { SmpCommandLine commands( argc, argv ); int imageWidth = commands.getInteger( "w", NULL ); bool bShowImage = commands.getBoolean( "s", NULL ); std::string outputFileName = commands.getString( "o", NULL ); std::string inputFileName = commands.getString( 1 ); // ... }
You just need to go from GitHub Download SmpCommandLine.hpp , put SmpCommandLine. Put HPP in your C++ project, include it in the cpp file where your main function is located, and add a few lines of code like the example above to the main function header to get the parameters you want from the user's command line input. In the example code above, first create a SmpCommandLine object, note that in the constructor you pass in two parameters, arc and argv, from the main entry, and then call
getInteger( "w", ...) getBoolean( "s", ...) getString( "o", ...)
Methods such as extracting parameters with -w, -s, -o flags, etc. in the method name,... Integer,... Boolean,... String represents the type of value returned by the method. Last call
getString( 1, ... )
Extract the first unlabeled parameter. After the program is compiled, when the user enters the following command line parameters at the terminal:
$./my_program source_photo.jpg -o target_photo.jpg -w 720 -s
Variables in the program are assigned the following values:
inputFileName = "source_photo.jpg" outputFileName = "target_photo.jpg" imageWidth = 720 bShowImage = true
In this example, the first parameter entered by the user (that is, the first parameter source_ photo.jpg following the executable name) is not flagged, and the next few parameters are flagged. (Flagged parameters here refer to flagged characters or words such as'-o target_photo.jpg', which start with a single bar'-'or a double bar'-', and the parameters that follow.)
2. Flagged and Unflagged Parameters
Insert a little here to illustrate flagged and unlabeled command line parameters. A flagged command-line parameter starts with a short flag led by a single bar'-'(such as'-o','-f'), or a long flag led by a double bar (such as'-output','-filter_name'), followed by an optional string that represents the value o f the parameter. For example above, when the user enters a command line with'-o_target_photo.jpg', When'- output target_photo.jpg'or'- output target_photo.jpg','target_photo.jpg' is the value of the corresponding parameter for'-o'or'- output'. Flagged parameters are identified by flags and are not confused with other parameters, so the user can insert them anywhere in the parameter table when entering the command line.
In a flagged parameter, if the parameter represents a Boolean variable, the flag may be followed by no parameter representing the value of the variable. For example, in the example above,'-s', when this flag appears on the command line, bShowImage is assigned a value of true, otherwise bShowImage is set to the default value of false.
Another is the unlabeled parameter, which must appear in a fixed order in the command line parameter table to be recognized by the program. For example, in the example above, The user enters "source_photo.jpg" on the command line, which must be placed after the executable name, except for all flagged parameters. Another example is the compilation command for g+++ SmpCommandLine_Demo.cpp-o my_program, which is commonly used in g+. Medium, previous SmpCommandLine_ Demo. CPP is a flagged parameter that specifies the input file name followed by'-o my_program', which represents the output executable name.
One important thing to note here is that SmpCommandLine doesn't know if a flag bit is followed by a parameter that represents a value until the parameter is extracted, so
When SmpCommandLine is called to extract parameters, all flagged parameters are extracted before parameters that are not parameters are extracted.
For example, in the example above, the next sentence is at the end:
string inputFileName = commands.getString( 1 );
That is, after all the flagged parameters (-w, -s, -o) are extracted, the first flagged parameter is extracted in the remaining command line strings (otherwise the program will be warned of errors).
Above is the usage of the simple version. Below is a look at the usage of the full version:
3. Full Version Usage
Let's start with the same sample code:
#include "SmpCommandLine.hpp" using namespace std; int main( int argc, char *argv[] ) { SmpCommandLine userCommands( argc, argv ); // Firstly extract all flagged argumants (i.e. argument identified by a leading hyphen flag) int index = userCommands.getInteger( "i", "index", 0, "specifies the index of the item" ); double radius = userCommands.getDouble( "r", "radius", 6750.0, "the radius of the shpere" ); bool bShowImage = userCommands.getBoolean( "s", "show_image", CMD_FLAG_ONLY, false, "whether display the image during processing" ); string filterName = userCommands.getString( "f", "filter", "", "specifies the image effect filter" ); // Then, extract unflagged arguments: string srcFileName = userCommands.getString( 1, "", "file name of the source image" ); string tgtFileName = userCommands.getString( 2, "", "file name of the target image" ); // Check whether need to show help message: if( userCommands.helpMessageWanted() || argc < 3 ) { userCommands.showHelpMessage(); } // Print the extracted arguments cout << "index = " << index << endl; cout << "radius = " << radius << endl; cout << "bShowImage = " << bShowImage << endl; cout << "description : " << description << endl; cout << "srcFileName : " << srcFileName << endl; cout << "tgtFileName : " << tgtFileName << endl; return(0); }
In this example, when calling the interface method of SmpCommandLine, in addition to specifying the flag or location of the command line parameter, a default value for the parameter variable is specified, as well as help information on the usage of this parameter.
For example, the getString() method below looks for parameters from the command line the user enters with the'-f'or'-filter' flag and assigns it to a string variable called filterName:
string filterName = userCommands.getString( "f", "filter", "", "specifies the image effect filter" );
The last two parameters of this method represent the default value of filterName and the help information for this command line parameter, respectively.
You can use getInteger(), getFloat(), getDouble() to get integer, floating-point, and double-precision floating-point parameters as input by the user. Each of these methods has two overloaded versions for extracting flagged and unidentifiered parameters:
// Extract command line parameters with the -f or--filter flag and return the string type: string filterName = userCommands.getString( "f", "filter", "", "specifies the applied filter" ); // Extract the first unlabeled parameter and return the string type: string srcFileName = userCommands.getString( 1, "", "file name of the source image" );
Note that the method used to extract a flagged Boolean parameter is slightly unusual, because a flag for a Boolean parameter can be followed by either a parameter with or without a value, and to indicate that the flag is followed by no additional parameters, pass in CMD_when calling the method that extracts the Boolean parameter FLAG_ ONLY (default) or CMD_WITH_VALUE as explanation:
getBoolean( "s", "show_image", CMD_FLAG_ONLY, false, "whether display the image during processing" );
Finally, when you feel the user needs help, call the following code to display the command line's help information
if( userCommands.helpMessageWanted() || argc < 3 ) { userCommands.showHelpMessage(); }
At this point, if user input is on the command line, $/ my_program --help (or -h), the terminal displays the following help information:
$>./my_program --help Help Message: Usage of ./my_program ./my_program [argument1] [argument2] [-i/--index val] [-r/--radius val] [-s/--show_image] [-d/--description val] [-h/--help] -i/--index val : specifies the index of the item (defualt value:0) -r/--radius val : the radius of the shpere (defualt value:6750.000000) -s/--show_image : whether display the image during processing (defualt value:false) -f/--filter val : specifies the applied image filter argument1: file name of the source image argument2: file name of the target image -h/--help : Show this help message (defualt value:false)
Okay, to summarize: SmpCommandLine is a lightweight C++ command line analysis package. Its main features are:
1) It only has one header file, so it only needs to be included in the cpp file where the main function is located.
2) It does not need to list all the parameter rules (parameter flags) in advance, but builds the parameter rules while extracting the required parameters. The help information is automatically synthesized by collecting information from the previously called extraction functions.
3) When using, attention should be paid to extracting all flagged parameters (parameters with "-") before extracting unflagged parameters. (This is the least desirable aspect of the package; it is a trade-off with point 2).
Finally, SmpCommandLine uses string s, vector s, algorithm, cctype, and so on from the STL standard library.
For more information, refer to readme on GitHub. MD.
Hope this happens to be a little useful to you, and welcome suggestions for improvement and optimization. Enjoy! 😊