C工程中灵活的参数解析方式

  1. 前言
  2. 1、 参考
  3. 2、 strcmp和strncmp使用方法
  4. 3、 灵活的参数解析方式
    1. 3.1 【Base Level】:
    2. 3.2 【Pro Level】:
    3. 3.3 【Pro+ Level】:
  5. THE END!

前言

    在工程实践中,通常我们在命令行传递参数时,会直接通过argv的方式将入参一一传递进去,这种方式简单且固定,不具扩展性,并且在大的工程中参数传递数目较多时,会出现漏传参数导致出现段错误等问题。本文主要提供了一种灵活的参数解析方式,推荐使用这种方式。

1、 参考

strcmp_reference
strncmp_reference

2、 strcmp和strncmp使用方法

int strcmp ( const char * str1, const char * str2 );

Compare two strings
Compares the C string str1 to the C string str2.

This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

This function performs a binary comparison of the characters. For a function that takes into account locale-specific rules, see strcoll.

return value indicates
<0 the first character that does not match has a lower value in ptr1 than in ptr2
0 the contents of both strings are equal
>0 the first character that does not match has a greater value in ptr1 than in ptr2
int strncmp ( const char * str1, const char * str2, size_t num );

Compare characters of two strings
Compares up to num characters of the C string str1 to those of the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.

3、 灵活的参数解析方式

主要是通过字符串函数strcmp和strncmp进行参数的解析。

3.1 【Base Level】:

if(argc > 1)
{
   int i = 0;
   for(i = 1; i < argc; ++i)
   {
      if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "-help"))
      {
          print_help(argv[0]);
      }
      else if(!strcmp(argv[i], "-s"))
      {
          width = atoi(argv[++i]);
          height = atoi(argv[++i]);
      }
   }
}

3.2 【Pro Level】:

if(argc > 1)
{
   int i = 0;
   f
   for(i = 1; i < argc; ++i)
   {
      char *s = argv[i];
      if(s[0] == '-')
      {
            char *f;
            switch(s[1])
            {
              case 'p':
                 f = argv[++i];
                 if(!strcmp(f, "noiframe"))
                    dumpi = 0;
                 if(!strcmp(f, "nopframe"))
                    dumpp = 0;
                 if(!strcmp(f, "nobframe"))
                    dumpb = 0;
                   break;
                 case 'i':
                     input = argv[++i];
                     break;
                 case 'o':
                     output = argv[++i];
                     break;
                 case '-':
                    if(!strcmp(s+2, "display"))
                    {
                      display = 1;
                    }
                  break;
         }
      }
   }
}

3.3 【Pro+ Level】:

通过strncmp指定解析字符的长度。

if(argc > 1)
{
   int i = 0;
   for(i = 1; i < argc; ++i)
   {
      if(!strncmp(argv[i], "-h", 2) || !strncmp(argv[i], "-help", 5))
      {
          print_help(argv[0]);
      }
      else if(!strncmp(argv[i], "-s", 2))
      {
          width = atoi(argv[++i]);
          height = atoi(argv[++i]);
      }
      else if(!strcmp(argv[i], "-ss", 3))
      {
          length = atoi(argv[++i]);
      }
   }
}

THE END!


本博文只能阅读,谢绝转载,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2963033731@qq.com

文章标题:C工程中灵活的参数解析方式

字数:616

本文作者:Soaring Lee

发布时间:2019-05-13, 15:13:47

最后更新:2021-06-14, 12:13:44

原始链接:https://soaringleefighting.github.io/2019/05/13/【C系列】C工程中灵活的参数解析方式/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

×

喜欢就点赞,疼爱就打赏

相册