So I'm in Linux and I want to have a program accept arguments when you execute it from the command line.
我在Linux中,我想让一个程序在执行命令行时接受参数。
For example,
例如,
./myprogram 42 -b -s
。/ myprogram 42 - b - s
So then the program would store that number 42 as an int and execute certain parts of code depending on what arguments it gets like -b or -s.
然后程序将42号存储为一个整数,并执行特定的代码部分,这取决于它的参数是-b还是-s。
6 个解决方案
#1
36
You could use getopt.
您可以使用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;
}
#2
25
In C, this is done using arguments passed to your main()
function:
在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;
}
More information can be found online such as this Arguments to main article.
更多的信息可以在网上找到,比如这篇文章的论点。
#3
10
Consider using getopt_long()
. It allows both short and long options in any combination.
考虑使用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;
}
Related:
相关:
Which command line commands style do you prefer?- 您喜欢哪种命令行命令样式?
- What is the general syntax of a Unix shell command?
- Unix shell命令的一般语法是什么?
#4
6
Take a look at the getopt library; it's pretty much the gold standard for this sort of thing.
看一下getopt库;这几乎是这类事情的黄金标准。
#5
4
Other have hit this one on the head:
另一个人则在头上打了一拳:
- the standard arguments to
main(int argc, char **argv)
give you direct access to the command line (after it has been mangled and tokenized by the shell) - 对main(int argc, char **argv)的标准参数使您可以直接访问命令行(在它被shell破坏和标记之后)
- there are very standard facility to parse the command line:
getopt()
andgetopt_long()
- 有非常标准的工具来解析命令行:getopt()和getopt_long()
but as you've seen the code to use them is a bit wordy, and quite idomatic. I generally push it out of view with something like:
但是,正如您所看到的,使用它们的代码有些冗长,而且是相当的idomatic。我通常会把它从视图中推出来,比如:
typedef
struct options_struct {
int some_flag;
int other_flage;
char *use_file;
} opt_t;
/* Parses the command line and fills the options structure,
* returns non-zero on error */
int parse_options(opt_t *opts, int argc, char **argv);
Then first thing in main:
然后是主要的第一件事:
int main(int argc, char **argv){
opt_t opts;
if (parse_options(&opts,argc,argv)){
...
}
...
}
Or you could use one of the solutions suggested in Argument-parsing helpers for C/UNIX.
或者,您可以使用在C/UNIX的argument解析帮助器中建议的解决方案之一。
#6
4
Instead of getopt()
, you may also consider using argp_parse()
(an alternative interface to the same library).
除了getopt(),您还可以考虑使用argp_parse()(同一个库的另一个接口)。
From libc manual:
从libc手册:
getopt
is more standard (the short-option only version of it is a part of the POSIX standard), but usingargp_parse
is often easier, both for very simple and very complex option structures, because it does more of the dirty work for you.getopt是更标准的(它只是POSIX标准的一部分),但是使用argp_parse通常更容易,因为它对非常简单和非常复杂的选项结构,因为它为您做了更多的工作。
But I was always happy with the standard getopt
.
但我总是对标准的getopt感到满意。
N.B. GNU getopt
with getopt_long
is GNU LGPL.
使用getopt_long的nb GNU getopt是GNU LGPL。
#1
36
You could use getopt.
您可以使用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;
}
#2
25
In C, this is done using arguments passed to your main()
function:
在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;
}
More information can be found online such as this Arguments to main article.
更多的信息可以在网上找到,比如这篇文章的论点。
#3
10
Consider using getopt_long()
. It allows both short and long options in any combination.
考虑使用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;
}
Related:
相关:
Which command line commands style do you prefer?- 您喜欢哪种命令行命令样式?
- What is the general syntax of a Unix shell command?
- Unix shell命令的一般语法是什么?
#4
6
Take a look at the getopt library; it's pretty much the gold standard for this sort of thing.
看一下getopt库;这几乎是这类事情的黄金标准。
#5
4
Other have hit this one on the head:
另一个人则在头上打了一拳:
- the standard arguments to
main(int argc, char **argv)
give you direct access to the command line (after it has been mangled and tokenized by the shell) - 对main(int argc, char **argv)的标准参数使您可以直接访问命令行(在它被shell破坏和标记之后)
- there are very standard facility to parse the command line:
getopt()
andgetopt_long()
- 有非常标准的工具来解析命令行:getopt()和getopt_long()
but as you've seen the code to use them is a bit wordy, and quite idomatic. I generally push it out of view with something like:
但是,正如您所看到的,使用它们的代码有些冗长,而且是相当的idomatic。我通常会把它从视图中推出来,比如:
typedef
struct options_struct {
int some_flag;
int other_flage;
char *use_file;
} opt_t;
/* Parses the command line and fills the options structure,
* returns non-zero on error */
int parse_options(opt_t *opts, int argc, char **argv);
Then first thing in main:
然后是主要的第一件事:
int main(int argc, char **argv){
opt_t opts;
if (parse_options(&opts,argc,argv)){
...
}
...
}
Or you could use one of the solutions suggested in Argument-parsing helpers for C/UNIX.
或者,您可以使用在C/UNIX的argument解析帮助器中建议的解决方案之一。
#6
4
Instead of getopt()
, you may also consider using argp_parse()
(an alternative interface to the same library).
除了getopt(),您还可以考虑使用argp_parse()(同一个库的另一个接口)。
From libc manual:
从libc手册:
getopt
is more standard (the short-option only version of it is a part of the POSIX standard), but usingargp_parse
is often easier, both for very simple and very complex option structures, because it does more of the dirty work for you.getopt是更标准的(它只是POSIX标准的一部分),但是使用argp_parse通常更容易,因为它对非常简单和非常复杂的选项结构,因为它为您做了更多的工作。
But I was always happy with the standard getopt
.
但我总是对标准的getopt感到满意。
N.B. GNU getopt
with getopt_long
is GNU LGPL.
使用getopt_long的nb GNU getopt是GNU LGPL。