Motr  M0
M0 getopt(3) version from heaven.

Data Structures

struct  m0_getopts_opt
 

Macros

#define M0_GETOPTS(progname, argc, argv, ...)
 
#define M0_VOIDARG(ch, desc, func)
 
#define M0_NUMBERARG(ch, desc, func)
 
#define M0_SCALEDARG(ch, desc, func)
 
#define M0_STRINGARG(ch, desc, func)
 
#define M0_FORMATARG(ch, desc, fmt, ptr)
 
#define M0_FLAGARG(ch, desc, ptr)
 
#define M0_HELPARG(ch)
 

Enumerations

enum  m0_getopts_opt_type {
  GOT_VOID, GOT_NUMBER, GOT_SCALED, GOT_STRING,
  GOT_FORMAT, GOT_FLAG, GOT_HELP
}
 

Functions

M0_INTERNAL int m0_bcount_get (const char *arg, m0_bcount_t *out)
 
M0_INTERNAL int m0_time_get (const char *arg, m0_time_t *out)
 
int m0_getopts (const char *progname, int argc, char *const *argv, const struct m0_getopts_opt *opts, unsigned nr)
 
static void usage (const char *progname, const struct m0_getopts_opt *opts, unsigned nr)
 
static int getnum (const char *arg, const char *desc, int64_t *out)
 
static int getscaled (const char *arg, const char *desc, m0_bcount_t *out)
 

Variables

const char M0_GETOPTS_DECIMAL_POINT
 

Detailed Description

m0_getopts() is a higher-level analogue of a standard getopt(3) function. The interface is designed to avoid proliferation of nearly identical code fragments, typical for getopt(3) usage (switches nested in loops) and to hide global state exposed by getopt(3).

m0_getopts() interface is especially convenient when used together with anonymous functions (see LAMBDA() macro in lib/user_space/thread.h).

m0_getopts() is implemented on top of getopt(3).

Note
the final 's' in m0_getopts() is to avoid file-name clashes with standard library headers.
See also
lib/ut/getopts.c for usage examples.

Macro Definition Documentation

◆ M0_FLAGARG

#define M0_FLAGARG (   ch,
  desc,
  ptr 
)
Value:
{ \
.go_type = GOT_FLAG, \
.go_opt = (ch), \
.go_desc = (desc), \
.go_u = { .got_flag = (ptr) } \
}
static void ptr(struct m0_addb2__context *ctx, const uint64_t *v, char *buf)
Definition: dump.c:440

Defines a GOT_FLAG option, with a given description and a flag address.

Definition at line 232 of file getopts.h.

◆ M0_FORMATARG

#define M0_FORMATARG (   ch,
  desc,
  fmt,
  ptr 
)
Value:
{ \
.go_type = GOT_FORMAT, \
.go_opt = (ch), \
.go_desc = (desc), \
.go_u = { \
.got_fmt = { \
.f_string = (fmt), .f_out = (ptr) \
} \
} \
}
static void ptr(struct m0_addb2__context *ctx, const uint64_t *v, char *buf)
Definition: dump.c:440
char * fmt(const char *format,...) __attribute__((format(printf

Defines a GOT_FORMAT option, with a given description, argument format and address.

Definition at line 218 of file getopts.h.

◆ M0_GETOPTS

#define M0_GETOPTS (   progname,
  argc,
  argv,
  ... 
)
Value:
m0_getopts((progname), (argc), (argv), \
(const struct m0_getopts_opt []){ __VA_ARGS__ }, \
ARRAY_SIZE(((const struct m0_getopts_opt []){ __VA_ARGS__ })))
static const char * progname
Definition: traced.c:65
int m0_getopts(const char *progname, int argc, char *const *argv, const struct m0_getopts_opt *opts, unsigned nr)
Definition: ugetopts.c:98

A wrapper around m0_getopts(), calculating the size of options array.

Definition at line 169 of file getopts.h.

◆ M0_HELPARG

#define M0_HELPARG (   ch)
Value:
{ \
.go_type = GOT_HELP, \
.go_opt = (ch), \
.go_desc = "display this help and exit",\
.go_u = { .got_void = NULL } \
}
#define NULL
Definition: misc.h:38

Defines a GOT_HELP option.

Definition at line 242 of file getopts.h.

◆ M0_NUMBERARG

#define M0_NUMBERARG (   ch,
  desc,
  func 
)
Value:
{ \
.go_type = GOT_NUMBER, \
.go_opt = (ch), \
.go_desc = (desc), \
.go_u = { .got_number = (func) } \
}

Defines a GOT_NUMBER option, with a given description and a call-back.

Definition at line 187 of file getopts.h.

◆ M0_SCALEDARG

#define M0_SCALEDARG (   ch,
  desc,
  func 
)
Value:
{ \
.go_type = GOT_SCALED, \
.go_opt = (ch), \
.go_desc = (desc), \
.go_u = { .got_scaled = (func) } \
}

Defines a GOT_SCALED option, with a given description and a call-back.

Definition at line 197 of file getopts.h.

◆ M0_STRINGARG

#define M0_STRINGARG (   ch,
  desc,
  func 
)
Value:
{ \
.go_type = GOT_STRING, \
.go_opt = (ch), \
.go_desc = (desc), \
.go_u = { .got_string = (func) } \
}

