MySQL 8.3.0
Source Code Documentation
my_getopt.h File Reference
#include <sys/types.h>
#include <cstdio>
#include <string_view>
#include <mysql/components/services/system_variable_source_type.h>
#include "my_config.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_macros.h"
#include "my_sys.h"

Go to the source code of this file.

Classes

struct  get_opt_arg_source
 
struct  my_option
 

Macros

#define GET_NO_ARG   1
 
#define GET_BOOL   2
 
#define GET_INT   3
 
#define GET_UINT   4
 
#define GET_LONG   5
 
#define GET_ULONG   6
 
#define GET_LL   7
 
#define GET_ULL   8
 
#define GET_STR   9
 
#define GET_STR_ALLOC   10
 
#define GET_DISABLED   11
 
#define GET_ENUM   12
 
#define GET_SET   13
 
#define GET_DOUBLE   14
 
#define GET_FLAGSET   15
 
#define GET_PASSWORD   16
 
#define GET_INT32   GET_INT
 
#define GET_UINT32   GET_UINT
 
#define GET_ASK_ADDR   128
 
#define GET_TYPE_MASK   127
 

Typedefs

typedef bool(* my_get_one_option) (int, const struct my_option *, char *)
 
typedef void *(* my_getopt_value) (const char *, size_t, const struct my_option *, int *)
 Used to retrieve a reference to the object (variable) that holds the value for the given option. More...
 

Enumerations

enum  get_opt_arg_type { NO_ARG , OPT_ARG , REQUIRED_ARG }
 Enumeration of the my_option::arg_type attributes. More...
 

Functions

int handle_options (int *argc, char ***argv, const struct my_option *longopts, my_get_one_option)
 Wrapper around my_handle_options() for interface compatibility. More...
 
int my_handle_options (int *argc, char ***argv, const struct my_option *longopts, my_get_one_option, const char **command_list, bool ignore_unknown_option)
 
int my_handle_options2 (int *argc, char ***argv, const struct my_option *longopts, my_get_one_option, const char **command_list, bool ignore_unknown_option, bool boolean_as_int)
 Handle command line options. More...
 
void print_cmdline_password_warning ()
 This function should be called to print a warning message if password string is specified on the command line. More...
 
void my_cleanup_options (const struct my_option *options)
 
void my_print_help (const struct my_option *options)
 
void my_print_variables (const struct my_option *options)
 function: my_print_variables Print variables. More...
 
void my_print_variables_ex (const struct my_option *options, FILE *file)
 function: my_print_variables_ex Print variables to given file parameter stream (by default to stdout). More...
 
void my_getopt_register_get_addr (my_getopt_value)
 
ulonglong getopt_ull_limit_value (ulonglong num, const struct my_option *optp, bool *fix)
 
longlong getopt_ll_limit_value (longlong, const struct my_option *, bool *fix)
 
double getopt_double_limit_value (double num, const struct my_option *optp, bool *fix)
 
ulonglong max_of_int_range (int var_type)
 Maximum possible value for an integer GET_* variable type. More...
 
ulonglong getopt_double2ulonglong (double)
 Returns an ulonglong value containing a raw representation of the given double value. More...
 
double getopt_ulonglong2double (ulonglong)
 Returns the double value which corresponds to the given raw representation. More...
 
int findopt (const char *, uint, const struct my_option **)
 Find option. More...
 
bool is_key_cache_variable_suffix (std::string_view suffix)
 
bool get_bool_argument (const char *argument, bool *error)
 Parse a boolean command line argument. More...
 
template<typename LLorULL >
LLorULL eval_num_suffix (const char *argument, int *error, const char *option_name)
 

Variables

char * disabled_my_option
 
bool my_getopt_print_errors
 
bool my_getopt_skip_unknown
 
bool log_replica_updates_supplied
 
bool replica_preserve_commit_order_supplied
 
my_error_reporter my_getopt_error_reporter
 

Macro Definition Documentation

◆ GET_ASK_ADDR

#define GET_ASK_ADDR   128

◆ GET_BOOL

#define GET_BOOL   2

◆ GET_DISABLED

#define GET_DISABLED   11

◆ GET_DOUBLE

#define GET_DOUBLE   14

◆ GET_ENUM

#define GET_ENUM   12

◆ GET_FLAGSET

#define GET_FLAGSET   15

