From: Michael Tremer Date: Sat, 30 Sep 2023 10:17:29 +0000 (+0000) Subject: cli: Fix parsing command options X-Git-Tag: 0.9.30~1596 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=101b03bb3ae2c868f0d4f6eacb8e1c433795e095;p=pakfire.git cli: Fix parsing command options Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/build.c b/src/cli/lib/build.c index 2e739bcfa..eb2e20f59 100644 --- a/src/cli/lib/build.c +++ b/src/cli/lib/build.c @@ -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) { diff --git a/src/cli/lib/command.c b/src/cli/lib/command.c index 533a2634e..6fa3f7bf1 100644 --- a/src/cli/lib/command.c +++ b/src/cli/lib/command.c @@ -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); } diff --git a/src/cli/lib/dist.c b/src/cli/lib/dist.c index 876cc64ca..c78f2a302 100644 --- a/src/cli/lib/dist.c +++ b/src/cli/lib/dist.c @@ -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; diff --git a/src/cli/lib/info.c b/src/cli/lib/info.c index 1ecdf92be..85079c7a9 100644 --- a/src/cli/lib/info.c +++ b/src/cli/lib/info.c @@ -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; diff --git a/src/cli/lib/install.c b/src/cli/lib/install.c index 60783366b..b8fc4a8f1 100644 --- a/src/cli/lib/install.c +++ b/src/cli/lib/install.c @@ -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) { diff --git a/src/cli/lib/provides.c b/src/cli/lib/provides.c index 5ad43fccf..9afe350ea 100644 --- a/src/cli/lib/provides.c +++ b/src/cli/lib/provides.c @@ -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; diff --git a/src/cli/lib/remove.c b/src/cli/lib/remove.c index 842e490d4..8d86f8b1e 100644 --- a/src/cli/lib/remove.c +++ b/src/cli/lib/remove.c @@ -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) { diff --git a/src/cli/lib/requires.c b/src/cli/lib/requires.c index 183fcca77..7cd23dedd 100644 --- a/src/cli/lib/requires.c +++ b/src/cli/lib/requires.c @@ -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; diff --git a/src/cli/lib/search.c b/src/cli/lib/search.c index 8666a7827..5119ccc80 100644 --- a/src/cli/lib/search.c +++ b/src/cli/lib/search.c @@ -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; diff --git a/src/cli/lib/update.c b/src/cli/lib/update.c index 98950c250..a3fef3397 100644 --- a/src/cli/lib/update.c +++ b/src/cli/lib/update.c @@ -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]); diff --git a/src/cli/pakfire-builder.c b/src/cli/pakfire-builder.c index c84bcfaca..e2c04fbf9 100644 --- a/src/cli/pakfire-builder.c +++ b/src/cli/pakfire-builder.c @@ -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) diff --git a/src/cli/pakfire.c b/src/cli/pakfire.c index ec64a72d5..d25657867 100644 --- a/src/cli/pakfire.c +++ b/src/cli/pakfire.c @@ -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)