mirror of https://github.com/djcb/mu.git
* make option parsing specific for the mu command
This commit is contained in:
parent
0101837c38
commit
091dedf954
46
src/mu-cmd.c
46
src/mu-cmd.c
|
@ -40,32 +40,6 @@ mu_cmd_equals (MuConfigOptions *config, const gchar *cmd)
|
|||
return (strcmp (config->params[0], cmd) == 0);
|
||||
}
|
||||
|
||||
|
||||
static MuCmd
|
||||
cmd_from_string (const char* cmd)
|
||||
{
|
||||
int i;
|
||||
typedef struct {
|
||||
const gchar* _name;
|
||||
MuCmd _cmd;
|
||||
} Cmd;
|
||||
|
||||
Cmd cmd_map[] = {
|
||||
{ "index", MU_CMD_INDEX },
|
||||
{ "find", MU_CMD_FIND },
|
||||
{ "cleanup", MU_CMD_CLEANUP },
|
||||
{ "mkdir", MU_CMD_MKDIR },
|
||||
{ "view", MU_CMD_VIEW },
|
||||
{ "extract", MU_CMD_EXTRACT }
|
||||
};
|
||||
|
||||
for (i = 0; i != G_N_ELEMENTS(cmd_map); ++i)
|
||||
if (strcmp (cmd, cmd_map[i]._name) == 0)
|
||||
return cmd_map[i]._cmd;
|
||||
|
||||
return MU_CMD_UNKNOWN;
|
||||
}
|
||||
|
||||
static void
|
||||
show_usage (gboolean noerror)
|
||||
{
|
||||
|
@ -91,8 +65,6 @@ show_version (void)
|
|||
gboolean
|
||||
mu_cmd_execute (MuConfigOptions *opts)
|
||||
{
|
||||
MuCmd cmd;
|
||||
|
||||
if (opts->version) {
|
||||
show_version ();
|
||||
return TRUE;
|
||||
|
@ -104,19 +76,17 @@ mu_cmd_execute (MuConfigOptions *opts)
|
|||
show_usage (TRUE);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
cmd = cmd_from_string (opts->params[0]);
|
||||
|
||||
switch (cmd) {
|
||||
switch (opts->cmd) {
|
||||
|
||||
case MU_CMD_CLEANUP: return mu_cmd_cleanup (opts);
|
||||
case MU_CMD_EXTRACT: return mu_cmd_extract (opts);
|
||||
case MU_CMD_FIND: return mu_cmd_find (opts);
|
||||
case MU_CMD_INDEX: return mu_cmd_index (opts);
|
||||
case MU_CMD_MKDIR: return mu_cmd_mkdir (opts);
|
||||
case MU_CMD_VIEW: return mu_cmd_view (opts);
|
||||
case MU_CONFIG_CMD_CLEANUP: return mu_cmd_cleanup (opts);
|
||||
case MU_CONFIG_CMD_EXTRACT: return mu_cmd_extract (opts);
|
||||
case MU_CONFIG_CMD_FIND: return mu_cmd_find (opts);
|
||||
case MU_CONFIG_CMD_INDEX: return mu_cmd_index (opts);
|
||||
case MU_CONFIG_CMD_MKDIR: return mu_cmd_mkdir (opts);
|
||||
case MU_CONFIG_CMD_VIEW: return mu_cmd_view (opts);
|
||||
|
||||
case MU_CMD_UNKNOWN:
|
||||
case MU_CONFIG_CMD_UNKNOWN:
|
||||
show_usage (FALSE);
|
||||
return FALSE;
|
||||
default:
|
||||
|
|
|
@ -81,6 +81,8 @@ set_group_index_defaults (MuConfigOptions * opts)
|
|||
g_free(opts->maildir);
|
||||
opts->maildir = exp;
|
||||
}
|
||||
|
||||
opts->xbatchsize = 0;
|
||||
}
|
||||
|
||||
static GOptionGroup*
|
||||
|
@ -99,6 +101,8 @@ config_options_group_index (MuConfigOptions * opts)
|
|||
NULL},
|
||||
{"nocleanup", 0, 0, G_OPTION_ARG_NONE, &opts->nocleanup,
|
||||
"don't clean up the database after indexing", NULL},
|
||||
{"xbatchsize", 0, 0, G_OPTION_ARG_INT, &opts->xbatchsize,
|
||||
"set a custom batchsize for committing to xapian or 0 for default", NULL},
|
||||
{NULL, 0, 0, 0, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
|
@ -220,28 +224,88 @@ config_options_group_extract(MuConfigOptions *opts)
|
|||
return og;
|
||||
}
|
||||
|
||||
|
||||
static MuConfigCmd
|
||||
cmd_from_params (int *argcp, char ***argvp)
|
||||
{
|
||||
int i;
|
||||
const char *cmd;
|
||||
typedef struct {
|
||||
const gchar* _name;
|
||||
MuConfigCmd _cmd;
|
||||
} Cmd;
|
||||
|
||||
Cmd cmd_map[] = {
|
||||
{ "index", MU_CONFIG_CMD_INDEX },
|
||||
{ "find", MU_CONFIG_CMD_FIND },
|
||||
{ "cleanup", MU_CONFIG_CMD_CLEANUP },
|
||||
{ "mkdir", MU_CONFIG_CMD_MKDIR },
|
||||
{ "view", MU_CONFIG_CMD_VIEW },
|
||||
{ "extract", MU_CONFIG_CMD_EXTRACT }
|
||||
};
|
||||
|
||||
if (*argcp < 2)
|
||||
return MU_CONFIG_CMD_UNKNOWN;
|
||||
|
||||
cmd = (*argvp)[1]; /* commmand or option */
|
||||
|
||||
for (i = 0; i != G_N_ELEMENTS(cmd_map); ++i)
|
||||
if (strcmp (cmd, cmd_map[i]._name) == 0)
|
||||
return cmd_map[i]._cmd;
|
||||
|
||||
/* if the first param starts with '-', there is no command, just some option
|
||||
* (like --version, --help etc.)*/
|
||||
if (cmd[0] == '-')
|
||||
return MU_CONFIG_CMD_NONE;
|
||||
|
||||
return MU_CONFIG_CMD_UNKNOWN;
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
parse_params (MuConfigOptions *opts, int *argcp, char ***argvp)
|
||||
{
|
||||
GError *err;
|
||||
GOptionContext *context;
|
||||
gboolean rv;
|
||||
|
||||
|
||||
opts->cmd = cmd_from_params (argcp, argvp);
|
||||
if (opts->cmd == MU_CONFIG_CMD_UNKNOWN)
|
||||
return FALSE;
|
||||
|
||||
context = g_option_context_new("- mu general option");
|
||||
|
||||
g_option_context_set_main_group(context, config_options_group_mu(opts));
|
||||
g_option_context_add_group(context, config_options_group_index(opts));
|
||||
g_option_context_add_group(context, config_options_group_find(opts));
|
||||
g_option_context_add_group(context, config_options_group_mkdir(opts));
|
||||
g_option_context_add_group(context, config_options_group_extract(opts));
|
||||
|
||||
switch (opts->cmd) {
|
||||
case MU_CONFIG_CMD_INDEX:
|
||||
g_option_context_add_group(context, config_options_group_index(opts));
|
||||
break;
|
||||
case MU_CONFIG_CMD_FIND:
|
||||
g_option_context_add_group(context, config_options_group_find(opts));
|
||||
break;
|
||||
case MU_CONFIG_CMD_MKDIR:
|
||||
g_option_context_add_group(context, config_options_group_mkdir(opts));
|
||||
break;
|
||||
case MU_CONFIG_CMD_EXTRACT:
|
||||
g_option_context_add_group(context, config_options_group_extract(opts));
|
||||
break;
|
||||
case MU_CONFIG_CMD_UNKNOWN:
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
err = NULL;
|
||||
rv = g_option_context_parse(context, argcp, argvp, &err);
|
||||
if (!rv) {
|
||||
g_warning("error in options: %s\n", err->message);
|
||||
/* use g_printerr here, as logging is not yet initialized */
|
||||
if (opts->cmd != MU_CONFIG_CMD_NONE)
|
||||
g_printerr ("error in options for command: %s\n", err->message);
|
||||
else
|
||||
g_printerr ("error in options: %s\n", err->message);
|
||||
g_error_free(err);
|
||||
}
|
||||
|
||||
|
||||
g_option_context_free(context);
|
||||
|
||||
/* fill in the defaults if user did not specify */
|
||||
|
@ -260,10 +324,10 @@ mu_config_init (MuConfigOptions *opts, int *argcp, char ***argvp)
|
|||
memset(opts, 0, sizeof(MuConfigOptions));
|
||||
|
||||
/* defaults are set in parse_params */
|
||||
if (argcp && argvp)
|
||||
if (argcp && argvp)
|
||||
if (!parse_params(opts, argcp, argvp))
|
||||
return FALSE;
|
||||
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/*
|
||||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
||||
**
|
||||
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or modify it
|
||||
|
@ -48,6 +49,19 @@ static MuRuntimeData *_data = NULL;
|
|||
|
||||
static void runtime_free (void);
|
||||
|
||||
static gboolean
|
||||
mu_dir_is_readable_and_writable (const char* muhome)
|
||||
{
|
||||
if (mu_util_check_dir(muhome, TRUE, TRUE))
|
||||
return TRUE;
|
||||
|
||||
g_warning ("'%s' is not a read/writable directory", muhome);
|
||||
g_warning ("use --muhome= to set a different one");
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
gboolean
|
||||
mu_runtime_init (const char* muhome_arg)
|
||||
|
@ -64,6 +78,11 @@ mu_runtime_init (const char* muhome_arg)
|
|||
else
|
||||
muhome = mu_util_guess_mu_homedir ();
|
||||
|
||||
if (!mu_dir_is_readable_and_writable (muhome)) {
|
||||
runtime_free ();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!mu_log_init (muhome, TRUE, FALSE, FALSE)) {
|
||||
g_free (muhome);
|
||||
return FALSE;
|
||||
|
@ -103,6 +122,11 @@ mu_runtime_init_from_cmdline (int *pargc, char ***pargv)
|
|||
if (!mu_config_init (_data->_config, pargc, pargv)) {
|
||||
runtime_free ();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!mu_dir_is_readable_and_writable (_data->_config->muhome)) {
|
||||
runtime_free ();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!init_log (_data->_config)) {
|
||||
|
@ -150,8 +174,8 @@ mu_runtime_uninit (void)
|
|||
const char*
|
||||
mu_runtime_mu_home_dir (void)
|
||||
{
|
||||
g_return_val_if_fail (_initialized, NULL);
|
||||
|
||||
g_return_val_if_fail (_initialized, NULL);
|
||||
|
||||
return _data->_muhome;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue