-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwimey.h
More file actions
146 lines (124 loc) · 3.84 KB
/
Copy pathwimey.h
File metadata and controls
146 lines (124 loc) · 3.84 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* wimey.h - Wimey command line management library
*
* Copyright (C) 2025 Davide Usberti <usertibox@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __WIMEY_H
#define __WIMEY_H
#ifdef _cplusplus
extern "C" {
#endif
#include <stdint.h>
#define WIMEY_OK 1
#define WIMEY_ERR 0
/* Debug level value:
* 0 - Errors only
* 1 - Errors & Warns
* 2 - Errors, Warns and Info
*/
#define LOG_ERR_ONLY 0
#define LOG_ERR_AND_WARNS 1
#define LOG_ALL 2
#define RESET "\033[0m"
#define RED "\033[1;31m"
#define YELLOW "\033[1;33m"
#define GREEN "\033[1;32m"
#define ERR(msg, ...) \
do { \
fprintf(stderr, RED "ERROR " RESET msg "\n", ##__VA_ARGS__); \
} while (0)
#define WARN(msg, ...) \
do { \
printf(YELLOW "WARN " RESET msg "\n", ##__VA_ARGS__); \
} while (0)
#define INFO(msg, ...) \
do { \
printf(GREEN "INFO " RESET msg "\n", ##__VA_ARGS__); \
} while (0)
struct wimey_config_t {
int log_level;
char name[32]; /* program name */
char *description; /* program description */
char *usage; /* Example: tung [options] url */
char *version; /* version: x.x.x */
char *copyright; /* Example: Copyright (C) 2025 Davide Usberti */
char *license; /* Example: Apache v2.0 */
};
struct wimey_command_t {
char *key; /* Command key */
int has_value; /* Boolean example: run <value?> */
int is_value_required;
char *value_name; /* or NULL */
char *desc;
void (*callback)(const char *value);
};
enum wimey_argument_type {
WIMEY_LONG = 1 << 0,
WIMEY_DOUBLE = 1 << 2,
WIMEY_STR = 1 << 3,
WIMEY_BOOL = 1 << 4
};
struct wimey_argument_t {
char *long_key; /* Example: --help */
char *short_key; /* Example: -h */
int has_value; /* Exampel: --port <value> */
int is_value_required; /* Example: --delay <delay> | --delay (default: 0) */
void *value_dest; /* destination variable pointer */
char *value_name;
enum wimey_argument_type value_type; /* long, dobule, str, bool */
char *desc;
/* here no callback because arguments just
* assign a value to a variable */
};
struct __wimey_command_node {
struct wimey_command_t cmd;
struct __wimey_command_node *next;
};
struct __wimey_argument_node {
struct wimey_argument_t argument;
struct __wimey_argument_node *next;
};
/* ------ Public API ------ */
/* Configuration & Init */
int wimey_init(void);
int wimey_set_config(struct wimey_config_t *conf);
struct wimey_config_t wimey_get_config(void);
void wimey_free_all(void);
/* This function is an universal wrapper
* both for commands and arguments */
int wimey_parse(int argc, char **argv);
/* Commands */
int wimey_add_command(struct wimey_command_t cmd);
/* In some case if we need to iterate on the commands list
* this functions return his head, so It's an internal
* function but public. */
struct __wimey_command_node *wimey_get_commands_head(void);
/* Arguments */
int wimey_add_argument(struct wimey_argument_t argument);
struct __wimey_argument_node *wimey_get_arguments_head(void);
/* Utility function */
int wimey_generate_help();
long wimey_val_to_long(const char *val);
int wimey_val_to_int(const char *val);
float wimey_val_to_float(const char *val);
double wimey_val_to_double(const char *val);
uint64_t wimey_val_to_u64(const char *val);
char wimey_val_to_char(const char *val);
#ifdef _cplusplus
}
#endif
#endif /* __WIMEY_H */
#pragma once