JackUtils 0.5
Provides simplified Jack API for clients :)
Loading...
Searching...
No Matches
ju_args.h
1/* Jack Utils Argument parser extension
2 * Copyright (C) UtoECat 2022. All rights Reserved!
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
18#pragma once
19
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#warning deprecated file!
25
26#define JA_API static inline
27
28#ifndef PROGRAM_NAME
29#define PROGRAM_NAME "dummy"
30#endif
31
32#ifndef PROGRAM_VERSION
33#define PROGRAM_VERSION 0.0
34#endif
35
36#ifndef PROGRAM_USAGE
37#define PROGRAM_USAGE "no usage defined!"
38#endif
39
40#ifndef PROGRAM_HELP
41#define PROGRAM_HELP "help is not defined!"
42#endif
43
47JA_API void ja_parse(int argc, char* const* argv, void (*cb) (char, const char*), const char* e) {
48 if (!cb && e) return;
49 char c = 0;
50
51 size_t s = e ? strlen(e) : 0;
52 char exp[s + 3];
53 exp[0] = 'v';
54 exp[1] = 'h';
55 if (e) memcpy(exp + 2, e, s+1);
56
57 while ((c = getopt(argc, argv, exp)) != -1) {
58 switch (c) {
59 case 'v':
60 printf("%s %f\n", PROGRAM_NAME, PROGRAM_VERSION);
61 break;
62 case 'h':
63 printf("%s %f : %s\n%s\n", PROGRAM_NAME, PROGRAM_VERSION, PROGRAM_USAGE, PROGRAM_HELP);
64 break;
65 case '?' :
66 printf("abort...\n");
67 exit(1);
68 break;
69 default :
70 if (cb) cb(c, optarg);
71 };
72 };
73}