* use the mu-index option group; rationalize option parsing, usage message

This commit is contained in:
Dirk-Jan C. Binnema 2009-11-26 20:29:17 +02:00
parent 836af9dcd4
commit fede192a7e
1 changed files with 80 additions and 34 deletions

114
src/mu.c
View File

@ -27,6 +27,39 @@
#include "mu-msg-gmime.h" #include "mu-msg-gmime.h"
enum _MuCmd {
MU_CMD_INDEX,
MU_CMD_QUERY,
MU_CMD_HELP,
MU_CMD_UNKNOWN
};
typedef enum _MuCmd MuCmd;
MuCmd
parse_cmd (const char* cmd)
{
if (!cmd)
return MU_CMD_UNKNOWN;
if (strcmp (cmd, "index") == 0)
return MU_CMD_INDEX;
/* support some synonyms... */
if ((strcmp (cmd, "query") == 0) ||
(strcmp (cmd, "find") == 0) ||
(strcmp (cmd, "search") == 0))
return MU_CMD_QUERY;
if ((strcmp (cmd, "help") == 0))
return MU_CMD_HELP;
return MU_CMD_UNKNOWN;
}
static MuResult static MuResult
msg_cb (MuIndexStats* stats, void *user_data) msg_cb (MuIndexStats* stats, void *user_data)
{ {
@ -39,15 +72,36 @@ msg_cb (MuIndexStats* stats, void *user_data)
return MU_OK; return MU_OK;
} }
static void static int
show_help (const char* cmd) show_help (const char* cmd)
{ {
if (cmd) if (cmd)
g_print ("Help about %s\n", cmd); g_print ("Help about %s\n", cmd);
else else
g_print ("General help"); g_print ("General help\n");
return 0;
} }
static int
show_usage (gboolean noerror)
{
const char* usage=
"usage: mu [options] command [parameters]\n"
"\twhere command is one of index, query, help\n"
"see mu(1) for for information\n";
if (noerror)
g_print ("%s", usage);
else
g_printerr ("%s", usage);
return noerror ? 0 : 1;
}
static gboolean opt_debug; static gboolean opt_debug;
static gboolean opt_quiet; static gboolean opt_quiet;
@ -61,51 +115,44 @@ static GOptionEntry entries[] = {
}; };
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
const char* cmd;
GError *error = NULL; GError *error = NULL;
GOptionContext *context; GOptionContext *context;
MuResult rv; MuResult rv;
MuCmd cmd;
g_type_init (); g_type_init ();
context = g_option_context_new ("- search your e-mail"); context = g_option_context_new ("- search your e-mail");
g_option_context_add_main_entries (context, entries, "mu"); g_option_context_add_main_entries (context, entries, "mu");
g_option_context_add_group (context, mu_query_option_group()); g_option_context_add_group (context, mu_query_option_group());
g_option_context_add_group (context, mu_index_option_group());
if (!g_option_context_parse (context, &argc, &argv, &error)) { if (!g_option_context_parse (context, &argc, &argv, &error)) {
g_printerr ("option parsing failed: %s\n", g_printerr ("error in options: %s\n",
error->message); error->message);
g_error_free (error); g_error_free (error);
return 1; return 1;
} }
if (argc < 2 || !( strcmp(argv[1], "index") == 0 || if (argc < 2)
strcmp(argv[1], "search") == 0 || return show_usage (FALSE);
strcmp(argv[1], "help") == 0)) {
g_printerr ("usage: mu [options] command [parameters]\n"
"\twhere command is one of index, search, help\n");
return 1;
}
cmd = argv[1];
if (strcmp (cmd, "help") == 0) { cmd = parse_cmd (argv[1]);
show_help (argc > 2 ? argv[2] : NULL); if (cmd == MU_CMD_UNKNOWN)
return 0; return show_usage (FALSE);
}
if (cmd == MU_CMD_HELP)
return show_help (argc > 2 ? argv[2] : NULL);
mu_msg_gmime_init (); mu_msg_gmime_init ();
rv = MU_OK; rv = MU_OK;
if (strcmp(cmd, "index") == 0) {
if (cmd == MU_CMD_INDEX) {
MuIndex *midx; MuIndex *midx;
MuIndexStats stats; MuIndexStats stats;
@ -116,20 +163,19 @@ main (int argc, char *argv[])
mu_index_destroy (midx); mu_index_destroy (midx);
} else if (strcmp(cmd, "search") == 0) { } else if (cmd == MU_CMD_QUERY) {
GSList *args;
if (argc < 3) { /* FIXME */
g_printerr ("error!\n");
return 1;
}
args = mu_util_strlist_from_args (argc-2, argv+2); GSList *args;
rv = mu_query_run (args); if (argc < 3) {
mu_util_strlist_free (args); g_printerr ("error: missing something to search for\n");
rv = 1;
} else {
args = mu_util_strlist_from_args (argc-2, argv+2);
rv = mu_query_run (args);
mu_util_strlist_free (args);
}
} }
mu_msg_gmime_uninit (); mu_msg_gmime_uninit ();
return rv == MU_OK ? 0 : 1; return rv == MU_OK ? 0 : 1;
} }