简体   繁体   English

解析命令行参数而不指定参数数量C ++

[英]Parsing command line arguments without specifying the number of arguments C++

This is probably a lot more simple than I am making it, but I am having trouble parsing a command line argument that is not given a size argument (ie I'm asking for int main(char* argv[]) rather than using int main(int argc, char* argv[]) ). 这可能比我做的要简单得多,但是我在解析没有给出大小参数的命令行参数时遇到了麻烦(即我要的是int main(char * argv [])而不是使用int main(int argc,char * argv []) )。 I suppose this is really more of a two part question, but I was wondering (1) can you run a program with command line arguments that look like the following?: 我想这实际上是一个由两部分组成的问题,但是我想知道(1)您是否可以运行带有如下命令行参数的程序?

testProgram arg1 arg2 arg3

where arg1, arg2, and arg3 would be placed into argv[], and (2) if you can, how do you cycle through each argument so that you might be able to use them within the program? 将arg1,arg2和arg3放在argv []中,以及(2)(如果可以),如何循环遍历每个参数,以便可以在程序中使用它们?

I know this probably seems very arbitrary, but I'm trying to understand better how command line arguments work and how to use them properly. 我知道这似乎很随意,但是我试图更好地理解命令行参数如何工作以及如何正确使用它们。

Thanks for the help and insight. 感谢您的帮助和见解。

The signature 签名

int main(char* argv[])

is not supported by any C++ compiler I know of. 我所知道的任何C ++编译器都不支持。 Admittedly very few nowadays. 不可否认,如今很少。 But still. 但是还是。


The signature 签名

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

is standard and works well for pure ASCII arguments. 是标准的,非常适合纯ASCII参数。

It also works well for general Unicode arguments in Unix-land, when they're encoded as UTF-8. 当它们被编码为UTF-8时,它也适用于Unix-land中的通用Unicode参数。

Anyway, it tells you the number of arguments. 无论如何,它告诉您参数的数量。

argc tells you how many arguments the program received. argc告诉您程序接收了多少个参数。 So argv[0] refers to the name of the program itself, argv[1] to the first argument you passed on the command line, argv[2] to the next, and so on up to argv[argc-1] , which is the last one. 因此argv[0]引用程序本身的名称, argv[1]引用您在命令行上传递的第一个参数, argv[2]引用下一个参数,依此类推,直到argv[argc-1]为止是最后一个。 That's followed by argv[argc] , which is a null pointer. 接下来是argv[argc] ,它是一个空指针。

So, when you run the program, you don't have to specify the number of arguments--you just pass whatever number of arguments you want to. 因此,在运行程序时,您不必指定参数数量,而只需传递所需数量的参数即可。 Some code in the library then goes through, splits what you pass up into individual arguments, and tells the program how many it found. 然后,库中的一些代码将通过,将您传递的内容拆分为单个参数,并告诉程序找到了多少个代码。

IOW, when you run: IOW,当您运行时:

testProgram arg1 arg2 arg3 testProgram arg1 arg2 arg3

your program is going to receive: 您的程序将收到:

argv[0] = testProgram
argv[1] = arg1
argv[2] = arg2
argv[3] = arg3
argv[4] = null pointer
argc = 4

Note: the exact content of argv[0] is open to variation--it might be exactly what you typed to invoke the command, or it might be a full path to the command, or it might (especially on ancient systems) be an empty string. 注意: argv[0]的确切内容容易变化-它可能正是您键入的内容以调用命令,或者它可能是命令的完整路径,或者(尤其是在古老的系统中)空字符串。 On most modern systems, it'll normally contain something like the file name though. 在大多数现代系统上,它通常会包含类似文件名的内容。

You can ignore the argc argument and process the argv[] elements until you get to the one that's NULL since the argv array is specified to have a sentinel element that's NULL . 您可以忽略argc参数并处理argv[]元素,直到到达NULL为止,因为argv数组被指定为具有NULL的sentinel元素。 I'm not sure what the ultimate point of the question is though. 我不确定这个问题的最终目的是什么。

#include <stdio.h>

int main(int argc, char** argv) 
{
    while (*argv) {
        puts(*argv);
        ++argv;
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM