gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
cmdapp.h
Go to the documentation of this file.
1// cmdapp: cmdapp.h
2// Copyright (C) 2021 Jérémie Astor
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef _CMDAPP_APP_H
18#define _CMDAPP_APP_H
19
20#include <stdbool.h>
21#include <stdint.h>
22#include <stddef.h>
23#include <stdlib.h>
24
25// So it works on windows
26#ifndef EXIT_FAILURE
27#define EXIT_FAILURE 1
28#endif
29#ifndef EXIT_SUCCESS
30#define EXIT_SUCCESS 0
31#endif
32
33typedef uint8_t cmdopt_flags_t;
34typedef uint8_t cmdapp_mode_t;
35
36typedef struct {
37 char shorto;
38 const char* longo;
39 const char* value;
40 const char* argtype;
42} cmdopt_t;
43
44#define CMDOPT_EXISTS 0b00000001
45#define CMDOPT_OPTIONAL 0b00000010
46#define CMDOPT_TAKESARG 0b00000100
47#define CMDOPT_MAYTAKEARG 0b00001000
48
49#define CMDAPP_MODE_MULTIFLAG 0b00000000
50#define CMDAPP_MODE_SHORTARG 0b00000001
51#define CMDAPP_MODE_SILENT 0b00000000
52#define CMDAPP_MODE_PRINT 0b00000010
53#define _CMDAPP_MODE_EXIT 0b10000000
54
55// Returns nonzero if the option was provided to the app
56#define cmdopt_exists(opt) ((opt).flags & CMDOPT_EXISTS)
57// Returns nonzero if the option was declared as optional
58#define cmdopt_is_optional(opt) ((opt).flags | CMDOPT_OPTIONAL)
59
61
62typedef struct {
63 const char* program;
64 const char** synopses;
65 const char* version;
66 const char* author;
67 int year;
68 const char* description;
70 const char* ver_extra;
72
73typedef struct {
74 size_t length;
75 const char** contents;
76} cmdargs_t;
77
78typedef void (*cmdapp_procedure_t)(void *data, cmdopt_t* option, const char* arg);
79
94
95// Returns nonzero if the program should terminate. Zero otherwise.
96#define cmdapp_should_exit(app) ((app)->_mode & _CMDAPP_MODE_EXIT)
97
98// Initializes a cmdapp_t with the given program environment, mode and metadata.
99void cmdapp_init(cmdapp_t* app, int argc, char** argv, cmdapp_mode_t mode,
100 const cmdapp_info_t* info);
101
102// Destroys the given cmdapp_t. Any subsequent member access is undefined.
103void cmdapp_destroy(cmdapp_t* app);
104
105// Registers an option to the app with the given values and flags
106void cmdapp_set(cmdapp_t* app, char shorto, const char* longo, uint8_t flags,
107 cmdopt_t** conflicts, const char* description,
108 const char* argtype,
109 cmdopt_t* option);
110
111// Generates a --help output to stdout. If cmdapp_info was not called, the
112// function behavior is undefined
113void cmdapp_print_help(cmdapp_t* app);
114
115// Generates a --version output to stdout. If cmdapp_info was not called, the
116// function behavior is undefined
118
119// Sets the cmdapp_t's procedural parsing function to the given function. If set
120// to NULL, it disables procedural parsing (default).
121// user_data will be passed as an argument to the procedure
122void cmdapp_enable_procedure(cmdapp_t* app, cmdapp_procedure_t proc, void *user_data);
123
124// Returns EXIT_SUCCESS on success and EXIT_FAILURE otherwise (printing a
125// diagnostic to stderr if configured)
126int cmdapp_run(cmdapp_t* app);
127
128// Returns a pointer to an array of standalone command line arguments, or NULL
129// if none exist.
131
132// Prints a formatted error message to stderr.
133void cmdapp_error(cmdapp_t* app, const char* fmt, ...);
134
135#endif /* _CMDAPP_APP_H */
void cmdapp_set(cmdapp_t *app, char shorto, const char *longo, uint8_t flags, cmdopt_t **conflicts, const char *description, const char *argtype, cmdopt_t *option)
Definition cmdapp.c:77
void cmdapp_enable_procedure(cmdapp_t *app, cmdapp_procedure_t proc, void *user_data)
Definition cmdapp.c:111
void cmdapp_print_version(cmdapp_t *app)
Definition cmdapp.c:172
uint8_t cmdopt_flags_t
Definition cmdapp.h:33
void cmdapp_print_help(cmdapp_t *app)
Definition cmdapp.c:116
cmdargs_t * cmdapp_getargs(cmdapp_t *app)
Definition cmdapp.c:352
int cmdapp_run(cmdapp_t *app)
Definition cmdapp.c:222
uint8_t cmdapp_mode_t
Definition cmdapp.h:34
void(* cmdapp_procedure_t)(void *data, cmdopt_t *option, const char *arg)
Definition cmdapp.h:78
void cmdapp_error(cmdapp_t *app, const char *fmt,...)
Definition cmdapp.c:364
void cmdapp_destroy(cmdapp_t *app)
Definition cmdapp.c:59
void cmdapp_init(cmdapp_t *app, int argc, char **argv, cmdapp_mode_t mode, const cmdapp_info_t *info)
Definition cmdapp.c:43
char const char * fmt
Definition gwion_print.h:2
const char * program
Definition cmdapp.h:63
int help_des_offset
Definition cmdapp.h:69
const char * description
Definition cmdapp.h:68
const char * ver_extra
Definition cmdapp.h:70
const char * version
Definition cmdapp.h:65
const char * author
Definition cmdapp.h:66
const char ** synopses
Definition cmdapp.h:64
int _argc
Definition cmdapp.h:81
void * _user_data
Definition cmdapp.h:92
size_t _capacity
Definition cmdapp.h:88
cmdargs_t _args
Definition cmdapp.h:90
int _custom_help
Definition cmdapp.h:85
char ** _argv
Definition cmdapp.h:82
int _custom_ver
Definition cmdapp.h:86
cmdapp_info_t _info
Definition cmdapp.h:84
cmdapp_mode_t _mode
Definition cmdapp.h:83
size_t _length
Definition cmdapp.h:87
cmdopt_internal_t ** _start
Definition cmdapp.h:89
cmdapp_procedure_t _proc
Definition cmdapp.h:91
size_t length
Definition cmdapp.h:74
const char ** contents
Definition cmdapp.h:75
const char * value
Definition cmdapp.h:39
cmdopt_flags_t flags
Definition cmdapp.h:41
const char * longo
Definition cmdapp.h:38
char shorto
Definition cmdapp.h:37
const char * argtype
Definition cmdapp.h:40
void void * arg
Definition threadpool.h:27