getopt_long() - 有道使用它? [英] getopt_long() -- proper way to use it?
问题描述
OK,我已搜查,发现我开始在正确的方向以下两个StackOverflow的主题:
OK, I have searched and found the following two StackOverflow topics that started me in the right direction:
<一个href=\"http://stackoverflow.com/questions/189972/argument-parsing-helpers-for-c-unix/\">Argument-parsing对于C / UNIX 佣工
<一个href=\"http://stackoverflow.com/questions/498320/pass-arguments-into-c-program-from-command-line\">Pass参数到C程序的命令行
注:所有code是伪code。会后编译code工作时。
NOTE: ALL CODE IS PSEUDO-CODE. WILL POST COMPILABLE CODE WHEN IT WORKS.
不过,我还是如何在C.将getopt_long()我写的程序被定义为具有以下可能的标签(但可以包含多达你绝对需要,填补剩下的完全糊涂空值):
However, I'm still completely confused on how to use getopt_long() in C. The program I'm writing is defined as having the following possible tags (but can include as many as you absolutely need, filling the rest in with empty values):
id3tagEd filename -title "title" -artist "artist" -year 1991 -comment "comment" -album "album" -track 1
现在,从我读,我需要利用结构为长选项,正确吗?如果是这样,我写的东西沿着这行:
Now, from what I read, I need to utilize a struct for the long options, correct? If so, I wrote something along the lines of this:
struct fields field =
{
char *[] title;
char *[] artist;
char *[] album;
int year;
char *[] comment;
int track;
}
static struct options long_options[] =
{
{"title", 0, &field.title, 't'},
{"artist", 0, &field.artist, 'a'},
{"album", 0, &field.album, 'b'},
{"year", 0, &field.year, 'y'},
{"comment", 0, &field.comment, 'c'},
{"track", 0, &field.track, 'u'},
{0, 0, 0, 0}
}
现在,从我收集的,我会通过此调用它:
Now, from what I gathered, I would be calling it via this:
int option_index = 0;
int values = getopt_long(argc, argv, "tabycu", long_options, &option_index);
从这里,我可以用严格的现场结构,做什么,我需要我的计划之内?但是,如果是这种情况,有人可以解释整个long_options结构?我读了手册页和这样的,我只是心乱如麻。通过重读手册页,我可以看到我可以给变量设置为null,和我所有的选择需求应该被设置为required_argument?然后,通过一个while()循环设置结构?但是,我看到OPTARG被使用。这组由getopt_long()?抑或是从这个例子里缺?
From here, could I strictly use the field struct and do what I need to within my program? However, if this is the case, can someone explain the whole long_options struct? I read the man pages and such, and I'm just utterly confused. By rereading the man pages, I can see I can set variables to null, and should be setting all my option requirements to "required_argument"? And then setting the structs via a while() loop? However, I see optarg being used. Is this set by getopt_long()? Or is it missing from the example?
和最后一个问题,我总会有一位不愿透露姓名的必需选项:文件名,将我只是用的argv [0]来访问呢? (因为我可以假设它会是第一个)。
And one last issue, I will always have an unnamed required option: filename, would I just use argv[0] to gain access to that? (Since I can assume it'll be first).
在一个侧面说明,这是关系到一个家庭作业的问题,但它没有任何关系确定其更多的是基本的它,必须了解的参数传递,并通过命令行先用C解析。
On a side note, this is related to a homework problem, but it has nothing to do with fixing it, its more of a fundamental, have to understand argument passing and parsing in C via command line first.
推荐答案
首先,你可能不希望 0
为 has_arg
领域 - 它必须是 no_argument
, required_arguemnt
或 optional_argument
。在你的情况,所有的人都将是 required_argument
。除此之外,你不使用标志
字段正确的 - 它必须是整数指针。如果相应的标志设置, getopt_long()
将与您通过 VAL
字段传递的整数填入。我不认为你需要在所有此功能。这里有一个更好的(缩短)例如,对于您的情况:
First off, you probably don't want 0
for the has_arg
field - it must be one of no_argument
, required_arguemnt
, or optional_argument
. In your case, all of them are going to be required_argument
. Besides that, you're not using the flag
field correctly - it has to be an integer pointer. If the corresponding flag is set, getopt_long()
will fill it in with the integer you passed in via the val
field. I don't think you need this feature at all. Here's a better (shortened) example for your case:
static struct option long_options[] =
{
{"title", required_argument, NULL, 't'},
{"artist", required_argument, NULL, 'a'},
{NULL, 0, NULL, 0}
};
后来的后来,你可以适当地使用它(直接从手册页,我添加了一些注释):
Then later, you can use it appropriately (straight from the manpage, I added some comments):
// loop over all of the options
while ((ch = getopt_long(argc, argv, "t:a:", long_options, NULL)) != -1)
{
// check to see if a single character or long option came through
switch (ch)
{
// short option 't'
case 't':
field.title = optarg; // or copy it if you want to
break;
// short option 'a'
case 'a':
field.artist = optarg; // or copy it if you want to
break;
}
}
您可以为您的其他领域扩展必要的(并添加一些错误处理,拜托了!)。注 - 如果你想使用 -title
和 -artist
就像你在你的例子有,你需要使用 getopt_long_only()
,它没有短选项。
You can extend for your other fields as necessary (and add some error handling, please!). Note - if you want to use -title
and -artist
like you have in your example, you'll need to use getopt_long_only()
, which doesn't have short options.
至于你的文件名
选项,你会得到作为一个?
从 getopt_long()
电话,所以你可以在那个时候处理。的其他选项是,要求它是第一或者最后的选项,并通过它本身单独处理。
As to your filename
option, you'll get that out as a '?'
from the getopt_long()
call, so you could handle it at that time. Your other options are to require that it is either the first or the last option and handle it by itself separately.
这篇关于getopt_long() - 有道使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!