解析选项的方式与传递给main的方式相同 [英] Parsing options in the same way they are passed to main

查看:45
本文介绍了解析选项的方式与传递给main的方式相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果main的原型为:

int main(int argc,char * argv []);


你最终会得到一堆* argv中的参数和argc中的数字。现在

我想要做的就是在字符串上模拟相同的动作。比方说我有:


char * command =" ./ blah --arg1 --arg2 123 -x --arg2 = w00t"


如何实现与main()相同的效果?

Jeff

If main is prototyped as:
int main(int argc, char *argv[]);

You will end up with a bunch of arguments in *argv, and the number in argc. Now
what I want to do is emulate that same action on a string. Say for example I have:

char *command = "./blah --arg1 --arg2 123 -x --arg2=w00t"

How do I acheive the same effect as in main()?
Jeff

推荐答案

Jeff Rodriguez<是ne ******** @ gurugeek.EXAMPLENOSPAM.com>写道:
Jeff Rodriguez <ne********@gurugeek.EXAMPLENOSPAM.com> writes:
如果main的原型为:
int main(int argc,char * argv []);

你最终会得到* argv中的一堆参数,以及
argc中的数字。现在我想做的是在
字符串上模拟相同的动作。比方说我有:

char * command =" ./ blah --arg1 --arg2 123 -x --arg2 = w00t"

我如何实现与main()相同的效果?
If main is prototyped as:
int main(int argc, char *argv[]);

You will end up with a bunch of arguments in *argv, and the number in
argc. Now what I want to do is emulate that same action on a
string. Say for example I have:

char *command = "./blah --arg1 --arg2 123 -x --arg2=w00t"

How do I acheive the same effect as in main()?




这取决于操作系统,shell和其他东西。如果

你要做的就是将字符串拆分为

空格中的单词,我建议你只需编写代码即可。它不是太难了。

-

鉴于计算能力随时间呈指数增长,
$带有指数或更好的O符号的b $ b算法

实际上是一个大的常数线性。

--Mike Lee



It depends on the operating system, shell, and other things. If
all you want to do is to break apart the string into words at
white space, I suggest you just write code to do it. It''s not
too hard.
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee


Ben Pfaff写道:
Ben Pfaff wrote:
这取决于操作系统,shell和其他东西。如果您想要做的就是将字符串拆分为白色空间中的单词,我建议您只需编写代码即可。这不太难。
It depends on the operating system, shell, and other things. If
all you want to do is to break apart the string into words at
white space, I suggest you just write code to do it. It''s not
too hard.




确实:(原始代码)

#include< stdio.h>

#include< string.h>

#include< unistd.h>

#include< stdlib.h>


int main(int argc,char * argv [])

{

int i;

char * string = NULL;

char * str = NULL;

int size = 0;


for(i = 1 ; i< argc; i ++)

{

if(strlen(argv [i])> size)

{

if((string = realloc(string,(size + strlen(argv [i]))* 2))== NULL)

{

perror(无法分配内存);

退出(1);

}

}


string = strcat(string,argv [i]);

string = strcat(string,"");

}


printf("%s%s \ n",argv [0],string);


str = strtok(string," \ t \ n");

printf(&quo t;%s \ n",str);

while((str = strtok(NULL," \ t \ n"))= = NULL)

{

printf("%s \ n",str);

}

返回0;

}


然而你遇到的问题包括:

../a.out --search =" Hello World!"


我宁愿不必重新发明轮子,因为main()已经

实现它!必须/某些/方式它是内置功能!


Jeff



Indeed: (crude code)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int i;
char *string = NULL;
char *str = NULL;
int size = 0;

for ( i = 1; i < argc; i++ )
{
if ( strlen(argv[i]) > size )
{
if ( (string = realloc(string, (size + strlen(argv[i])) * 2)) == NULL )
{
perror("Could not allocate memory");
exit(1);
}
}

string = strcat(string, argv[i]);
string = strcat(string, " ");
}

printf("%s %s\n", argv[0], string);

str = strtok(string, " \t\n");
printf("%s\n", str);
while ( (str = strtok(NULL, " \t\n")) != NULL )
{
printf("%s\n", str);
}
return 0;
}

