c - 处理 C 命令行参数

标签 c parsing command-line parameters package-managers

我如何处理多个命令行参数,例如在包管理器中的 say?我正在尝试编写一个包管理器,现在接受要安装的包让我很头疼。例如,用户想要安装包 x、y 和 z。现在我的代码将向一个函数发送 3 个不同的请求。我想一次性得到所有的包名。因此,例如,用户想要安装包 x、y 和 z,它会被处理,我的代码将向函数发送请求,说明它需要 x、y 和 z,该函数将立即开始工作。

这是我当前的实现...

case 'S':
    switch (argv[1][2]) {
        case 'u':
            id = 1;
            alfred("system", "update", "", id);
            break;

        case 'r':
            id = 1;
            alfred("system", "reload", "", id);
            break;

        case 'i':
            if (argc - 2 != 0) {
                // Loop until packages are complete.
                packages = 2; // Starting point of packages = argv[2][0]
                srand(time(NULL)); // Seed for random number
                id = rand(); // Generate random number for id
                argc = argc - 2 + 1; // argc minus the number of packages and plus 1
                /* This is a very inefficent loop! */
                /* Must get all targets and feed it to alfred */
                while (packages <= argc) {
                    alfred("system", "install", &argv[packages][0], id);
                    packages++;
                }
            } else {
                printf("Unrecognized format. Execute alfred -h for more information.\n");
            }
            break;

        default:
            printf("Unrecognized format. Execute alfred -h for more information.\n");
            break;
    }
    break;

最佳答案

你也可以试试libargtable . 我以前用它来解析各种参数,非常方便。

关于c - 处理 C 命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16539213/

相关文章:

c - 如何在代码中使用mount函数来挂载ntfs-3g?

ruby - 安装了两个版本的 Ruby。删除了旧的并以某种方式卡住了

php - 如何使用 php 在控制台中设置文本样式? [在 PHP CLI 中运行]

r - 相当于 R 中的 Stata 选项卡命令

c - fprintf() 线程安全吗?

c - 有没有人对 C 的设计和错误跟踪/控制系统有很好的指导?

从没有自定义头文件的多个 C 文件调用方法,Makefile 链接

node.js - 在 NodeJS 中将 200'000 行以上的大型 csv 文件插入 MongoDB

php - 如何在没有意外匹配的情况下在 PHP 的字符串中找到整个单词?

linux - linux "gdb a.out param1 param2"是否将 param1 和 2 传递给 "gdb"或 "a.out"?