From 38e3f5463878a29e675be8430135e36b552c0ac1 Mon Sep 17 00:00:00 2001 From: Timo Teräs Date: Wed, 8 Oct 2014 15:29:27 +0300 Subject: rework option parsing to have a group structure Add also a new 'commit' group that is the common options for all applets that can commit package changes. --- src/add.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src/add.c') diff --git a/src/add.c b/src/add.c index 2a0a36c..9583ff2 100644 --- a/src/add.c +++ b/src/add.c @@ -21,8 +21,7 @@ struct add_ctx { unsigned short solver_flags; }; -static int add_parse(void *ctx, struct apk_db_options *dbopts, - int optch, int optindex, const char *optarg) +static int option_parse_applet(void *ctx, struct apk_db_options *dbopts, int optch, const char *optarg) { struct add_ctx *actx = (struct add_ctx *) ctx; @@ -37,11 +36,28 @@ static int add_parse(void *ctx, struct apk_db_options *dbopts, actx->virtpkg = optarg; break; default: - return -1; + return -ENOTSUP; } return 0; } +static const struct apk_option options_applet[] = { + { 0x10000, "initdb", "Initialize database" }, + { 'u', "upgrade", "Prefer to upgrade package" }, + { 't', "virtual", + "Instead of adding all the packages to 'world', create a new virtual " + "package with the listed dependencies and add that to 'world'; the " + "actions of the command are easily reverted by deleting the virtual " + "package", required_argument, "NAME" }, +}; + +static const struct apk_option_group optgroup_applet = { + .name = "Add", + .options = options_applet, + .num_options = ARRAY_SIZE(options_applet), + .parse = option_parse_applet, +}; + static int non_repository_check(struct apk_database *db) { if (apk_flags & APK_FORCE) @@ -149,16 +165,6 @@ static int add_main(void *ctx, struct apk_database *db, struct apk_string_array return r; } -static struct apk_option add_options[] = { - { 0x10000, "initdb", "Initialize database" }, - { 'u', "upgrade", "Prefer to upgrade package" }, - { 't', "virtual", - "Instead of adding all the packages to 'world', create a new virtual " - "package with the listed dependencies and add that to 'world'; the " - "actions of the command are easily reverted by deleting the virtual " - "package", required_argument, "NAME" }, -}; - static struct apk_applet apk_add = { .name = "add", .help = "Add PACKAGEs to 'world' and install (or upgrade) " @@ -166,9 +172,7 @@ static struct apk_applet apk_add = { .arguments = "PACKAGE...", .open_flags = APK_OPENF_WRITE, .context_size = sizeof(struct add_ctx), - .num_options = ARRAY_SIZE(add_options), - .options = add_options, - .parse = add_parse, + .optgroups = { &optgroup_global, &optgroup_commit, &optgroup_applet }, .main = add_main, }; -- cgit v1.2.3-60-g2f50