]> git.ipfire.org Git - pakfire.git/commitdiff
cli: Fix parsing command options
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 30 Sep 2023 10:17:29 +0000 (10:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 30 Sep 2023 10:17:29 +0000 (10:17 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
12 files changed:
src/cli/lib/build.c
src/cli/lib/command.c
src/cli/lib/dist.c
src/cli/lib/info.c
src/cli/lib/install.c
src/cli/lib/provides.c
src/cli/lib/remove.c
src/cli/lib/requires.c
src/cli/lib/search.c
src/cli/lib/update.c
src/cli/pakfire-builder.c
src/cli/pakfire.c

index 2e739bcfa2f2857f8600117fadd5f558b8a6cde1..eb2e20f595534e00016b198fd993eb5980896f4d 100644 (file)
@@ -108,7 +108,7 @@ static int parse_argv(struct config* config, int argc, char* argv[]) {
                                break;
 
                        case '?':
-                               return -EINVAL;
+                               break;
 
                        default:
                                break;
@@ -167,7 +167,7 @@ int cli_build(struct pakfire* pakfire, int argc, char* argv[]) {
        }
 
        // Process all packages
-       for (int i = optind; i < argc; i++) {
+       for (int i = optind + 1; i < argc; i++) {
                // Run the build
                r = pakfire_build_exec(build, argv[i]);
                if (r) {
index 533a2634e914fd5fd3daefb0c2268396e45c5e3d..6fa3f7bf1e59c537c1d3d95159a9e0dc076b0170 100644 (file)
@@ -26,6 +26,9 @@
 
 #include "command.h"
 
+// Do not let getopt_long print any error messages
+int opterr = 0;
+
 static const struct command* command_find(const struct command* commands, const char* verb) {
        for (const struct command* command = commands; command->verb; command++) {
                if (strcmp(command->verb, verb) == 0)
@@ -38,12 +41,12 @@ static const struct command* command_find(const struct command* commands, const
 int command_dispatch(struct pakfire* pakfire, const struct command* commands, int argc, char* argv[]) {
        const struct command* command = NULL;
 
-       if (argc < 2) {
+       if (optind >= argc) {
                fprintf(stderr, "Command required\n");
                return -EINVAL;
        }
 
-       const char* verb = argv[1];
+       const char* verb = argv[optind];
 
        // Find a matching command
        command = command_find(commands, verb);
@@ -52,12 +55,8 @@ int command_dispatch(struct pakfire* pakfire, const struct command* commands, in
                return -EINVAL;
        }
 
-       // Consume the verb
-       argv++;
-       argc--;
-
-       // Reset optind
-       optind = 1;
+       // Reset optind (to parse everything again)
+       optind = 0;
 
        return command->callback(pakfire, argc, argv);
 }
index 876cc64caed0d02cd437a1c5dc6f0c59d64b818c..c78f2a302a2e9646311770bc0e37d48313512da3 100644 (file)
@@ -90,7 +90,7 @@ int cli_dist(struct pakfire* pakfire, int argc, char* argv[]) {
                config.resultdir = getcwd(cwd, sizeof(cwd));
 
        // Run for all arguments
-       for (int i = optind; i < argc; i++) {
+       for (int i = optind + 1; i < argc; i++) {
                r = pakfire_dist(pakfire, argv[i], config.resultdir, NULL);
                if (r)
                        break;
index 1ecdf92bec81e8d25bca280a165616c975984fdc..85079c7a9bb9fbb2d3ead5e328a24933b4c00423 100644 (file)
@@ -107,7 +107,7 @@ int cli_info(struct pakfire* pakfire, int argc, char* argv[]) {
                goto ERROR;
 
        // Perform search
-       for (int i = optind; i < argc; i++) {
+       for (int i = optind + 1; i < argc; i++) {
                r = pakfire_search(pakfire, argv[i], PAKFIRE_SEARCH_NAME_ONLY, list);
                if (r)
                        goto ERROR;
index 60783366b81abe3049f918622403051a60d78110..b8fc4a8f113119fe506aa1b845c4cc8026946043 100644 (file)
@@ -106,7 +106,7 @@ static int __cli_install(struct pakfire_transaction* transaction, int argc, char
        int r;
 
        // Add the remaining command line options as packages
-       for (int i = optind; i < argc; i++) {
+       for (int i = optind + 1; i < argc; i++) {
                r = pakfire_transaction_request(transaction,
                                PAKFIRE_JOB_INSTALL, argv[i], config->job_flags);
                if (r) {
index 5ad43fccf3dc95fe4de28f2b023613add5551e3b..9afe350eaeb7174dbf14d11ff1744aaac77ed27e 100644 (file)
@@ -33,7 +33,7 @@ int cli_provides(struct pakfire* pakfire, int argc, char* argv[]) {
                goto ERROR;
 
        // Perform search
-       for (int i = optind; i < argc; i++) {
+       for (int i = optind + 1; i < argc; i++) {
                r = pakfire_whatprovides(pakfire, argv[i], 0, list);
                if (r)
                        goto ERROR;
index 842e490d4ea09ac808d6f17c317144621aeba42a..8d86f8b1ef2f79a6b19168e743149f151df9104b 100644 (file)
@@ -85,7 +85,7 @@ static int __cli_remove(struct pakfire_transaction* transaction, int argc, char*
        int r;
 
        // Add the remaining command line options as packages
-       for (int i = optind; i < argc; i++) {
+       for (int i = optind + 1; i < argc; i++) {
                r = pakfire_transaction_request(transaction,
                                PAKFIRE_JOB_ERASE, argv[i], config->job_flags);
                if (r) {
index 183fcca77ced8ffa57fe1952a4e44c2d8ac5ab48..7cd23dedd7dec5d0c0b8713e0ba25d2828da98be 100644 (file)
@@ -33,7 +33,7 @@ int cli_requires(struct pakfire* pakfire, int argc, char* argv[]) {
                goto ERROR;
 
        // Perform search
-       for (int i = optind; i < argc; i++) {
+       for (int i = optind + 1; i < argc; i++) {
                r = pakfire_whatrequires(pakfire, argv[i], 0, list);
                if (r)
                        goto ERROR;
index 8666a78278d5e2f53660a5dd9bbb96527ec1c42a..5119ccc80c1677ff24eb51e701ccaadb9d40c881 100644 (file)
@@ -33,7 +33,7 @@ int cli_search(struct pakfire* pakfire, int argc, char* argv[]) {
                goto ERROR;
 
        // Perform search
-       for (int i = optind; i < argc; i++) {
+       for (int i = optind + 1; i < argc; i++) {
                r = pakfire_search(pakfire, argv[i], 0, list);
                if (r)
                        goto ERROR;
index 98950c250c0424922878e5ea80bd3154c5dbb425..a3fef339780bb374f7f6bafc1ce008483de3565d 100644 (file)
@@ -116,7 +116,7 @@ static int __cli_update(struct pakfire_transaction* transaction, int argc, char*
        // Did the user pass any packages?
        if (optind < argc) {
                // Add the remaining command line options as packages
-               for (int i = optind; i < argc; i++) {
+               for (int i = optind + 1; i < argc; i++) {
                        r = pakfire_transaction_request(transaction, PAKFIRE_JOB_UPDATE, argv[i], 0);
                        if (r) {
                                fprintf(stderr, "Could not find '%s': %m\n", argv[i]);
index c84bcfaca047e9909915d5858df656697fc370ef..e2c04fbf989f21de3707869b955c6026fb87872d 100644 (file)
@@ -163,7 +163,7 @@ static int parse_argv(struct config* config, int argc, char* argv[]) {
                                break;
 
                        case '?':
-                               return -EINVAL;
+                               break;
 
                        default:
                                break;
@@ -273,7 +273,7 @@ int main(int argc, char* argv[]) {
                goto ERROR;
 
        // Run a command
-       r = cli_main(pakfire, argc - optind + 1, argv + optind - 1);
+       r = cli_main(pakfire, argc, argv);
 
 ERROR:
        if (pakfire)
index ec64a72d53b2130240312f6f633ad3af10a646a6..d2565786764f7db341ef84fcd6159abd7b8a0039 100644 (file)
@@ -185,7 +185,7 @@ static int parse_argv(struct config* config, int argc, char* argv[]) {
                                break;
 
                        case '?':
-                               return -EINVAL;
+                               break;
 
                        default:
                                break;
@@ -274,7 +274,7 @@ int main(int argc, char* argv[]) {
                cli_set_repo_enabled(pakfire, config.disable_repos[i], 0);
 
        // Run a command
-       r = cli_main(pakfire, argc - optind + 1, argv + optind - 1);
+       r = cli_main(pakfire, argc, argv);
 
 ERROR:
        if (f)