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".
This commit is contained in:
+75
-31
@@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <getopt.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -28,15 +29,31 @@ int gForce = B_UPDATE_MIME_INFO_NO_FORCE;
|
|||||||
static void
|
static void
|
||||||
usage(int status)
|
usage(int status)
|
||||||
{
|
{
|
||||||
printf("usage: %s [OPTION]... [PATH]...\n"
|
printf("Usage: %s <options> <path> ...\n"
|
||||||
" -all\t\tcombine default action and the -apps option\n"
|
"Recursively updates the MIME related attributes (e.g. file type) for\n"
|
||||||
" -apps\t\tupdate 'app' and 'meta_mime' information\n"
|
"the given files. Alternatively or additionally encountered\n"
|
||||||
" -f\t\tforce updating, even if previously updated\n"
|
"applications are entered into the MIME database. When \"@\" is\n"
|
||||||
" \t (will not overwrite the 'type' of a file)\n"
|
"specified as <path>, file paths are read from stdin.\n"
|
||||||
" -F\t\tforce updating, even if previously updated\n"
|
"\n"
|
||||||
" \t (will overwrite the 'type' of a file)\n"
|
"Options:\n"
|
||||||
" --help\tdisplay this help information\n"
|
" -A, --all\n"
|
||||||
"When PATH is @, file names are read from stdin\n\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);
|
sProgramName);
|
||||||
|
|
||||||
exit(status);
|
exit(status);
|
||||||
@@ -66,41 +83,68 @@ process_file(const char* path)
|
|||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, const char* const* argv)
|
main(int argc, const char** argv)
|
||||||
{
|
{
|
||||||
// parse arguments
|
// parse arguments
|
||||||
|
|
||||||
if (argc < 2)
|
// replace old-style options first
|
||||||
usage(1);
|
for (int i = 1; i < argc; i++) {
|
||||||
|
const char* arg = argv[i];
|
||||||
while (*++argv != NULL) {
|
|
||||||
const char* arg = *argv;
|
|
||||||
if (*arg != '-')
|
if (*arg != '-')
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (strcmp(arg, "-all") == 0)
|
if (strcmp(arg, "-all") == 0)
|
||||||
gApps = true;
|
argv[i] = "--all";
|
||||||
else if (strcmp(arg, "-apps") == 0) {
|
else if (strcmp(arg, "-apps") == 0)
|
||||||
gApps = true;
|
argv[i] = "--apps";
|
||||||
gFiles = false;
|
}
|
||||||
} else if (strcmp(arg, "-f") == 0)
|
|
||||||
gForce = B_UPDATE_MIME_INFO_FORCE_KEEP_TYPE;
|
for (;;) {
|
||||||
else if (strcmp(arg, "-F") == 0)
|
static struct option sLongOptions[] = {
|
||||||
gForce = B_UPDATE_MIME_INFO_FORCE_UPDATE_ALL;
|
{ "all", no_argument, 0, 'A' },
|
||||||
else if (strcmp(arg, "--help") == 0)
|
{ "apps", no_argument, 0, 'a' },
|
||||||
usage(0);
|
{ "help", no_argument, 0, 'h' },
|
||||||
else {
|
{ 0, 0, 0, 0 }
|
||||||
fprintf(stderr, "unknown option \"%s\"\n", arg);
|
};
|
||||||
usage(1);
|
|
||||||
|
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
|
// process files
|
||||||
|
|
||||||
BApplication app("application/x-vnd.haiku.mimeset");
|
BApplication app("application/x-vnd.haiku.mimeset");
|
||||||
|
|
||||||
while (*argv != NULL) {
|
for (; optind < argc; optind++) {
|
||||||
const char* arg = *argv++;
|
const char* arg = argv[optind];
|
||||||
|
|
||||||
if (strcmp(arg, "@") == 0) {
|
if (strcmp(arg, "@") == 0) {
|
||||||
// read file names from stdin
|
// read file names from stdin
|
||||||
|
|||||||
Reference in New Issue
Block a user