◆ GET_INT

#define GET_INT   3

◆ GET_INT32

#define GET_INT32   GET_INT

◆ GET_LL

#define GET_LL   7

◆ GET_LONG

#define GET_LONG   5

◆ GET_NO_ARG

#define GET_NO_ARG   1

◆ GET_PASSWORD

#define GET_PASSWORD   16

◆ GET_SET

#define GET_SET   13

◆ GET_STR

#define GET_STR   9

◆ GET_STR_ALLOC

#define GET_STR_ALLOC   10

◆ GET_TYPE_MASK

#define GET_TYPE_MASK   127

◆ GET_UINT

#define GET_UINT   4

◆ GET_UINT32

#define GET_UINT32   GET_UINT

◆ GET_ULL

#define GET_ULL   8

◆ GET_ULONG

#define GET_ULONG   6

Typedef Documentation

◆ my_get_one_option

typedef bool(* my_get_one_option) (int, const struct my_option *, char *)

◆ my_getopt_value

typedef void *(* my_getopt_value) (const char *, size_t, const struct my_option *, int *)

Used to retrieve a reference to the object (variable) that holds the value for the given option.

For example, if var_type is GET_UINT, the function must return a pointer to a variable of type uint. A argument is stored in the location pointed to by the returned pointer.

Enumeration Type Documentation

◆ get_opt_arg_type

Enumeration of the my_option::arg_type attributes.

It should be noted that for historical reasons variables with the combination arg_type=NO_ARG, my_option::var_type=GET_BOOL still accepts arguments. This is someone counter intuitive and care should be taken if the code is refactored.

Enumerator
NO_ARG 
OPT_ARG 
REQUIRED_ARG 

Function Documentation

◆ eval_num_suffix()

template<typename LLorULL >
LLorULL eval_num_suffix ( const char *  argument,
int *  error,
const char *  option_name 
)

◆ findopt()

int findopt ( const char *  optpat,
uint  length,
const struct my_option **  opt_res 
)

Find option.

IMPLEMENTATION Go through all options in the my_option struct. Return true if an option is found. sets opt_res to the option found, if any.

Parameters
optpatname of option to find (with - or _)
lengthLength of optpat
[in,out]opt_resOptions
Return values
0No matching options
1Found an option

◆ get_bool_argument()

bool get_bool_argument ( const char *  argument,
bool *  error 
)

Parse a boolean command line argument.

"ON", "TRUE" and "1" will return true, other values will return false.

Parameters
argumentThe value argument
[out]errorError indicator
Returns
boolean value

◆ getopt_double2ulonglong()

ulonglong getopt_double2ulonglong ( double  v)

Returns an ulonglong value containing a raw representation of the given double value.

◆ getopt_double_limit_value()

double getopt_double_limit_value ( double  num,
const struct my_option optp,
bool *  fix 
)

◆ getopt_ll_limit_value()

longlong getopt_ll_limit_value ( longlong  num,
const struct my_option optp,
bool *  fix 
)

◆ getopt_ull_limit_value()

ulonglong getopt_ull_limit_value ( ulonglong  num,
const struct my_option optp,
bool *  fix 
)

◆ getopt_ulonglong2double()

double getopt_ulonglong2double ( ulonglong  v)

Returns the double value which corresponds to the given raw representation.

◆ handle_options()

int handle_options ( int *  argc,
char ***  argv,
const struct my_option longopts,
my_get_one_option  get_one_option 
)

Wrapper around my_handle_options() for interface compatibility.

Parameters
[in,out]argcCommand line options (count)
[in,out]argvCommand line options (values)
[in]longoptsDescriptor of all valid options
[in]get_one_optionOptional callback function to process each option, can be NULL.
Returns
Error in case of ambiguous or unknown options, 0 on success.

◆ is_key_cache_variable_suffix()

bool is_key_cache_variable_suffix ( std::string_view  suffix)

◆ max_of_int_range()

ulonglong max_of_int_range ( int  var_type)

Maximum possible value for an integer GET_* variable type.

Parameters
var_typetype of integer variable (GET_*)
Returns
maximum possible value for this type

◆ my_cleanup_options()

void my_cleanup_options ( const struct my_option options)

◆ my_getopt_register_get_addr()

void my_getopt_register_get_addr ( my_getopt_value  func_addr)

