libclap 1.0.0
Command Line Argument Parser for C
Loading...
Searching...
No Matches
Typedefs | Enumerations
clap_types.h File Reference

Type definitions for libclap. More...

#include <stdbool.h>
#include <stddef.h>
#include <clap/clap_error.h>
#include <clap/clap_export.h>
Include dependency graph for clap_types.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct clap_parser_s clap_parser_t
 
typedef struct clap_argument_s clap_argument_t
 
typedef struct clap_namespace_s clap_namespace_t
 
typedef bool(* clap_type_handler_t) (const char *input, void *output, size_t output_size, clap_error_t *error)
 Type validation / conversion handler function pointer.
 
typedef bool(* clap_action_handler_t) (clap_parser_t *parser, clap_argument_t *argument, clap_namespace_t *namespace, const char **values, size_t value_count, void *user_data, clap_error_t *error)
 Custom action handler function pointer.
 

Enumerations

enum  clap_parse_result_t { CLAP_PARSE_SUCCESS = 0 , CLAP_PARSE_ERROR = 1 , CLAP_PARSE_HELP = 2 , CLAP_PARSE_VERSION = 3 }
 Parse result codes returned by clap_parse_args() More...
 

Detailed Description

Type definitions for libclap.

Typedef Documentation

◆ clap_action_handler_t

typedef bool(* clap_action_handler_t) (clap_parser_t *parser, clap_argument_t *argument, clap_namespace_t *namespace, const char **values, size_t value_count, void *user_data, clap_error_t *error)

Custom action handler function pointer.

Invoked when a CUSTOM-action argument is encountered during parsing. The handler receives the full parser context and the list of values associated with this argument occurrence.

Parameters
parserThe parser being used for this parse run. The handler may inspect parser state but should not add/modify arguments or groups during parsing.
argumentThe argument definition that triggered this handler.
namespaceThe result namespace. Write parsed values here via clap_namespace_set_* / clap_namespace_append_*.
valuesArray of raw string values for this occurrence. For nargs=1 or the first value of nargs>1, this contains one element. The handler is called once per argument occurrence.
value_countNumber of elements in values.
user_dataOpaque pointer set via clap_argument_data(). May be NULL.
errorSet on failure with a descriptive message.
Returns
true on success, false on failure (parse aborts with error).
Example
static bool count_lines(clap_parser_t *p, clap_argument_t *a,
clap_namespace_t *ns, const char **vals,
size_t n, void *data, clap_error_t *err) {
(void)p; (void)a; (void)data;
FILE *f = fopen(vals[0], "r");
if (!f) { clap_error_set(err, CLAP_ERR_CUSTOM, "cannot open"); return false; }
long lines = 0; int c;
while ((c = fgetc(f)) != EOF) if (c == '\n') lines++;
fclose(f);
return clap_namespace_set_int(ns, "lines", (int)lines);
}
CLAP_EXPORT bool clap_namespace_set_int(clap_namespace_t *ns, const char *name, int value)
Set an int value in the namespace.
#define CLAP_ERR_CUSTOM
Definition clap_error.h:31
CLAP_EXPORT void clap_error_set(clap_error_t *error, int code, const char *format,...)
Set error code and printf-style message.
Argument definition (positional or optional).
Error information structure.
Definition clap_error.h:36
Container for parsed argument values.
Parsing engine state and configuration.

◆ clap_argument_t

◆ clap_namespace_t

◆ clap_parser_t

typedef struct clap_parser_s clap_parser_t

◆ clap_type_handler_t

typedef bool(* clap_type_handler_t) (const char *input, void *output, size_t output_size, clap_error_t *error)

Type validation / conversion handler function pointer.

Converts a string input into a typed value. Built-in handlers exist for "string", "int", "float", and "bool" and their converted output is persisted in the namespace.

Custom type handlers registered via clap_register_type() are used for validation only — the handler must return false on invalid input, but the converted output value is discarded. The raw input string is always stored in the namespace, retrievable via clap_namespace_get_string(). Use CLAP_ACTION_CUSTOM if you need to persist a typed conversion result.

Parameters
inputNUL-terminated string to convert.
outputPointer to output buffer of output_size bytes. The handler writes the converted value here.
output_sizeSize of the output buffer in bytes.
errorOptional (may be NULL). Set on failure with a descriptive message.
Returns
true on success, false on conversion failure (error is set).

Enumeration Type Documentation

◆ clap_parse_result_t

Parse result codes returned by clap_parse_args()

CLAP_PARSE_SUCCESS (0): Parsing completed successfully. CLAP_PARSE_ERROR (1): An error occurred; check clap_error_t. CLAP_PARSE_HELP (2): –help or -h was requested; help has been printed to stdout. The caller should exit with code 0. CLAP_PARSE_VERSION(3): –version was requested; version has been printed to stdout. The caller should exit with code 0.

Enumerator
CLAP_PARSE_SUCCESS 
CLAP_PARSE_ERROR 
CLAP_PARSE_HELP 
CLAP_PARSE_VERSION