从命令行将参数传递给C程序

Bla*_*Cow 32 c linux arguments

所以我在Linux中,当你从命令行执行它时,我希望有一个程序接受参数.

例如,

./myprogram 42 -b -s

那么程序会将该数字42存储为int并执行某些代码部分,具体取决于它是什么参数-b或-s.

CMS*_*CMS 39

你可以使用getopt.

 #include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>

 int
 main (int argc, char **argv)
 {
   int bflag = 0;
   int sflag = 0;
   int index;
   int c;

   opterr = 0;

   while ((c = getopt (argc, argv, "bs")) != -1)
     switch (c)
       {
       case 'b':
         bflag = 1;
         break;
       case 's':
         sflag = 1;
         break;
       case '?':
         if (isprint (optopt))
           fprintf (stderr, "Unknown option `-%c'.\n", optopt);
         else
           fprintf (stderr,
                    "Unknown option character `\\x%x'.\n",
                    optopt);
         return 1;
       default:
         abort ();
       }

   printf ("bflag = %d, sflag = %d\n", bflag, sflag);

   for (index = optind; index < argc; index++)
     printf ("Non-option argument %s\n", argv[index]);
   return 0;
 }
Run Code Online (Sandbox Code Playgroud)

  • 他/她不应该首先理解"主要论点"吗?;) (2认同)

Gre*_*ill 26

在C中,这是使用传递给main()函数的参数完成的:

int main(int argc, char *argv[])
{
    int i = 0;
    for (i = 0; i < argc; i++) {
        printf("argv[%d] = %s\n", i, argv[i]);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

可以在网上找到更多信息,例如主要文章的参数.

  • 在C中,对数组的引用是一个地址,就像指针是一个地址一样.因此,argv可以被称为"数组"和"指向数组的指针".这是C的美丽简单之一,也是混淆点之一. (2认同)

jfs*_*jfs 11

考虑使用getopt_long().它允许任何组合的短期和长期期权.

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

/* Flag set by `--verbose'. */
static int verbose_flag;

int
main (int argc, char *argv[])
{
  while (1)
    {
      static struct option long_options[] =
    {
      /* This option set a flag. */
      {"verbose", no_argument,       &verbose_flag, 1},
      /* These options don't set a flag.
         We distinguish them by their indices. */
      {"blip",    no_argument,       0, 'b'},
      {"slip",    no_argument,       0, 's'},
      {0,         0,                 0,  0}
    };
      /* getopt_long stores the option index here. */
      int option_index = 0;

      int c = getopt_long (argc, argv, "bs",
               long_options, &option_index);

      /* Detect the end of the options. */
      if (c == -1)
    break;

      switch (c)
    {
    case 0:
      /* If this option set a flag, do nothing else now. */
      if (long_options[option_index].flag != 0)
        break;
      printf ("option %s", long_options[option_index].name);
      if (optarg)
        printf (" with arg %s", optarg);
      printf ("\n");
      break;
    case 'b':
      puts ("option -b\n");
      break;
    case 's':
      puts ("option -s\n");
      break;
    case '?':
      /* getopt_long already printed an error message. */
      break;

    default:
      abort ();
    }
    }

  if (verbose_flag)
    puts ("verbose flag is set");

  /* Print any remaining command line arguments (not options). */
  if (optind < argc)
    {
      printf ("non-option ARGV-elements: ");
      while (optind < argc)
    printf ("%s ", argv[optind++]);
      putchar ('\n');
    }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

有关:


wom*_*ble 7

看看getopt库; 这几乎是这类事情的黄金标准.


sas*_*nin 5

getopt()您也可以考虑使用argp_parse()(同一个库的替代接口),而不是.

来自libc手册:

getopt更标准(它的短选项版本只是POSIX标准的一部分),但argp_parse对于非常简单和非常复杂的选项结构,使用 通常更容易,因为它为您做了更多的脏工作.

但我总是对标准感到满意getopt.

NB GNU getoptwith getopt_long是GNU LGPL.