# #
#############################################################################*/
-#include <errno.h>
-#include <getopt.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <argp.h>
#include <pakfire/pakfire.h>
-#include <pakfire/transaction.h>
+#include "command.h"
+#include "pakfire.h"
#include "sync.h"
#include "transaction.h"
+static const char* args_doc = "sync [OPTIONS...]";
+
+static const char* doc = "Synchronize packages";
+
struct config {
int job_flags;
};
-static void help(void) {
- printf(
- "%s [OPTIONS...] sync [OPTIONS...]\n\n"
- "Options:\n"
- " --keep-orphaned Do not uninstall orphaned packages\n"
- " -h --help Show help\n",
- program_invocation_short_name
- );
-
- exit(0);
-}
-
-static int parse_argv(struct config* config, int argc, char* argv[]) {
- enum {
- ARG_KEEP_ORPHANED,
- };
-
- static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { "keep-orphaned", no_argument, NULL, ARG_KEEP_ORPHANED },
- { NULL },
- };
- int c;
-
- for (;;) {
- c = getopt_long(argc, argv, "h", options, NULL);
- if (c < 0)
- break;
+enum {
+ OPT_KEEP_ORPHANED = 1,
+};
- switch (c) {
- case 'h':
- help();
+static struct argp_option options[] = {
+ { "keep-orphaned", OPT_KEEP_ORPHANED, NULL, 0, "Keep orphaned packages", 0 },
+ { NULL },
+};
- case ARG_KEEP_ORPHANED:
- config->job_flags |= PAKFIRE_JOB_KEEP_ORPHANED;
- break;
+static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
+ struct config* config = data;
- case '?':
- return -EINVAL;
+ switch (key) {
+ case OPT_KEEP_ORPHANED:
+ config->job_flags |= PAKFIRE_JOB_KEEP_ORPHANED;
+ break;
- default:
- break;
- }
+ default:
+ return ARGP_ERR_UNKNOWN;
}
return 0;
return 0;
}
-int cli_sync(struct pakfire* pakfire, int argc, char* argv[]) {
- struct config config = {
- .job_flags = 0,
- };
+int cli_sync(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_sync, &config);
+
+ERROR:
+ if (pakfire)
+ pakfire_unref(pakfire);
- return cli_transaction(pakfire, argc, argv, 0, __cli_sync, &config);
+ return r;
}
{ "repolist", cli_repolist, 0, 0, 0 },
{ "requires", cli_requires, 1, -1, 0 },
{ "search", cli_search, 1, -1, 0 },
- //{ "sync", cli_sync, 0, 0, 0 },
+ { "sync", cli_sync, 0, 0, 0 },
//{ "update", cli_update, 0, -1, 0 },
{ NULL },
};