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#pragma once
17
18#include <stdbool.h>
19#include <stdint.h>
20#include <stddef.h>
21#include <stdlib.h>
22
23// So it works on windows
24#ifndef EXIT_FAILURE
25#define EXIT_FAILURE 1
26#endif
27#ifndef EXIT_SUCCESS
28#define EXIT_SUCCESS 0
29#endif
30
31typedef uint8_t cmdopt_flags_t;
32typedef uint8_t cmdapp_mode_t;
33
34typedef struct {
35 char shorto;
36 const char* longo;
37 const char* value;
38 const char* argtype;
40} cmdopt_t;
41
42#define CMDOPT_EXISTS 0b00000001
43#define CMDOPT_OPTIONAL 0b00000010
44#define CMDOPT_TAKESARG 0b00000100
45#define CMDOPT_MAYTAKEARG 0b00001000
46
47#define CMDAPP_MODE_MULTIFLAG 0b00000000
48#define CMDAPP_MODE_SHORTARG 0b00000001
49#define CMDAPP_MODE_SILENT 0b00000000
50#define CMDAPP_MODE_PRINT 0b00000010
51#define _CMDAPP_MODE_EXIT 0b10000000
52
53// Returns nonzero if the option was provided to the app
54#define cmdopt_exists(opt) ((opt).flags & CMDOPT_EXISTS)
55// Returns nonzero if the option was declared as optional
56#define cmdopt_is_optional(opt) ((opt).flags | CMDOPT_OPTIONAL)
57
59
60typedef struct {
61 const char* program;
62 const char** synopses;
63 const char* version;
64 const char* author;
65 int year;
66 const char* description;
68 const char* ver_extra;
70
71typedef struct {
72 size_t length;
73 const char** contents;
74} cmdargs_t;
75
76typedef void (*cmdapp_procedure_t)(void *data, cmdopt_t* option, const char* arg);
77
92
93// Returns nonzero if the program should terminate. Zero otherwise.
94#define cmdapp_should_exit(app) ((app)->_mode & _CMDAPP_MODE_EXIT)
95
96// Initializes a cmdapp_t with the given program environment, mode and metadata.
97void cmdapp_init(cmdapp_t* app, int argc, char** argv, cmdapp_mode_t mode,
98 const cmdapp_info_t* info);
99
100// Destroys the given cmdapp_t. Any subsequent member access is undefined.
101void cmdapp_destroy(cmdapp_t* app);
102
103// Registers an option to the app with the given values and flags
104void cmdapp_set(cmdapp_t* app, char shorto, const char* longo, uint8_t flags,
105 cmdopt_t** conflicts, const char* description,
106 const char* argtype,
107 cmdopt_t* option);
108
109// Generates a --help output to stdout. If cmdapp_info was not called, the
110// function behavior is undefined
111void cmdapp_print_help(cmdapp_t* app);
112
113// Generates a --version output to stdout. If cmdapp_info was not called, the
114// function behavior is undefined
116
117// Sets the cmdapp_t's procedural parsing function to the given function. If set
118// to NULL, it disables procedural parsing (default).
119// user_data will be passed as an argument to the procedure
120void cmdapp_enable_procedure(cmdapp_t* app, cmdapp_procedure_t proc, void *user_data);
121
122// Returns EXIT_SUCCESS on success and EXIT_FAILURE otherwise (printing a
123// diagnostic to stderr if configured)
124int cmdapp_run(cmdapp_t* app);
125
126// Returns a pointer to an array of standalone command line arguments, or NULL
127// if none exist.
129
130// Prints a formatted error message to stderr.
131void cmdapp_error(cmdapp_t* app, const char* fmt, ...);
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:31
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:32
void(* cmdapp_procedure_t)(void *data, cmdopt_t *option, const char *arg)
Definition cmdapp.h:76
struct _cmdopt_internal_t cmdopt_internal_t
Definition cmdapp.h:58
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:61
int help_des_offset
Definition cmdapp.h:67
const char * description
Definition cmdapp.h:66
const char * ver_extra
Definition cmdapp.h:68
const char * version
Definition cmdapp.h:63
const char * author
Definition cmdapp.h:64
const char ** synopses
Definition cmdapp.h:62
int _argc
Definition cmdapp.h:79
void * _user_data
Definition cmdapp.h:90
size_t _capacity
Definition cmdapp.h:86
cmdargs_t _args
Definition cmdapp.h:88
int _custom_help
Definition cmdapp.h:83
char ** _argv
Definition cmdapp.h:80
int _custom_ver
Definition cmdapp.h:84
cmdapp_info_t _info
Definition cmdapp.h:82
cmdapp_mode_t _mode
Definition cmdapp.h:81
size_t _length
Definition cmdapp.h:85
cmdopt_internal_t ** _start
Definition cmdapp.h:87
cmdapp_procedure_t _proc
Definition cmdapp.h:89
size_t length
Definition cmdapp.h:72
const char ** contents
Definition cmdapp.h:73
const char * value
Definition cmdapp.h:37
cmdopt_flags_t flags
Definition cmdapp.h:39
const char * longo
Definition cmdapp.h:36
char shorto
Definition cmdapp.h:35
const char * argtype
Definition cmdapp.h:38
void void * arg
Definition threadpool.h:26