处理命令行参数? [英] Handle command line arguments?

查看:73
本文介绍了处理命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我可以使程序全部运行,但是如果得到提示

So, I can make my program run and all, but if I'm given

$ ./a.out -f Text.txt

我只是不确定如何获取程序来建立连接-f表示文件。这样做的逻辑是什么?

I'm just not sure how to get the program to make the connection that -f indicates a file. What is the logic for doing this?

推荐答案

main 函数具有签名 int main(int argc,char ** argv);因此您可以使用 argc (是肯定的)& argv 参数。 argv 数组保证具有 argc + 1 元素。最后一个总是 NULL 。其他是非零,非混淆的零字节终止字符串。请注意,通常 shell 浏览程序中的参数之前, execve(2):参见 glob(7)

The main function has signature int main(int argc, char**argv); so you can use the argc (which is positive) & argv arguments. The argv array is guaranteed to have argc+1 elements. The last is always NULL. The others are non-nil, non-aliased zero-byte terminated strings. Notice that often some shell is globbing the arguments before your program is started by execve(2): see glob(7).

例如,如果您键入(在Linux终端中) myprog -ia *。 c -o foo.txt ,并且如果您现在输入该外壳已扩展(通过通气)将 a * .c 转换为 a1.c a2.c (因为这是仅有的名称以 a 开头且具有 .c 后缀(在当前目录中),您的 myprog 可执行文件 main 程序将被调用

For example, if you type (in a Linux terminal) myprog -i a*.c -o foo.txt and if at the moment you type that the shell has expanded (by globbing) a*.c into a1.c and a2.c (because these are the only files whose name start with a and have a .c suffix in the current directory), your myprog executable main program is called with


  • argc == 6

  • argv [0] 包含 myprog (因此您可以测试一下 strcmp(argv [0], myprog)== 0

  • argv [1] 包含-i

  • argv [2] 包含 a1.c

  • argv [3] a2.c

  • argv [4] 包含-o

  • argv [5] 包含 foo.txt c>

  • argv [6] NULL 指针

  • argc==6
  • argv[0] containing "myprog" (so you could test that strcmp(argv[0],"myprog") == 0)
  • argv[1] containing "-i"
  • argv[2] containing "a1.c"
  • argv[3] containing "a2.c"
  • argv[4] containing "-o"
  • argv[5] containing "foo.txt"
  • argv[6] being the NULL pointer

此外,您可以保证(内核执行 execve(2))表示所有6个 argv 指针是不同的,非别名,并且不重叠。

In addition you are guaranteed (by the kernel doing the execve(2)) that all the 6 argv pointers are distinct, non-aliasing, and non-overlapping.

GNU libc为您提供几种方法来解析这些参数: getopt & argp getopt 在POSIX中是标准化的(但是GNU也为您提供了非常有用的< a href = http://man7.org/linux/man-pages/man3/getopt.3.html rel = nofollow> getopt_long(3))

GNU libc gives you several ways to parse these arguments: getopt & argp. getopt is standardized in POSIX (but GNU gives you also the very useful getopt_long(3))

我强烈建议您遵循 GNU约定:至少接受-help -version

I strongly suggest you to follow GNU conventions: accept at least --help and --version

例如 -f 是用于某些选项的,并非通常使用文件名(但请参见程序参数中使用- )。如果您碰巧真的想要一个名为 -f 文件(这是一个非常糟糕的主意),请使用。/- f

The fact that e.g. -f is for some option and not a file name is often conventional (but see use of -- in program arguments). If you happen to really want a file named -f (which is a very bad idea), use ./-f

多个外壳具有自动补全。您需要为此配置它们(甚至可能为您自己的程序配置它们)。

Several shells have autocompletion. You need to configure them for that (and you might configure them even for your own programs).

这篇关于处理命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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