-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.h
More file actions
125 lines (98 loc) · 2.86 KB
/
cli.h
File metadata and controls
125 lines (98 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* @file cli.h
* @author bsnacks000
* @brief
* @version 0.1
* @date 2024-05-17
*
* @copyright Copyright (c) 2024
*
*/
#ifndef __CLI_H__
#define __CLI_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdlib.h>
// The max length for all tokens
#ifndef CLI_OPT_TOKEN_MAX_LEN
#define CLI_OPT_TOKEN_MAX_LEN 64
#endif
// The max length for option usage statement
#ifndef CLI_OPT_USAGE_MAX_LEN
#define CLI_OPT_USAGE_MAX_LEN 128
#endif
// Max number of options
#ifndef CLI_MAX_OPTS
#define CLI_MAX_OPTS 64
#endif
// Max number of args
#ifndef CLI_MAX_ARGS
#define CLI_MAX_ARGS 64
#endif
#define CLI_UNUSED(x) (void)(x)
#define CLI_CHECK_MEM_ALLOC(value) \
do { \
if ((value) == NULL) { \
fprintf(stderr, "Out of memory."); \
exit(EXIT_FAILURE); \
} \
} while (0)
typedef enum cli_err {
CLI_OK = 0,
CLI_PARSE_FAILED_STR,
CLI_PARSE_FAILED_INT,
CLI_PARSE_FAILED_FLOAT,
CLI_PARSE_FAILED_BOOL,
CLI_FULL_REGISTRY,
CLI_NOT_FOUND,
CLI_NAME_REQUIRED,
CLI_UNSEEN_REQ_OPTS,
CLI_OUT_OF_BOUNDS,
CLI_ALREADY_SEEN,
CLI_ARG_COUNT,
CLI_PRINT_HELP_AND_EXIT,
CLI_TOKEN_TOO_LONG,
CLI_USAGE_STR_TOO_LONG
} cli_err;
void cli_print_err(cli_err err);
typedef struct cli_command cli_command;
cli_command* cli_command_new(void);
void cli_command_destroy(cli_command* cli);
cli_err cli_init(cli_command* cli,
const char* desc,
const char* usage,
int argc,
char** argv);
void cli_cleanup(cli_command* cli);
// high level API for adding options and arguments
cli_err cli_add_flag(cli_command* cli,
const char* name,
const char* usage,
bool* value);
cli_err cli_add_int_argument(cli_command* cli, int* value);
cli_err cli_add_int_option(cli_command* cli,
const char* name,
const char* usage,
int* value,
bool required);
cli_err cli_add_float_argument(cli_command* cli, float* value);
cli_err cli_add_float_option(cli_command* cli,
const char* name,
const char* usage,
float* value,
bool required);
cli_err cli_add_str_argument(cli_command* cli, char* value, size_t buf_size);
cli_err cli_add_str_option(cli_command* cli,
const char* name,
const char* usage,
char* value,
bool required,
size_t buf_size);
void cli_print_help_and_exit(cli_command* cli, int status);
cli_err cli_parse(cli_command* cli);
#ifdef __cplusplus
}
#endif
#endif //!__CLI_H__