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.我可以将整个消息粘贴到程序中的变量中,并在用户键入mycmd --help或类似内容时将其打印到屏幕上。优点,保持可执行和不可编辑,缺点是代码,因为我会有类似下面的东西漂浮。
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.我可以为我的程序编写一个man条目,但这不是非常便携,因为该应用程序将在Windows和Linux上使用得非常相似。
我可以写一个README文件,只是把它放在与可执行文件相同的目录中。优点是它简单易用,但缺点是有人可以轻松删除/编辑文件。
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.
理想情况下,开发人员(目前是我)很容易编辑和保持更新,但其他人不能真正搞砸它。
4 个解决方案
#2
6
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!
如果您愿意,也可以使用printf功能...我认为这是一种常见的做法,但如果有人有更好的想法,我会被捕!
Regards!
#3
1
You could also write a README and on prog --help
just print it to the console.
您也可以编写README并在prog --help上将其打印到控制台。
#4
0
You can use the getopt C library which goal is precisely allowing to parse and use multiple options (short or long form).
您可以使用getopt C库,其目标是精确地允许解析和使用多个选项(短或长形式)。
(Beware there is also a getopts
program to be used with shell script with similar features)
(注意还有一个getopts程序用于具有类似功能的shell脚本)
#1
#2
6
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!
如果您愿意,也可以使用printf功能...我认为这是一种常见的做法,但如果有人有更好的想法,我会被捕!
Regards!
#3
1
You could also write a README and on prog --help
just print it to the console.
您也可以编写README并在prog --help上将其打印到控制台。
#4
0
You can use the getopt C library which goal is precisely allowing to parse and use multiple options (short or long form).
您可以使用getopt C库,其目标是精确地允许解析和使用多个选项(短或长形式)。
(Beware there is also a getopts
program to be used with shell script with similar features)
(注意还有一个getopts程序用于具有类似功能的shell脚本)