正确解析 C 中的命令行参数
我想做的是接受命令行参数并根据参数更改一些变量。我附上了我的一段代码,因为整个代码大约有 400 行。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char somestring[500];
int ca=0;
if (argc==1) //if no arguments are specified use defaults
{
}
else
{
while(ca<argc)
{
ca++
if(strcmp(argv[ca],"-f")==0)
{
printf("This works");
ca++;
if(strcmp(argv[ca],"red")==0){
printf("this will print red\n");
}
else{
printf("invalid color");
}
}
if(strcmp(argv[ca),"")==0)
{
printf("invalid argument");
}
else {
strcat(somestring,argv[ca]);
}
}
printf("%s",somestring);
}
}
如果用户输入:
./foobar -f red 这是一个字符串
./foobar -f red 这是程序应该打印的
:“这将打印红色,这是一个字符串”
如果用户输入:
./foobar -f 红色
程序应该打印“无效的命令行参数数量”。
做到这一点最简单的方法是什么?我尝试了很多可能性但没有运气。 不同数量的参数对我来说是主要问题(而且我有超过 5 个选项,例如 -f -b -h -w -e),
非常感谢您的帮助。如果您愿意,我可以添加我的整个代码。
What I am trying to do is take in command line arguments and change some variables according to the arguments. I have attached a chunk of my code because the whole code is ~400 lines.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char somestring[500];
int ca=0;
if (argc==1) //if no arguments are specified use defaults
{
}
else
{
while(ca<argc)
{
ca++
if(strcmp(argv[ca],"-f")==0)
{
printf("This works");
ca++;
if(strcmp(argv[ca],"red")==0){
printf("this will print red\n");
}
else{
printf("invalid color");
}
}
if(strcmp(argv[ca),"")==0)
{
printf("invalid argument");
}
else {
strcat(somestring,argv[ca]);
}
}
printf("%s",somestring);
}
}
If the user inputs:
./foobar -f red this is a string
the program should print:
"this will print red this is a string"
If the user inputs:
./foobar -f red
the program should print "invalid number of command line arguments".
What is the easiest way to do this? I have tried tons of possibilities with no luck.
Varying number of arguments is the main problem for me (also I have more than 5 options e.g..-f -b -h -w -e)
Help would much appreciated. I can add my whole code if you want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正确的方法是使用许多现有的解析器库之一,而不是自己手动解析。它更简单、更强大,并且省去了您重新发明轮子的麻烦。
GNU
libc
手册建议了一些库,具体取决于您想要的花哨/标准:http://www.gnu.org/software/libc /manual/html_node/Parsing-Program-Arguments.html
getopt
:正如另一个答案提到的argp
:我通常的选择子选项
:用于复杂的解决方案The proper way is to use one of the many existing parser libraries instead of manually parse yourself. It's easier, more powerful, and saves you the trouble of reinventing the wheel.
GNU
libc
manual suggests a few libraries, depending on how fancy/standard you want to be:http://www.gnu.org/software/libc/manual/html_node/Parsing-Program-Arguments.html
getopt
: as mentioned by another answerargp
: my usual choicesuboptions
: for complex solutions将 int ca= 0 更改为 int ca= 1
因为 argv[0] 是可执行文件的名称
Change int ca= 0 to int ca= 1
Because argv[0] is the name of your executable
如果您使用 for 循环而不是愚蠢的“else while”结构,事情会变得更加清晰:
Things will get much clearer if you use a for-loop instead of the silly "else while" construct: