summaryrefslogtreecommitdiff
path: root/mysys/my_getopt.c
diff options
context:
space:
mode:
authorunknown <serg@serg.mylan>2004-08-26 15:19:46 +0200
committerunknown <serg@serg.mylan>2004-08-26 15:19:46 +0200
commite8d41a5b1bf0c559c5c3acab469eaa6ff56accfb (patch)
tree9299076637e9019c66560b2d68fe6b4bc6456487 /mysys/my_getopt.c
parent42771f9877c17050a230cdaf8a26afc5d077a712 (diff)
parentad1f3bb17d5a8117ff9feb987c04a52f22394189 (diff)
downloadmariadb-git-e8d41a5b1bf0c559c5c3acab469eaa6ff56accfb.tar.gz
merged
BitKeeper/etc/logging_ok: auto-union VC++Files/sql/mysqld.dsp: Auto merged client/mysql.cc: Auto merged client/mysqladmin.c: Auto merged BitKeeper/deleted/.del-mysql_install.c~8c089740d79a92: Auto merged client/mysqlbinlog.cc: Auto merged client/mysqlcheck.c: Auto merged client/mysqldump.c: Auto merged client/mysqlimport.c: Auto merged client/mysqlmanager-pwgen.c: Auto merged client/mysqlshow.c: Auto merged client/mysqltest.c: Auto merged extra/my_print_defaults.c: Auto merged extra/perror.c: Auto merged extra/resolve_stack_dump.c: Auto merged extra/resolveip.c: Auto merged include/my_getopt.h: Auto merged innobase/buf/buf0rea.c: Auto merged isam/isamchk.c: Auto merged isam/pack_isam.c: Auto merged libmysqld/Makefile.am: Auto merged myisam/mi_test1.c: Auto merged myisam/myisamchk.c: Auto merged myisam/myisampack.c: Auto merged mysql-test/mysql-test-run.sh: Auto merged sql/gen_lex_hash.cc: Auto merged sql/sql_base.cc: Auto merged sql/sql_parse.cc: Auto merged tools/mysqlmanager.c: Auto merged
Diffstat (limited to 'mysys/my_getopt.c')
-rw-r--r--mysys/my_getopt.c61
1 files changed, 32 insertions, 29 deletions
diff --git a/mysys/my_getopt.c b/mysys/my_getopt.c
index d7a9babe5e7..07a8f199b3b 100644
--- a/mysys/my_getopt.c
+++ b/mysys/my_getopt.c
@@ -17,9 +17,9 @@
#include <my_global.h>
#include <m_string.h>
#include <stdlib.h>
-#include <my_getopt.h>
#include <my_sys.h>
#include <mysys_err.h>
+#include <my_getopt.h>
static int findopt(char *optpat, uint length,
const struct my_option **opt_res,
@@ -56,6 +56,13 @@ char *disabled_my_option= (char*) "0";
my_bool my_getopt_print_errors= 1;
+void default_reporter(enum loglevel level, const char *format, ...)
+{
+ va_list args;
+ va_start(args, format);
+ vfprintf(stderr, format, args);
+ va_end(args);
+}
/*
function: handle_options
@@ -76,10 +83,8 @@ void my_getopt_register_get_addr(gptr* (*func_addr)(const char *, uint,
}
int handle_options(int *argc, char ***argv,
- const struct my_option *longopts,
- my_bool (*get_one_option)(int,
- const struct my_option *,
- char *))
+ const struct my_option *longopts, my_get_one_option get_one_option,
+ my_error_reporter reporter)
{
uint opt_found, argvpos= 0, length, i;
my_bool end_of_options= 0, must_be_var, set_maximum_value,
@@ -95,6 +100,8 @@ int handle_options(int *argc, char ***argv,
(*argv)++; /* --- || ---- */
init_variables(longopts);
+ if (! reporter) reporter = &default_reporter;
+
for (pos= *argv, pos_end=pos+ *argc; pos != pos_end ; pos++)
{
char *cur_arg= *pos;
@@ -118,8 +125,8 @@ int handle_options(int *argc, char ***argv,
if (!*++pos)
{
if (my_getopt_print_errors)
- fprintf(stderr, "%s: Option '-O' requires an argument\n",
- my_progname);
+ reporter(ERROR_LEVEL, "%s: Option '-O' requires an argument\n",
+ my_progname);
return EXIT_ARGUMENT_REQUIRED;
}
cur_arg= *pos;
@@ -135,9 +142,8 @@ int handle_options(int *argc, char ***argv,
if (!*cur_arg)
{
if (my_getopt_print_errors)
- fprintf(stderr,
- "%s: Option '--set-variable' requires an argument\n",
- my_progname);
+ reporter(ERROR_LEVEL, "%s: Option '--set-variable' requires an argument\n",
+ my_progname);
return EXIT_ARGUMENT_REQUIRED;
}
}
@@ -149,9 +155,8 @@ int handle_options(int *argc, char ***argv,
if (!*++pos)
{
if (my_getopt_print_errors)
- fprintf(stderr,
- "%s: Option '--set-variable' requires an argument\n",
- my_progname);
+ reporter(ERROR_LEVEL, "%s: Option '--set-variable' requires an argument\n",
+ my_progname);
return EXIT_ARGUMENT_REQUIRED;
}
cur_arg= *pos;
@@ -210,7 +215,7 @@ int handle_options(int *argc, char ***argv,
if (opt_found > 1)
{
if (my_getopt_print_errors)
- fprintf(stderr,
+ reporter(ERROR_LEVEL,
"%s: ambiguous option '--%s-%s' (--%s-%s)\n",
my_progname, special_opt_prefix[i], opt_str,
special_opt_prefix[i], prev_found);
@@ -245,18 +250,16 @@ int handle_options(int *argc, char ***argv,
if (must_be_var)
{
if (my_getopt_print_errors)
- fprintf(stderr,
- "%s: %s: unknown variable '%s'\n", my_progname,
- option_is_loose ? "WARNING" : "ERROR", opt_str);
+ reporter(option_is_loose ? WARNING_LEVEL : ERROR_LEVEL,
+ "%s: unknown variable '%s'\n", my_progname, cur_arg);
if (!option_is_loose)
return EXIT_UNKNOWN_VARIABLE;
}
else
{
if (my_getopt_print_errors)
- fprintf(stderr,
- "%s: %s: unknown option '--%s'\n", my_progname,
- option_is_loose ? "WARNING" : "ERROR", opt_str);
+ reporter(option_is_loose ? WARNING_LEVEL : ERROR_LEVEL,
+ "%s: unknown option '--%s'\n", my_progname, cur_arg);
if (!option_is_loose)
return EXIT_UNKNOWN_OPTION;
}
@@ -272,14 +275,14 @@ int handle_options(int *argc, char ***argv,
if (must_be_var)
{
if (my_getopt_print_errors)
- fprintf(stderr, "%s: variable prefix '%s' is not unique\n",
+ reporter(ERROR_LEVEL, "%s: variable prefix '%s' is not unique\n",
my_progname, opt_str);
return EXIT_VAR_PREFIX_NOT_UNIQUE;
}
else
{
if (my_getopt_print_errors)
- fprintf(stderr, "%s: ambiguous option '--%s' (%s, %s)\n",
+ reporter(ERROR_LEVEL, "%s: ambiguous option '--%s' (%s, %s)\n",
my_progname, opt_str, prev_found, optp->name);
return EXIT_AMBIGUOUS_OPTION;
}
@@ -300,7 +303,7 @@ int handle_options(int *argc, char ***argv,
if (must_be_var && (optp->var_type & GET_TYPE_MASK) == GET_NO_ARG)
{
if (my_getopt_print_errors)
- fprintf(stderr, "%s: option '%s' cannot take an argument\n",
+ reporter(ERROR_LEVEL, "%s: option '%s' cannot take an argument\n",
my_progname, optp->name);
return EXIT_NO_ARGUMENT_ALLOWED;
}
@@ -312,7 +315,7 @@ int handle_options(int *argc, char ***argv,
if (optend && (optp->var_type & GET_TYPE_MASK) != GET_BOOL)
{
if (my_getopt_print_errors)
- fprintf(stderr, "%s: option '--%s' cannot take an argument\n",
+ reporter(ERROR_LEVEL, "%s: option '--%s' cannot take an argument\n",
my_progname, optp->name);
return EXIT_NO_ARGUMENT_ALLOWED;
}
@@ -351,7 +354,7 @@ int handle_options(int *argc, char ***argv,
if (!*++pos)
{
if (my_getopt_print_errors)
- fprintf(stderr, "%s: option '--%s' requires an argument\n",
+ reporter(ERROR_LEVEL, "%s: option '--%s' requires an argument\n",
my_progname, optp->name);
return EXIT_ARGUMENT_REQUIRED;
}
@@ -410,7 +413,7 @@ int handle_options(int *argc, char ***argv,
if (!pos[1])
{
if (my_getopt_print_errors)
- fprintf(stderr,
+ reporter(ERROR_LEVEL,
"%s: option '-%c' requires an argument\n",
my_progname, optp->id);
return EXIT_ARGUMENT_REQUIRED;
@@ -423,7 +426,7 @@ int handle_options(int *argc, char ***argv,
if ((error= setval(optp, optp->value, argument,
set_maximum_value)))
{
- fprintf(stderr,
+ reporter(ERROR_LEVEL,
"%s: Error while setting value '%s' to '%s'\n",
my_progname, argument, optp->name);
return error;
@@ -435,7 +438,7 @@ int handle_options(int *argc, char ***argv,
if (!opt_found)
{
if (my_getopt_print_errors)
- fprintf(stderr,
+ reporter(ERROR_LEVEL,
"%s: unknown option '-%c'\n", my_progname, *optend);
return EXIT_UNKNOWN_OPTION;
}
@@ -445,7 +448,7 @@ int handle_options(int *argc, char ***argv,
}
if ((error= setval(optp, value, argument, set_maximum_value)))
{
- fprintf(stderr,
+ reporter(ERROR_LEVEL,
"%s: Error while setting value '%s' to '%s'\n",
my_progname, argument, optp->name);
return error;