-
Notifications
You must be signed in to change notification settings - Fork 1
/
arghlp.h
137 lines (105 loc) · 3.85 KB
/
arghlp.h
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
/** @file arghlp.h
* @brief extend getopt for a more readable use of
* arguments with an intergrated description
* field for improved code readability,
* Define your helper at the same time as you
* define your flags
*
* @Copyright (C) 2024 Umar Ba [email protected] @OpenWire Studio .HomeLab
* 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.
*/
#if!defined (__argument_helper)
#define __argument_helper
#if __GNUC_PREREQ(3,4)
# pragma once
#endif
#include <getopt.h>
#ifdef __cplusplus
# define ARGHLP extern "C"
#else
# define ARGHLP
#endif
#define nullable NULL
#define MAX_BUFF 255
#ifndef USAGE_BUFF
#define USAGE_BUFF 5000 //! That's arbitrary
#endif
#define OPT_END { nullable,0,nullable,0}
#define OPTX_END {OPT_END,0}
#define OPT_END_DEF (struct option) OPT_END
#define OPTX_END_DEF (struct optionx) OPTX_END
#define def(__option) __option##_DEF
#ifndef BUILD_FROM_SNAME
//! By default it use option.val to build short arguments
# define BUILD_FROM_SVAL
#endif
#define ARGENTRY_SYMB 58
//! FORMATING
#if !defined(HELPER_FRMT)
# define HELPER_FRMT " -%c, --%s %s"
#endif
#if !defined(USAGE_FRMT)
# define USAGE_FRMT "USAGE: %s \n"
#endif
//! __help__
#define __help__ fprintf(stdout ,"%s\n", __usage)
#define __usage__ __help__
#define __display_usage__ __help__
enum {
REQUIRED,
#ifdef required_argument
# define REQUIRED required_argument
#endif
OPTIONAL
#ifdef optional_argument
# define OPTIONAL optional_argument
#endif
};
enum {
ARGREQUIERED ,
#define ARGREQUIERED (1 << ARGREQUIERED)
ARGOPTIONAL
#define ARGOPTIONAL (1 << ARGOPTIONAL)
};
#define __attribute_option(__attr__) ARG#__attr__
typedef struct __flags_t flags_t ; //! Opaque type of flags
extern char __usage[USAGE_BUFF] ; //!
extern char __shopt[MAX_BUFF] ;
extern struct option __optl[MAX_BUFF] ;
extern int __nargp ; //! arguments counter
struct optionx {
struct option optlong ;
char optdesc[MAX_BUFF];
};
struct synopsys_t {
char * header_description ;
char * footer_description ;
};
typedef struct arghlp arghlp;
typedef void *(*arghlp_handler)(int __ac , char *const * __av ,
const char * __shortopt ,
struct option * __longoption ,
void * __wur __args) ;
struct arghlp {
struct synopsys_t synopsys ;
struct optionx * options;
arghlp_handler ah_handler;
};
ARGHLP static char * get_program_basename(char *const * __argument_vector) __wur __nonnull((1));
ARGHLP static void * set_option( char * __shortopt , int __has_arg ) ;
ARGHLP static char * build_short_option(const struct optionx * __raw_optionx) __wur __nonnull((1));
ARGHLP void static build_usage_helper(char* flags_t, const char * __flag_description) __nonnull((1,2));
ARGHLP static char * make_sysnopsys(char *__basename, struct arghlp * __synopsys, char * __usage_body) __nonnull((1,2,3)) ;
ARGHLP static struct option * extract_getopt_option(struct option * __g_option) __nonnull(()) ;
ARGHLP void *arghlp_context ( int __argcounter, char * const *__argvector ,
const struct arghlp *__arghlp , void * __wur __argxtra) __nonnull((2,3));
ARGHLP char *optentry(char * const * __av ,int __ac , int __optindex) ;
#endif //!__argument_helper