-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdlcmdline.h
159 lines (116 loc) · 3.55 KB
/
dlcmdline.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* @file dlcmdline.h
* @brief Command-line parsing types and function prototypes
* @author Dominique LaSalle <[email protected]>
* Copyright (c) 2013-2015, Dominique LaSalle
* @version 1
* @date 2013-10-08
*/
#ifndef DL_CMDLINE_H
#define DL_CMDLINE_H
#include "domlib.h"
/******************************************************************************
* TYPES ***********************************************************************
******************************************************************************/
typedef enum cmd_opt_type_t {
CMD_OPT_CHOICE,
CMD_OPT_STRING,
CMD_OPT_BOOL,
CMD_OPT_INT,
CMD_OPT_FLOAT,
CMD_OPT_FLAG,
CMD_OPT_XARG,
CMD_OPT_NULL
} cmd_opt_type_t;
typedef struct cmd_opt_pair_t {
const char * str;
const char * desc;
int val;
} cmd_opt_pair_t;
typedef struct cmd_opt_t {
ssize_t id;
char sflag;
const char * lflag;
const char * desc;
cmd_opt_type_t type;
const cmd_opt_pair_t * vals;
size_t nvals;
} cmd_opt_t;
typedef struct cmd_arg_t {
ssize_t id;
cmd_opt_type_t type;
union {
long long int i;
double f;
const char * s;
int b;
int o;
} val;
} cmd_arg_t;
typedef enum dl_cmdline_error_t {
DL_CMDLINE_SUCCESS,
DL_CMDLINE_BAD_ARGUMENT,
DL_CMDLINE_UNKNOWN_ARGUMENT,
DL_CMDLINE_MISSING_PARAMETER,
DL_CMDLINE_EXTRA_PARAMETER,
DL_CMDLINE_BAD_PARAMETER
} dl_cmdline_error;
/******************************************************************************
* MEMORY HEADERS **************************************************************
******************************************************************************/
#define DLMEM_PREFIX cmd_opt
#define DLMEM_TYPE_T cmd_opt_t
#include "dlmem_headers.h"
#undef DLMEM_PREFIX
#undef DLMEM_TYPE_T
#define DLMEM_PREFIX cmd_arg
#define DLMEM_TYPE_T cmd_arg_t
#include "dlmem_headers.h"
#undef DLMEM_PREFIX
#undef DLMEM_TYPE_T
/******************************************************************************
* PUBLIC FUNCTIONS ************************************************************
******************************************************************************/
/**
* @brief Initialization function for the cmd_opt_t structure. Initialization
* involves zeroing out all fields.
*
* @param cmd_opt The structure to initialize.
*
* @return The initialized structure.
*/
cmd_opt_t * init_cmd_opt(cmd_opt_t * cmd_opt);
/**
* @brief Initialization function for the cmd_arg_t structure. Initialization
* involves zeroing otu all fields.
*
* @param cmd_arg The structure to initialize.
*
* @return The initialized structure.
*/
cmd_arg_t * init_cmd_arg(cmd_arg_t * cmd_arg);
/**
* @brief Parse the command line options passed to the main function and place
* them in cmd_arg_t structures for processing.
*
* @param argc The number of command line arguments.
* @param argv The command line arguments.
* @param opts The command line options to match against.
* @param nopts The size of the opts array.
* @param args The parsed aguments.
* @param nargs The number of parsed arguments.
*
* @return DL_CMDLINE_SUCCESS unless an error in parsing occurs.
*/
int cmd_parse_args(size_t argc, char ** argv, const cmd_opt_t * opts,
size_t nopts, cmd_arg_t ** args, size_t * nargs);
/**
* @brief This function generates and prints the helpfile associated with the
* passed in command line options.
*
* @param out The file stream to print to.
* @param opts The options array to print.
* @param nopts The size of the options array.
*/
void fprint_cmd_opts(FILE * out, const cmd_opt_t * opts, size_t nopts);
#endif