正确解析 C 中的命令行参数

发布于 2024-12-19 05:30:03 字数 1342 浏览 1 评论 0原文

我想做的是接受命令行参数并根据参数更改一些变量。我附上了我的一段代码,因为整个代码大约有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

囚我心虐我身 2024-12-26 05:30:03

正确的方法是使用许多现有的解析器库之一,而不是自己手动解析。它更简单、更强大,并且省去了您重新发明轮子的麻烦。

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 answer
  • argp: my usual choice
  • suboptions: for complex solutions
最好是你 2024-12-26 05:30:03
char somestring[500]="";//need initialize

    while(++ca<argc){//increment before condition test
        if(strcmp(argv[ca],"-f")==0){

            if(ca < argc && strcmp(argv[ca],"red")==0){//need ca check

        if(ca == argc){//bad strcmp(argv[ca],"")
            printf("invalid argument");
char somestring[500]="";//need initialize

    while(++ca<argc){//increment before condition test
        if(strcmp(argv[ca],"-f")==0){

            if(ca < argc && strcmp(argv[ca],"red")==0){//need ca check

        if(ca == argc){//bad strcmp(argv[ca],"")
            printf("invalid argument");
狼性发作 2024-12-26 05:30:03

将 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

故事灯 2024-12-26 05:30:03

如果您使用 for 循环而不是愚蠢的“else while”结构,事情会变得更加清晰:

  for(ca=1; ca < argc ; ca++)
  {
      if(!strcmp(argv[ca],"-f"))
      {
         printf("This works");
         ca++; /* need to test if ca can be incremented */
         if(!strcmp(argv[ca],"red")){
             printf("this will print red\n");
         }
         else{
             printf("invalid color");
         }
      }
      else if(!strcmp(argv[ca],""))
      {
         printf("invalid argument");
      }
      else{
          strcat(somestring,argv[ca]);
      }
  }
  printf("%s",somestring);

Things will get much clearer if you use a for-loop instead of the silly "else while" construct:

  for(ca=1; ca < argc ; ca++)
  {
      if(!strcmp(argv[ca],"-f"))
      {
         printf("This works");
         ca++; /* need to test if ca can be incremented */
         if(!strcmp(argv[ca],"red")){
             printf("this will print red\n");
         }
         else{
             printf("invalid color");
         }
      }
      else if(!strcmp(argv[ca],""))
      {
         printf("invalid argument");
      }
      else{
          strcat(somestring,argv[ca]);
      }
  }
  printf("%s",somestring);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文