# #
#############################################################################*/
-#include <errno.h>
-#include <getopt.h>
-#include <stdlib.h>
+#include <argp.h>
#include <pakfire/pakfire.h>
-#include <pakfire/transaction.h>
+#include "command.h"
+#include "pakfire.h"
#include "remove.h"
#include "transaction.h"
+static const char* args_doc = "remove [OPTIONS...] PACKAGES...";
+
+static const char* doc = "Remove packages";
+
+#define MAX_PACKAGES 128
+
struct config {
int job_flags;
+
+ // Packages
+ char* packages[MAX_PACKAGES];
+ unsigned int num_packages;
};
-static void help(void) {
- printf(
- "%s [OPTIONS...] remove [OPTIONS...] PACKAGE...\n\n"
- "Options:\n"
- " --keep-dependencies Do not uninstall any dependencies\n"
- " -h --help Show help\n",
- program_invocation_short_name
- );
+enum {
+ OPT_KEEP_DEPENDENCIES = 1,
+};
- exit(0);
-}
+static struct argp_option options[] = {
+ { "keep-dependencies", OPT_KEEP_DEPENDENCIES, NULL, 0, "Keep previously installed dependencies", 0 },
+ { NULL },
+};
-static int parse_argv(struct config* config, int argc, char* argv[]) {
- enum {
- ARG_KEEP_DEPS,
- };
-
- static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { "keep-dependencies", no_argument, NULL, ARG_KEEP_DEPS },
- { NULL },
- };
- int c;
-
- for (;;) {
- c = getopt_long(argc, argv, "h", options, NULL);
- if (c < 0)
- break;
+static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
+ struct config* config = data;
- switch (c) {
- case 'h':
- help();
+ switch (key) {
+ case OPT_KEEP_DEPENDENCIES:
+ config->job_flags |= PAKFIRE_JOB_KEEP_DEPS;
+ break;
- case ARG_KEEP_DEPS:
- config->job_flags |= PAKFIRE_JOB_KEEP_DEPS;
- break;
+ case ARGP_KEY_ARG:
+ if (config->num_packages >= MAX_PACKAGES)
+ return -ENOBUFS;
- case '?':
- return -EINVAL;
+ config->packages[config->num_packages++] = arg;
+ break;
- default:
- break;
- }
+ default:
+ return ARGP_ERR_UNKNOWN;
}
return 0;
struct config* config = (struct config*)data;
int r;
- // Add the remaining command line options as packages
- for (int i = 0; i < argc; i++) {
+ for (unsigned int i = 0; i < config->num_packages; i++) {
r = pakfire_transaction_request(transaction,
- PAKFIRE_JOB_ERASE, argv[i], config->job_flags);
+ PAKFIRE_JOB_ERASE, config->packages[i], config->job_flags);
if (r) {
fprintf(stderr, "Could not find '%s': %m\n", argv[i]);
return r;
return 0;
}
-int cli_remove(struct pakfire* pakfire, int argc, char* argv[]) {
- struct config config = {
- .job_flags = 0,
- };
+int cli_remove(void* data, int argc, char* argv[]) {
+ struct pakfire* pakfire = NULL;
+ struct config config = {};
int r;
- // Parse CLI options
- r = parse_argv(&config, argc, argv);
+ struct cli_config* cli_config = data;
+
+ // Parse the command line
+ r = cli_parse(options, NULL, args_doc, doc, parse, argc, argv, &config);
if (r)
- return r;
+ goto ERROR;
+
+ // Setup Pakfire
+ r = cli_setup_pakfire(&pakfire, cli_config);
+ if (r)
+ goto ERROR;
+
+ r = cli_transaction(pakfire, argc, argv, 0, __cli_remove, &config);
+
+ERROR:
+ if (pakfire)
+ pakfire_unref(pakfire);
- return cli_transaction(pakfire, argc, argv, 0, __cli_remove, &config);
+ return r;
}
{ "info", cli_info, 1, -1, 0 },
{ "install", cli_install, 1, -1, 0 },
{ "provides", cli_provides, 1, -1, 0 },
- //{ "remove", cli_remove, 1, -1, 0 },
+ { "remove", cli_remove, 1, -1, 0 },
{ "repolist", cli_repolist, 0, 0, 0 },
{ "requires", cli_requires, 1, -1, 0 },
{ "search", cli_search, 1, -1, 0 },