◆ my_handle_options()

int my_handle_options ( int *  argc,
char ***  argv,
const struct my_option longopts,
my_get_one_option  get_one_option,
const char **  command_list,
bool  ignore_unknown_option 
)

◆ my_handle_options2()

int my_handle_options2 ( int *  argc,
char ***  argv,
const struct my_option longopts,
my_get_one_option  get_one_option,
const char **  command_list,
bool  ignore_unknown_option,
bool  boolean_as_int 
)

Handle command line options.

Sort options. Put options first, until special end of options (–), or until the end of argv. Parse options, check that the given option matches with one of the options in struct 'my_option'. Check that option was given an argument if it requires one Call the optional 'get_one_option()' function once for each option.

Note that handle_options() can be invoked multiple times to parse a command line in several steps. In this case, use the global flag my_getopt_skip_unknown to indicate that options unknown in the current step should be preserved in the command line for later parsing in subsequent steps.

For 'long' options (–a_long_option), my_getopt_skip_unknown is fully supported. Command line parameters such as:

  • "--a_long_option"
  • "--a_long_option=value"
  • "--a_long_option value" will be preserved as is when the option is not known.

For 'short' options (-S), support for my_getopt_skip_unknown comes with some limitation, because several short options can also be specified together in the same command line argument, as in "-XYZ".

The first use case supported is: all short options are declared. handle_options() will be able to interpret "-XYZ" as one of:

  • an unknown X option
  • "-X -Y -Z", three short options with no arguments
  • "-X -YZ", where Y is a short option with argument Z
  • "-XYZ", where X is a short option with argument YZ based on the full short options specifications.

The second use case supported is: no short option is declared. handle_options() will reject "-XYZ" as unknown, to be parsed later.

The use case that is explicitly not supported is to provide only a partial list of short options to handle_options(). This function can not be expected to extract some option Y in the middle of the string "-XYZ" in these conditions, without knowing if X will be declared an option later.

Note that this limitation only impacts parsing of several short options from the same command line argument, as in "mysqld -anW5". When each short option is properly separated out in the command line argument, for example in "mysqld -a -n -w5", the code would actually work even with partial options specs given at each stage.

Parameters
[in,out]argccommand line options (count)
[in,out]argvcommand line options (values)
[in]longoptsdescriptor of all valid options
[in]get_one_optionoptional callback function to process each option, can be NULL.
[in]command_listNULL-terminated list of strings (commands) which (if set) is looked up for all non-option strings found while parsing the command line parameters. The parsing terminates if a match is found. At exit, argv [out] would contain all the remaining unparsed options along with the matched command.
[in]ignore_unknown_optionWhen set to true, options are continued to be read even when unknown options are encountered.
[in]boolean_as_intParse boolean as integer value. Mimic the logic of parsing booleans at runtime: Instead of parsing
bool_val = (str_val == '1' || str_val = 'ON') ?
true :
(str_val == '0' || str_val = 'OFF') ?
false :
false;
do:
bool_val == (str_val == 'OFF') ?
false :
((str_val == 'ON') ?
true :
atoi(str_val) != 0)
Returns
error in case of ambiguous or unknown options, 0 on success.

◆ my_print_help()

void my_print_help ( const struct my_option options)

◆ my_print_variables()

void my_print_variables ( const struct my_option options)

function: my_print_variables Print variables.

Parameters
[in]optionsmy_option list

◆ my_print_variables_ex()

void my_print_variables_ex ( const struct my_option options,
FILE *  file 
)

function: my_print_variables_ex Print variables to given file parameter stream (by default to stdout).

Parameters
[in]optionsmy_options list
[in]filestream where the output goes.

◆ print_cmdline_password_warning()

void print_cmdline_password_warning ( )

This function should be called to print a warning message if password string is specified on the command line.

Variable Documentation

◆ disabled_my_option

char* disabled_my_option
extern

◆ log_replica_updates_supplied

bool log_replica_updates_supplied
extern

◆ my_getopt_error_reporter

my_error_reporter my_getopt_error_reporter
extern

◆ my_getopt_print_errors

bool my_getopt_print_errors
extern

◆ my_getopt_skip_unknown

bool my_getopt_skip_unknown
extern

◆ replica_preserve_commit_order_supplied

bool replica_preserve_commit_order_supplied
extern