However you run into problems like:
../a.out --search="Hello World!"

I would prefer not to have to reinvent the wheel on this since main() already
implements it! There must be /some/ way that it''s a built-in function!

Jeff


12月12日星期五2003 22:02:24 -0700,Jeff Rodriguez

< ne ******** @ gurugeek.EXAMPLENOSPAM.com>在comp.lang.c中写道:
On Fri, 12 Dec 2003 22:02:24 -0700, Jeff Rodriguez
<ne********@gurugeek.EXAMPLENOSPAM.com> wrote in comp.lang.c:
Ben Pfaff写道:
Ben Pfaff wrote:
这取决于操作系统,shell和其他东西。如果您想要做的就是将字符串拆分为白色空间中的单词,我建议您只需编写代码即可。它确实不太难。
确实:(原始代码)
#include< stdio.h>
#include< string.h>
#include< unistd.h>
It depends on the operating system, shell, and other things. If
all you want to do is to break apart the string into words at
white space, I suggest you just write code to do it. It''s not
too hard.
Indeed: (crude code)
#include <stdio.h>
#include <string.h>
#include <unistd.h>




上面的标题在您的程序中都是非标准和不需要的。

#include< stdlib.h>

int main(int argc,char * argv [])
{
int i;
char * string = NULL;
char * str = NULL;
int size = 0;

for(i = 1; i< argc; i ++)
{
if(strlen(argv) [i])> size)
{
if((string = realloc(string,(size + strlen(argv [i]))* 2))== NULL)
{
perror(无法分配内存);
退出(1);
}


string = strcat(string,argv [一世]);


这很可能第一次崩溃,因为realloc返回的第一个块

是未初始化的,并且不知道它是否是

将包含一个终止''\ 0'',更不用说在第一个

元素中。

string = strcat(string," ;");
}

printf("%s%s \ n",argv [0],string);

str = strtok (string," \t\\\
");
printf("%s \ n",str);
while((str = strtok(NULL," \ t) \ n"))!= NULL)
{
printf("%s \ n",str);
}
返回0;
}

然而你遇到的问题包括:
./a.out --search =" Hello World!"

我宁愿不要重新发明轮子,因为main()已经实现了它!必须/某些/方式它是内置功能!

杰夫



The header above is both non-standard and unneeded in your program.
#include <stdlib.h>

int main(int argc, char *argv[])
{
int i;
char *string = NULL;
char *str = NULL;
int size = 0;

for ( i = 1; i < argc; i++ )
{
if ( strlen(argv[i]) > size )
{
if ( (string = realloc(string, (size + strlen(argv[i])) * 2)) == NULL )
{
perror("Could not allocate memory");
exit(1);
}
}

string = strcat(string, argv[i]);
This is quite likely to crash the first time, since the first block
that realloc returns is uninitialized and there''s no telling if it
will contain a terminating ''\0'' at all, let alone in the first
element.
string = strcat(string, " ");
}

printf("%s %s\n", argv[0], string);

str = strtok(string, " \t\n");
printf("%s\n", str);
while ( (str = strtok(NULL, " \t\n")) != NULL )
{
printf("%s\n", str);
}
return 0;
}

However you run into problems like:
./a.out --search="Hello World!"

I would prefer not to have to reinvent the wheel on this since main() already
implements it! There must be /some/ way that it''s a built-in function!

Jeff




不,main()还没有实现它。主机运行

系统或实现的启动代码都可以。这样做没有C

标准库函数。


如果你想要源代码,请试试Google。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c ++ http://www.parashift .com / c ++ - faq-lite /

alt.comp.lang.learn.c-c ++ ftp://snurse-l.org/pub/acllc-c++/faq



No, main() does not already implement it. Either the host operating
system or the implementation''s start-up code does. There is no C
standard library function to do so.

If you want source code, try Google.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


这篇关于解析选项的方式与传递给main的方式相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