Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to include a "help" message in a console application?

I am writing a console application which is rapidly gaining many command line arguments and flags. For this reason I want the user to be able to access a description of these flags and what purpose they serve.

There are several possible solutions I can think of

  • I could write a README file and just stick it in the same directory as the executable. Advantages are it is simple and portable, but disadvantage is that it is easy for someone to remove/edit the file.
  • I could stick the whole message in a variable inside my program and print this to the screen when the user types mycmd --help or something similar. Advantages, stays with executable and not editable, disadvantage is in code since I would have something like that below floating around.

    const char[] helpmsg = "Line1\n"
                           "Line2\n"
                           "...\n"
                           "LineN\n";
    
  • I could write a man entry for my program but this isn't very portable as the application will be used pretty equally on Windows and Linux.

I'm aware the question is probably a matter of taste but I was just curious if there are any other solutions which I haven't thought of that people have used in the past.

Ideally it would be something which is easy for the developer (at the moment me) to edit and keep updated but where others cannot really mess with it.

like image 351
Dan Avatar asked Dec 06 '22 19:12

Dan


2 Answers

Consider using the boost program options library.

like image 55
Simon Avatar answered May 16 '23 07:05

Simon


To print an help message, I usually use a function for this. So you can use it at startup or as well at runtime. For example:

void usage(char* progName)
{
  cout << progName << "[options]" << endl <<
      "Options:" << endl <<
      "-h | --help        Print this help" << endl <<
      "-v | --version     Print the SVN version" << endl <<
      "-V | --Version     Print the proxy version" << endl <<
      "-d | --daemonize   Run as daemon" << endl <<
      "-P | --pidfile     Path to PID file (default: " <<
        WPASUP_PROXY_DEFAULT_PID_FILE << ")" << endl <<
      "-l | --logging     Path to logging file (default: " <<
        WPASUP_PROXY_DEFAULT_LOGGING << ")" << endl <<
      "-i | --ip          The IP address of the main application (default: " <<
        WPASUP_PROXY_MAIN_APP_IP << ")" << endl <<
      "-p | --port        The port number of the main application (default: " <<
        WPASUP_PROXY_DEFAULT_MAIN_APP_PORT << ")" << endl <<
      "-w | --wpa_cli     Path to wpa_cli program (default: " <<
        WPASUP_PROXY_DEFAULT_WPA_CLI << ")" << endl;
}

You can also use the printf function if your prefer ... I think that is a common practice but if someone has a better idea, I'd be interrested!

Regards!

like image 33
morandg Avatar answered May 16 '23 08:05

morandg