clix
Single-header file CLI libraries for C
Loading...
Searching...
No Matches
args.h: Parser Helpers

Overview

Parser callback generation and built-in parser helpers.

Built-in parser wrappers
ARG_PARSE_L, ARG_PARSE_LL, ARG_PARSE_UL, ARG_PARSE_ULL, ARG_PARSE_F, and ARG_PARSE_D are convenience wrappers around ARG_PARSER.
Custom parser contract
Custom parser callbacks return ARG_INVALID or ARG_VALID and use signature (const char *str, void *dest).

Parser Generation

#define ARG_PARSER(name, strto, ARG_BASE, strto_t, dest_t, CAST, cond, err)
 Generates a typed parser callback with common conversion checks.
#define ARG_BASE(N)
 Helper for the ARG_BASE parameter in ARG_PARSER.
#define ARG_ERR   "Invalid value"
 Default parser error message if err is empty in ARG_PARSER.
#define ARG_INVALID(msg)
 Creates a failing arg_callback value.
#define ARG_VALID()
 Creates a successful arg_callback value.

Built-in Parser Wrappers

#define ARG_PARSE_L(name, base, dest_t, CAST, cond, err)
 Convenience wrapper of ARG_PARSER using strtol.
#define ARG_PARSE_LL(name, base, dest_t, CAST, cond, err)
 Convenience wrapper of ARG_PARSER using strtoll.
#define ARG_PARSE_UL(name, base, dest_t, CAST, cond, err)
 Convenience wrapper of ARG_PARSER using strtoul.
#define ARG_PARSE_ULL(name, base, dest_t, CAST, cond, err)
 Convenience wrapper of ARG_PARSER using strtoull.
#define ARG_PARSE_F(name, dest_t, CAST, cond, err)
 Convenience wrapper of ARG_PARSER using strtof.
#define ARG_PARSE_D(name, dest_t, CAST, cond, err)
 Convenience wrapper of ARG_PARSER using strtod.

Data Structures

struct  args_raw
 Copy of the raw process argument vector. More...

Macro Definition Documentation

◆ ARG_PARSER

#define ARG_PARSER ( name,
strto,
ARG_BASE,
strto_t,
dest_t,
CAST,
cond,
err )

Generates a typed parser callback with common conversion checks.

The generated function is named parse_<name> and matches argument::parse_callback. It parses str, rejects malformed input, range errors, and custom condition failures, then writes to dest.

Parameters
nameSuffix used for generated function name.
strtoConversion routine, e.g. strtol or strtod.
ARG_BASEMacro, leave out macro for no base, or e.g. ARG_BASE(10) to pass to strto.
strto_tReturn type of strto.
dest_tDestination pointee type.
CASTOptional cast expression when narrowing/widening.
condRejection predicate evaluated against local variable val.
errError text, falls back to ARG_ERR when empty.
Return values
ARG_VALIDInput accepted and stored in dest.
ARG_INVALIDInput rejected with diagnostic message.

◆ ARG_BASE

#define ARG_BASE ( N)

Helper for the ARG_BASE parameter in ARG_PARSER.

Note
If the conversion function doesn't take a base parameter, omit the macro like in the ARG_PARSE_F macro definition.
Parameters
NNumeric base to pass to conversion functions, e.g. 10 for decimal or 16 for hex.

◆ ARG_ERR

#define ARG_ERR   "Invalid value"

Default parser error message if err is empty in ARG_PARSER.

Remarks
Overridable before including args.h.

◆ ARG_INVALID

#define ARG_INVALID ( msg)

Creates a failing arg_callback value.

Parameters
msgDiagnostic message to show the user, e.g. "Must be a positive integer".