I'm pretty sure it's simple. Is there a pre-defined header for create a help context in a command line program.
$ program --help
would provide a list of various help options.
The simplest way to do it in c++ is:
int main(int argc, char** argv)
{
if(argc == 2 && strcmp(argv[1], "--help")==0)
{..print help here..}
return 0;
}
Something along these lines...
int main(int argc, char* argv[])
{
std::vector<std::string> cmdLineArgs(argv, argv+argc);
for(auto& arg : cmdLineArgs)
{
if(arg == "--help" || arg == "-help")
{
std::cout << "Helpful stuff here\n";
}
else if(arg == "whatever")
{
std::cout << "whatever?!\n";
}
}
}
Of course, there are libraries to handle cmd line arguments. But for simple stuff it's really not hard to do yourself.
For C++, you have Boost.Program_options
http://www.boost.org/doc/libs/1_55_0/doc/html/program_options.html
But you'll have to bring the whole boost library (which can be tedious, the first time you do it).
I think you should have a look to this library : Getopt which is part of the GNU C Library. It allows you to parse -like
parameters efficiently.
You could do it multiple ways depending on how you want to go about it.
You could use strcmp() and just parse argv[1]:
if(strcmp(argv[1],"--help") == 0)
or you can use getopt if you are running linux:
http://www.gnu.org/software/libc/manual/html_node/Getopt.html
getopt_long is your friend. For one-character options, getopt
will suffice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.