* mu: some better command error / help output

This commit is contained in:
djcb 2012-09-17 19:53:30 +03:00
parent 81c3724e77
commit ca2fccf11e
6 changed files with 107 additions and 99 deletions

View File

@ -365,27 +365,33 @@ show_parts (const char* path, MuConfig *opts, GError **err)
static gboolean
check_params (MuConfig *opts)
check_params (MuConfig *opts, GError **err)
{
size_t param_num;
param_num = mu_config_param_num (opts);
if (param_num < 2) {
g_warning ("usage: mu extract [options] <file> [<pattern>]");
mu_util_g_set_error
(err, MU_ERROR_IN_PARAMETERS,
"parameters missing");
return FALSE;
}
if (opts->save_attachments || opts->save_all)
if (opts->parts || param_num == 3) {
g_warning ("--save-attachments and --save-all don't "
"accept a filename pattern or --parts");
mu_util_g_set_error
(err, MU_ERROR_IN_PARAMETERS,
"--save-attachments and --save-all don't "
"accept a filename pattern or --parts");
return FALSE;
}
if (opts->save_attachments && opts->save_all) {
g_warning ("only one of --save-attachments and"
" --save-all is allowed");
mu_util_g_set_error
(err, MU_ERROR_IN_PARAMETERS,
"only one of --save-attachments and"
" --save-all is allowed");
return FALSE;
}
@ -401,15 +407,13 @@ mu_cmd_extract (MuConfig *opts, GError **err)
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_EXTRACT,
MU_ERROR_INTERNAL);
if (!check_params (opts)) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS,
"error in parameters");
if (!check_params (opts, err))
return MU_ERROR_IN_PARAMETERS;
}
if (!opts->params[2] && !opts->parts &&
!opts->save_attachments && !opts->save_all)
rv = show_parts (opts->params[1], opts, err); /* show, don't save */
/* show, don't save */
rv = show_parts (opts->params[1], opts, err);
else {
rv = mu_util_check_dir(opts->targetdir, FALSE, TRUE);
if (!rv)

View File

@ -719,13 +719,6 @@ query_params_valid (MuConfig *opts, GError **err)
}
static void
show_usage (void)
{
g_print ("%s", "usage: mu find [options] <search expression>\n");
}
MuError
mu_cmd_find (MuStore *store, MuConfig *opts, GError **err)
{
@ -737,11 +730,8 @@ mu_cmd_find (MuStore *store, MuConfig *opts, GError **err)
opts->format = MU_CONFIG_FORMAT_EXEC; /* pseudo format */
if (!query_params_valid (opts, err) ||
!format_params_valid(opts, err)) {
if (MU_G_ERROR_CODE(err) == MU_ERROR_IN_PARAMETERS)
show_usage ();
!format_params_valid(opts, err))
return MU_G_ERROR_CODE (err);
}
if (!execute_find (store, opts, err))
return MU_G_ERROR_CODE(err);

View File

@ -76,20 +76,20 @@ check_params (MuConfig *opts, GError **err)
{
/* param[0] == 'index' there should be no param[1] */
if (opts->params[1]) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS,
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"unexpected parameter");
return FALSE;
}
if (opts->xbatchsize < 0) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS,
"the batch size must be non-negative");
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"the batch size must be >= 0");
return FALSE;
}
if (opts->max_msg_size < 0) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS,
"the maximum message size must be non-negative");
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"the maximum message size must >= 0");
return FALSE;
}
@ -100,21 +100,21 @@ static gboolean
check_maildir (const char *maildir, GError **err)
{
if (!maildir) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS,
"no maildir to work on; use --maildir=");
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"no maildir to work on; use --maildir=");
return FALSE;
}
if (!g_path_is_absolute (maildir)) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS,
"maildir path '%s' is not absolute",
maildir);
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"maildir path '%s' is not absolute",
maildir);
return FALSE;
}
if (!mu_util_check_dir (maildir, TRUE, FALSE)) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS,
"not a valid Maildir: %s", maildir);
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"not a valid Maildir: %s", maildir);
return FALSE;
}

View File

