• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H04-Jun-2019-

sub/testoasterror/H03-May-2022-

test/H04-Jun-2019-

.giteaH A D04-Jun-2019113

.githubH A D04-Jun-2019109

.gitignoreH A D04-Jun-201920

license.mdH A D04-Jun-2019483

makefileH A D04-Jun-20191.2 KiB

readme.mdH A D04-Jun-20193.4 KiB

readme.md

1# Argoat
2Argoat is a lightweight library for command-line options parsing.
3This was created because most of the existing solutions rely heavily on macros,
4and all of them expect you to write a giant switch to handle the given options.
5
6Argoat allows you to deal with arguments using function pointers.
7It does not use any macro, switch, or dynamic memory allocation.
8
9Argoat supports the following syntaxes:
10 - simple options    `test -a -b`
11 - compound options  `test -ab`
12 - assigned options  `test -c=4 -d 2`
13 - long options      `test --code 4 --den 2`
14 - lone dash         `test --oki - --den 2`
15 - lone double-dash  `test --oki -- --doki`
16 - unflagged options `test 0 -c=4 1 -d=2 3`
17 - limited params    `test 0 -c 4 1 -d 2 3`
18
19Argoat does not support the following syntaxes *on purpose*:
20 - simple neighbours `test -a4`
21 - custom symbols    `test +a 4`
22
23All of that in around 200 lines of code (getopt has approximately 700).
24Don't be shy, sneak a goat in your code.
25
26## Cloning
27Execute `make gitea` or `make github` to initialize the required submodules
28(depending on where you cloned this repo from).
29
30## Testing
31Run `make` to compile the testing suite, and `make run` to perform the tests.
32
33## Using
34### TL;DR
35```
36#include "argoat.h"
37#include <stdbool.h>
38#include <stdio.h>
39
40void handle_main(void* data, char** pars, const int pars_count)
41{
42}
43
44void handle_bool(void* data, char** pars, const int pars_count)
45{
46	*((bool*) data) = true;
47}
48
49int main(int argc, char** argv)
50{
51	bool data1 = false;
52	char* unflagged[23];
53
54	const struct argoat_sprig sprigs[2] =
55	{
56		{NULL, 0, NULL, handle_main},
57		{"t", 0, (void*) &data1, handle_bool}
58	};
59
60	struct argoat args = {sprigs, 2, unflagged, 0, 23};
61	argoat_graze(&args, argc, argv);
62	printf("%c\n", data1 ? '1' : '0');
63
64	return 0;
65}
66```
67
68### Details
69Include `argoat.h` and compile `argoat.c` with your code.
70
71Write the functions that will handle your parameters.
72They will be called during the parsing process, in the order given by the user
73```
74void handle_main(void* data, char** pars, const int pars_count)
75{
76}
77
78void handle_bool(void* data, char** pars, const int pars_count)
79{
80	*((bool*) data) = true;
81}
82```
83
84In your `int main(int argc, char** argv)`, declare the variables to configure.
85They will be passed to the corresponding functions as `void* data`
86```
87bool data1 = false;
88```
89
90Also declare an array of strings to store the unflagged arguments.
91Just choose a size corresponding to the maximum number of unflagged arguments
92your program supports, or create a null pointer if it does not use them.
93```
94char* unflagged[UNFLAGGED_MAX];
95```
96
97Then, declare an array of flag structures.
98The first entry only has to contain the unflagged-arguments handling function.
99The others must specify:
100 - the name of the flag (one char for '-' prefix, multiple chars for '--')
101 - the maximum number of arguments supported by this flag
102 - a pointer to the data that has to be configured by the handling function
103 - a pointer to the handling function
104```
105const struct argoat_sprig sprigs[2] =
106{
107	{NULL, 0, NULL, handle_main},
108	{"t", 0, (void*) &data1, handle_bool}
109};
110```
111
112Then, create the main argoat structure given:
113 - the flags array
114 - its size,
115 - the unflagged string buffer
116 - the initial number of unflagged arguments
117 - the maximum possible
118```
119struct argoat args = {sprigs, 2, unflagged, 0, UNFLAGGED_MAX};
120```
121
122All that remains to do is calling the parsing function
123```
124argoat_graze(&args, argc, argv);
125```
126
127And using the configured data
128```
129printf("%c\n", data1 ? '1' : '0');
130```
131