开发者

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.


Consider using the boost program options library.


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!


You could also write a README and on prog --help just print it to the console.


You can use the getopt C library which goal is precisely allowing to parse and use multiple options (short or long form).

(Beware there is also a getopts program to be used with shell script with similar features)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