@ -435,6 +435,8 @@ cmd_from_string (const char *str)
{ "view", MU_CONFIG_CMD_VIEW }
};
if (!str)
return MU_CONFIG_CMD_UNKNOWN;
for (i = 0; i != G_N_ELEMENTS(cmd_map); ++i)
if (strcmp (str, cmd_map[i].name) == 0)
@ -493,27 +495,6 @@ get_option_group (MuConfigCmd cmd)
static const gchar*
cmd_help (MuConfigCmd cmd, gboolean long_help)
{
unsigned u;
/* this include gets us MU_HELP_STRINGS */
#include "mu-help-strings.h"
for (u = 0; u != G_N_ELEMENTS(MU_HELP_STRINGS); ++u)
if (cmd == MU_HELP_STRINGS[u].cmd) {
if (long_help)
return MU_HELP_STRINGS[u].long_help;
else
return MU_HELP_STRINGS[u].usage ;
}
g_return_val_if_reached ("");
return "";
}
/* ugh yuck massaging the GOption text output; glib prepares some text
* which has a 'Usage:' for the 'help' commmand. However, we need the
* help for the command we're asking help for. So, we remove the Usage:
@ -534,44 +515,77 @@ massage_help (const char *help)
}
static gboolean
init_cmd_help (GError **err)
static const gchar*
get_help_string (MuConfigCmd cmd, gboolean long_help)
{
unsigned u;
/* this include gets us MU_HELP_STRINGS */
#include "mu-help-strings.h"
for (u = 0; u != G_N_ELEMENTS(MU_HELP_STRINGS); ++u)
if (cmd == MU_HELP_STRINGS[u].cmd) {
if (long_help)
return MU_HELP_STRINGS[u].long_help;
else
return MU_HELP_STRINGS[u].usage ;
}
g_return_val_if_reached ("");
return "";
}
void
mu_config_show_help (MuConfigCmd cmd)
{
MuConfigCmd cmd;
GOptionContext *ctx;
GOptionGroup *group;
char *cleanhelp;
if (!MU_CONFIG.params ||
!MU_CONFIG.params[0] || !MU_CONFIG.params[1] ||
MU_CONFIG.params[2])
goto errexit;
cmd = cmd_from_string (MU_CONFIG.params[1]);
if (cmd == MU_CONFIG_CMD_UNKNOWN)
goto errexit;
ctx = g_option_context_new ("");
g_option_context_set_main_group
(ctx, config_options_group_mu());
g_option_context_set_main_group (ctx, config_options_group_mu());
group = get_option_group (cmd);
if (group)
g_option_context_add_group (ctx, group);
g_option_context_set_description (ctx, cmd_help (cmd, TRUE));
g_option_context_set_description (ctx,
get_help_string (cmd, TRUE));
cleanhelp = massage_help
(g_option_context_get_help (ctx, TRUE, group));
g_print ("Usage:\n\t%s\n%s",
cmd_help (cmd, FALSE), cleanhelp);
g_print ("usage:\n\t%s\n%s",
get_help_string (cmd, FALSE), cleanhelp);
g_free (cleanhelp);
}
static gboolean
cmd_help (void)
{
MuConfigCmd cmd;
cmd = cmd_from_string (MU_CONFIG.params[1]);
if (cmd == MU_CONFIG_CMD_UNKNOWN) {
mu_config_show_help (MU_CONFIG_CMD_HELP);
return TRUE;
}
mu_config_show_help (cmd);
return TRUE;
}
errexit:
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"usage: mu help <command>");
return FALSE;
static void
show_usage (void)
{
g_print ("usage: mu <command> [options] [parameters]\n");
g_print ("try 'mu help <command>', or ");
g_print ("see the mu, mu-<command> or mu-easy manpages.\n");
}
@ -588,10 +602,15 @@ parse_params (int *argcp, char ***argvp)
err = NULL;
if (MU_CONFIG.cmd == MU_CONFIG_CMD_NONE) {
show_usage ();
return TRUE;
}
/* help is special */
if (MU_CONFIG.cmd == MU_CONFIG_CMD_HELP) {
rv = g_option_context_parse (context, argcp, argvp, &err) &&
init_cmd_help (&err);
cmd_help ();
} else {
GOptionGroup *group;
group = get_option_group (MU_CONFIG.cmd);

View File

@ -219,8 +219,6 @@ MuError mu_config_execute (MuConfig *conf);
size_t mu_config_param_num (MuConfig *conf);
/**
* determine MuMsgOptions for command line args
*
@ -231,6 +229,13 @@ size_t mu_config_param_num (MuConfig *conf);
MuMsgOptions mu_config_get_msg_options (MuConfig *opts);
/**
* print help text for the current command
*
* @param cmd the command to show help for
*/
void mu_config_show_help (MuConfigCmd cmd);
G_END_DECLS
#endif /*__MU_CONFIG_H__*/

View File

@ -43,41 +43,27 @@ show_version (void)
static void
handle_error (MuConfig *conf, GError *err)
{
const char *advise;
char *dynadvise;
if (!err)
return; /* nothing to do */
dynadvise = NULL;
advise = NULL;
switch (err->code) {
case MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK:
advise = "maybe mu is already running?"; break;
g_print ("maybe mu is already running?\n");
break;
case MU_ERROR_XAPIAN_CORRUPTION:
case MU_ERROR_XAPIAN_NOT_UP_TO_DATE:
advise = "please try 'mu index --rebuild'"; break;
g_print ("database needs update; try 'mu index --rebuild'\n");
break;
case MU_ERROR_XAPIAN_IS_EMPTY:
advise = "please try 'mu index'";
g_print ("database is empty; try 'mu index'");
break;
case MU_ERROR_IN_PARAMETERS:
if (conf->cmd != MU_CONFIG_CMD_UNKNOWN)
dynadvise = g_strdup_printf ("see 'mu help %s'",
conf->cmdstr);
mu_config_show_help (conf->cmd);
break;
default:break; /* nothing to do */
}
g_warning ("%s", err->message);
if (advise)
g_print ("%s\n", advise);
if (dynadvise) {
g_print ("%s\n", dynadvise);
g_free (dynadvise);
}
}
@ -99,6 +85,10 @@ main (int argc, char *argv[])
return 0;
}
/* nothing to do */
if (conf->cmd == MU_CONFIG_CMD_NONE)
return 0;
if (!mu_runtime_init (conf->muhome, PACKAGE_NAME)) {
mu_config_uninit (conf);
return 1;