From a625f557c5c896b64b535f95be155fbba2a00b88 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Tue, 7 May 2013 16:24:34 +0200 Subject: [PATCH] mimeset: Change options style * Use "--" prefix for long options and switch to getopt parsing. There's still compatibility support for "-apps" and "-all", but they are considered obsolete, now. * Add short options "-a" for "--apps", "-A" for "--all", and "-h" for "--help". --- src/bin/mimeset.cpp | 106 +++++++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 31 deletions(-) diff --git a/src/bin/mimeset.cpp b/src/bin/mimeset.cpp index 60d7a74bdd..abb54a1de1 100644 --- a/src/bin/mimeset.cpp +++ b/src/bin/mimeset.cpp @@ -4,6 +4,7 @@ */ +#include #include #include #include @@ -28,15 +29,31 @@ int gForce = B_UPDATE_MIME_INFO_NO_FORCE; static void usage(int status) { - printf("usage: %s [OPTION]... [PATH]...\n" - " -all\t\tcombine default action and the -apps option\n" - " -apps\t\tupdate 'app' and 'meta_mime' information\n" - " -f\t\tforce updating, even if previously updated\n" - " \t (will not overwrite the 'type' of a file)\n" - " -F\t\tforce updating, even if previously updated\n" - " \t (will overwrite the 'type' of a file)\n" - " --help\tdisplay this help information\n" - "When PATH is @, file names are read from stdin\n\n", + printf("Usage: %s ...\n" + "Recursively updates the MIME related attributes (e.g. file type) for\n" + "the given files. Alternatively or additionally encountered\n" + "applications are entered into the MIME database. When \"@\" is\n" + "specified as , file paths are read from stdin.\n" + "\n" + "Options:\n" + " -A, --all\n" + " Update the files' MIME information and enter applications into\n" + " the MIME database.\n" + " -a, --apps\n" + " Only enter applications into the MIME database.\n" + " -f\n" + " Force updating, even if previously updated, but do not overwrite\n" + " the type of a file.\n" + " -F\n" + " Force updating, even if previously updated. Also overwrite the\n" + " type of a file.\n" + " -h, --help\n" + " Display this help information.\n" + "\n" + "Obsolete options:\n" + " -all (synonymous with --all)\n" + " -apps (synonymous with --apps)\n" + "\n", sProgramName); exit(status); @@ -66,41 +83,68 @@ process_file(const char* path) int -main(int argc, const char* const* argv) +main(int argc, const char** argv) { // parse arguments - if (argc < 2) - usage(1); - - while (*++argv != NULL) { - const char* arg = *argv; + // replace old-style options first + for (int i = 1; i < argc; i++) { + const char* arg = argv[i]; if (*arg != '-') break; - if (strcmp(arg, "-all") == 0) - gApps = true; - else if (strcmp(arg, "-apps") == 0) { - gApps = true; - gFiles = false; - } else if (strcmp(arg, "-f") == 0) - gForce = B_UPDATE_MIME_INFO_FORCE_KEEP_TYPE; - else if (strcmp(arg, "-F") == 0) - gForce = B_UPDATE_MIME_INFO_FORCE_UPDATE_ALL; - else if (strcmp(arg, "--help") == 0) - usage(0); - else { - fprintf(stderr, "unknown option \"%s\"\n", arg); - usage(1); + argv[i] = "--all"; + else if (strcmp(arg, "-apps") == 0) + argv[i] = "--apps"; + } + + for (;;) { + static struct option sLongOptions[] = { + { "all", no_argument, 0, 'A' }, + { "apps", no_argument, 0, 'a' }, + { "help", no_argument, 0, 'h' }, + { 0, 0, 0, 0 } + }; + + opterr = 0; // don't print errors + int c = getopt_long(argc, (char**)argv, "aAfFh", sLongOptions, + NULL); + if (c == -1) + break; + + switch (c) { + case 'a': + gApps = true; + gFiles = false; + break; + case 'A': + gApps = true; + gFiles = true; + break; + case 'f': + gForce = B_UPDATE_MIME_INFO_FORCE_KEEP_TYPE; + break; + case 'F': + gForce = B_UPDATE_MIME_INFO_FORCE_UPDATE_ALL; + break; + case 'h': + usage(0); + break; + default: + usage(1); + break; } } + if (argc - optind < 1) + usage(1); + // process files BApplication app("application/x-vnd.haiku.mimeset"); - while (*argv != NULL) { - const char* arg = *argv++; + for (; optind < argc; optind++) { + const char* arg = argv[optind]; if (strcmp(arg, "@") == 0) { // read file names from stdin