apk: added option --version/-V

Both variants should work:
 apk --version
 apk <applet> --version
cute-signatures
Natanael Copa 2009-01-17 08:51:52 +00:00
parent f88f8205a7
commit 24b424a458
1 changed files with 25 additions and 14 deletions

View File

@ -38,13 +38,17 @@ void apk_log(const char *prefix, const char *format, ...)
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
int version(void)
{
printf("apk-tools " APK_VERSION "\n");
return 0;
}
int usage(void) int usage(void)
{ {
struct apk_applet **a, *applet; struct apk_applet **a, *applet;
printf("apk-tools " APK_VERSION "\n" version();
"\n" printf("\nUsage:\n");
"Usage:\n");
for (a = &__start_apkapplets; a < &__stop_apkapplets; a++) { for (a = &__start_apkapplets; a < &__stop_apkapplets; a++) {
applet = *a; applet = *a;
@ -95,12 +99,13 @@ static struct apk_applet *deduce_applet(int argc, char **argv)
return NULL; return NULL;
} }
#define NUM_GENERIC_OPTS 5 #define NUM_GENERIC_OPTS 6
static struct option generic_options[32] = { static struct option generic_options[32] = {
{ "root", required_argument, NULL, 'p' }, { "root", required_argument, NULL, 'p' },
{ "repository", required_argument, NULL, 'X' }, { "repository", required_argument, NULL, 'X' },
{ "quiet", no_argument, NULL, 'q' }, { "quiet", no_argument, NULL, 'q' },
{ "verbose", no_argument, NULL, 'v' }, { "verbose", no_argument, NULL, 'v' },
{ "version", no_argument, NULL, 'V' },
{ "progress", no_argument, NULL, 0x100 }, { "progress", no_argument, NULL, 0x100 },
}; };
@ -117,14 +122,17 @@ int main(int argc, char **argv)
apk_root = getenv("ROOT"); apk_root = getenv("ROOT");
applet = deduce_applet(argc, argv); applet = deduce_applet(argc, argv);
if (applet == NULL) if (applet != NULL) {
return usage(); if (applet->num_options && applet->options) {
memcpy(&generic_options[NUM_GENERIC_OPTS],
applet->options,
applet->num_options * sizeof(struct option));
}
if (applet->num_options && applet->options) { if (applet->context_size != 0)
memcpy(&generic_options[NUM_GENERIC_OPTS], ctx = calloc(1, applet->context_size);
applet->options,
applet->num_options * sizeof(struct option));
} }
for (opt = &generic_options[0], sopt = short_options; for (opt = &generic_options[0], sopt = short_options;
opt->name != NULL; opt++) { opt->name != NULL; opt++) {
if (opt->flag == NULL && if (opt->flag == NULL &&
@ -135,9 +143,6 @@ int main(int argc, char **argv)
} }
} }
if (applet->context_size != 0)
ctx = calloc(1, applet->context_size);
optindex = 0; optindex = 0;
while ((r = getopt_long(argc, argv, short_options, while ((r = getopt_long(argc, argv, short_options,
generic_options, &optindex)) != -1) { generic_options, &optindex)) != -1) {
@ -154,11 +159,14 @@ int main(int argc, char **argv)
case 'v': case 'v':
apk_verbosity++; apk_verbosity++;
break; break;
case 'V':
return version();
break;
case 0x100: case 0x100:
apk_progress = 1; apk_progress = 1;
break; break;
default: default:
if (applet->parse == NULL) if (applet == NULL || applet->parse == NULL)
return usage(); return usage();
if (applet->parse(ctx, r, optindex - NUM_GENERIC_OPTS, if (applet->parse(ctx, r, optindex - NUM_GENERIC_OPTS,
optarg) != 0) optarg) != 0)
@ -167,6 +175,9 @@ int main(int argc, char **argv)
} }
} }
if (applet == NULL)
return usage();
if (apk_root == NULL) if (apk_root == NULL)
apk_root = "/"; apk_root = "/";