Defines a GOT_STRING option, with a given description and a call-back.

Definition at line 207 of file getopts.h.

◆ M0_VOIDARG

#define M0_VOIDARG (   ch,
  desc,
  func 
)
Value:
{ \
.go_type = GOT_VOID, \
.go_opt = (ch), \
.go_desc = (desc), \
.go_u = { .got_void = (func) } \
}

Defines a GOT_VOID option, with a given description and a call-back.

Definition at line 177 of file getopts.h.

Enumeration Type Documentation

◆ m0_getopts_opt_type

Types of options supported by m0_getopts().

Enumerator
GOT_VOID 

An option without an argument.

When this option is encountered, its call-back m0_getopts_opt::go_u::got_void() is executed for its side-effects.

GOT_NUMBER 

An option with a numerical argument. The argument is expected in the format that strtoull(..., 0) can parse. When this option is encountered, its call-back m0_getopts_opt::go_u::got_number() is invoked with the parsed argument as its sole parameter.

GOT_SCALED 

An option with a numerical argument, followed by a optional multiplier suffix. The argument is expected in the format that m0_bcount_get() can parse. When this option is encountered, its call-back m0_getopts_opt::go_u::got_scaled() is invoked with the parsed argument as its sole parameter.

GOT_STRING 

An option with a string argument. When this option is encountered, its call-back m0_getopts_opt::go_u::got_string() is invoked with the string argument as its sole parameter.

GOT_FORMAT 

An options with an argument with a format that can be parsed by scanf(3). The argument string is parsed by a call to sscanf(3) with a caller-supplied format string and caller-supplied address to store the result at. No call-back is invoked. The caller is expected to analyse the contents of the address after m0_getopts() returns.

GOT_FLAG 

An option without an argument, serving as a binary flag. When this option is encountered, a user supplied boolean stored at m0_getopts_opt::go_u::got_flag is set to true. If the option wasn't encountered, the flag is set to false. No call-back is invoked. The user is expected to inspect the flag after m0_getopts() returns.

GOT_HELP 

An option without an argument.

    When this option encountered, program usage is printed to STDERR and
    program terminates immediately with exit(3).

Definition at line 54 of file getopts.h.

Function Documentation

◆ getnum()

static int getnum ( const char *  arg,
const char *  desc,
int64_t *  out 
)
static

Definition at line 74 of file ugetopts.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getscaled()

static int getscaled ( const char *  arg,
const char *  desc,
m0_bcount_t out 
)
static

Definition at line 87 of file ugetopts.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_bcount_get()

M0_INTERNAL int m0_bcount_get ( const char *  arg,
m0_bcount_t out 
)

Convert numerical argument, followed by a optional multiplier suffix, to an uint64_t value. The numerical argument is expected in the format that strtoull(..., 0) can parse. The multiplier suffix should be a char from "bkmgKMG" string. The char matches factor which will be multiplied by numerical part of argument.

Suffix char matches:

  • b = 512
  • k = 1024
  • m = 1024 * 1024
  • g = 1024 * 1024 * 1024
  • K = 1000
  • M = 1000 * 1000
  • G = 1000 * 1000 * 1000

Definition at line 35 of file getopts.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_getopts()

int m0_getopts ( const char *  progname,
int  argc,
char *const *  argv,
const struct m0_getopts_opt opts,
unsigned  nr 
)

Parses command line stored in argv[] array with argc elements according to a traditional UNIX/POSIX fashion.

Recognized options are supplied in opts[] array with nr elements.

When a parsing error occurs (unrecognized option, invalid argument format, etc.), a error message is printed on stderr, followed by a usage summary. The summary enumerates all the recognized options, their argument requirements and human-readable descriptions. Caller-supplied progname is used as a prefix of error messages.

Note
-W option is reserved by POSIX.2. GNU getopt() uses -W as a long option escape. Do not use it.

Definition at line 98 of file ugetopts.c.

Here is the call graph for this function:

◆ m0_time_get()

M0_INTERNAL int m0_time_get ( const char *  arg,
m0_time_t out 
)

Convert numerical argument, followed by a optional multiplier suffix, to an m0_time_t value. The numerical argument is expected in the format "[integer].[integer]" or just "integer", where [integer] is optional integer value in format that strtoull(..., 10) can parse, and at least one integer should be present in the numerical argument. The multiplier suffix matches unit of time and should be a string from the following list.

Suffix string matches:

  • empty string = a second
  • s = a second
  • ms = millisecond = 1/1000 of a second
  • us = microsecond = 1/1000'000 of a second
  • ns = nanosecond = 1/1000'000'000 of a second
Note
M0_GETOPTS_DECIMAL_POINT is used as decimal point in numerical argument to this function.

Definition at line 69 of file getopts.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usage()

static void usage ( const char *  progname,
const struct m0_getopts_opt opts,
unsigned  nr 
)
static

Definition at line 51 of file ugetopts.c.

Here is the caller graph for this function:

Variable Documentation

◆ M0_GETOPTS_DECIMAL_POINT

const char M0_GETOPTS_DECIMAL_POINT

Definition at line 33 of file getopts.c.