c_project 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +18 -0
- data/bin/c_project +77 -0
- data/c_project.gemspec +25 -0
- data/lib/c_project/version.rb +3 -0
- data/lib/c_project.rb +5 -0
- data/templates/LICENCE.tt +23 -0
- data/templates/Makefile.tt +74 -0
- data/templates/README.md.tt +15 -0
- data/templates/src/CExceptionConfig.h.tt +8 -0
- data/templates/src/c_project.c.tt +16 -0
- data/templates/src/c_project.h.tt +8 -0
- data/templates/src/main.c.tt +15 -0
- data/templates/test/support/test_helper.c.tt +2 -0
- data/templates/test/support/test_helper.h.tt +13 -0
- data/templates/test/test_c_project.c.tt +41 -0
- data/templates/vendor/cexception/docs/license.txt +30 -0
- data/templates/vendor/cexception/docs/readme.txt +242 -0
- data/templates/vendor/cexception/lib/CException.c +43 -0
- data/templates/vendor/cexception/lib/CException.h +86 -0
- data/templates/vendor/cexception/release/build.info +2 -0
- data/templates/vendor/cexception/release/version.info +2 -0
- data/templates/vendor/commander.c/History.md +27 -0
- data/templates/vendor/commander.c/Makefile +8 -0
- data/templates/vendor/commander.c/Readme.md +103 -0
- data/templates/vendor/commander.c/package.json +9 -0
- data/templates/vendor/commander.c/src/commander.c +250 -0
- data/templates/vendor/commander.c/src/commander.h +88 -0
- data/templates/vendor/commander.c/test.c +34 -0
- data/templates/vendor/unity/.gitignore +1 -0
- data/templates/vendor/unity/auto/colour_prompt.rb +94 -0
- data/templates/vendor/unity/auto/colour_reporter.rb +39 -0
- data/templates/vendor/unity/auto/generate_config.yml +36 -0
- data/templates/vendor/unity/auto/generate_module.rb +202 -0
- data/templates/vendor/unity/auto/generate_test_runner.rb +316 -0
- data/templates/vendor/unity/auto/test_file_filter.rb +23 -0
- data/templates/vendor/unity/auto/unity_test_summary.rb +139 -0
- data/templates/vendor/unity/docs/Unity Summary.txt +216 -0
- data/templates/vendor/unity/docs/license.txt +31 -0
- data/templates/vendor/unity/release/build.info +2 -0
- data/templates/vendor/unity/release/version.info +2 -0
- data/templates/vendor/unity/src/unity.c +1146 -0
- data/templates/vendor/unity/src/unity.h +245 -0
- data/templates/vendor/unity/src/unity_internals.h +546 -0
- metadata +135 -0
@@ -0,0 +1,242 @@
|
|
1
|
+
====================================================================
|
2
|
+
CException
|
3
|
+
====================================================================
|
4
|
+
|
5
|
+
CException is a basic exception framework for C, suitable for use in
|
6
|
+
embedded applications. It provides an exception framework similar in
|
7
|
+
use to C++, but with much less overhead.
|
8
|
+
|
9
|
+
CException uses C standard library functions setjmp and longjmp to
|
10
|
+
operate. As long as the target system has these two functions defined,
|
11
|
+
this library should be useable with very little configuration. It
|
12
|
+
even supports environments where multiple program flows are in use,
|
13
|
+
such as real-time operating systems.
|
14
|
+
|
15
|
+
There are about a gabillion exception frameworks using a similar
|
16
|
+
setjmp/longjmp method out there... and there will probably be more
|
17
|
+
in the future. Unfortunately, when we started our last embedded
|
18
|
+
project, all those that existed either (a) did not support multiple
|
19
|
+
tasks (therefore multiple stacks) or (b) were way more complex than
|
20
|
+
we really wanted. CException was born.
|
21
|
+
|
22
|
+
Why use CException?
|
23
|
+
|
24
|
+
0. It's ANSI C, and it beats passing error codes around.
|
25
|
+
|
26
|
+
1. You want something simple... CException throws a single id. You can
|
27
|
+
define those ID's to be whatever you like. You might even choose which
|
28
|
+
type that number is for your project. But that's as far as it goes.
|
29
|
+
We weren't interested in passing objects or structs or strings...
|
30
|
+
just simple error codes.
|
31
|
+
|
32
|
+
2. Performance... CException can be configured for single tasking or
|
33
|
+
multitasking. In single tasking, there is very little overhead past
|
34
|
+
the setjmp/longjmp calls (which are already fast). In multitasking,
|
35
|
+
your only additional overhead is the time it takes you to determine
|
36
|
+
a unique task id 0 - num_tasks.
|
37
|
+
|
38
|
+
For the latest version, go to http://cexception.sourceforge.net
|
39
|
+
|
40
|
+
--------------------------------------------------------------------
|
41
|
+
CONTENTS OF THIS DOCUMENT
|
42
|
+
--------------------------------------------------------------------
|
43
|
+
|
44
|
+
Usage
|
45
|
+
Limitations
|
46
|
+
API
|
47
|
+
Configuration
|
48
|
+
Testing
|
49
|
+
License
|
50
|
+
|
51
|
+
--------------------------------------------------------------------
|
52
|
+
Usage
|
53
|
+
--------------------------------------------------------------------
|
54
|
+
|
55
|
+
Code that is to be protected are wrapped in Try { } Catch { } blocks.
|
56
|
+
The code directly following the Try call is "protected", meaning that
|
57
|
+
if any Throws occur, program control is directly transferred to the
|
58
|
+
start of the Catch block.
|
59
|
+
|
60
|
+
A numerical exception ID is included with Throw, and is made accessible
|
61
|
+
from the Catch block.
|
62
|
+
|
63
|
+
Throws can occur from within function calls (nested as deeply as you
|
64
|
+
like) or directly from within the function itself.
|
65
|
+
|
66
|
+
--------------------------------------------------------------------
|
67
|
+
Limitations
|
68
|
+
--------------------------------------------------------------------
|
69
|
+
|
70
|
+
This library was made to be as fast as possible, and provide basic
|
71
|
+
exception handling. It is not a full-blown exception library. Because
|
72
|
+
of this, there are a few limitations that should be observed in order
|
73
|
+
to successfully utilize this library:
|
74
|
+
|
75
|
+
1. Do not directly "return" from within a Try block, nor "goto"
|
76
|
+
into or out of a Try block.
|
77
|
+
|
78
|
+
Why?
|
79
|
+
|
80
|
+
The "Try" macro allocates some local memory and alters a global
|
81
|
+
pointer. These are cleaned up at the top of the "Catch" macro.
|
82
|
+
Gotos and returns would bypass some of these steps, resulting in
|
83
|
+
memory leaks or unpredictable behavior.
|
84
|
+
|
85
|
+
2. If (a) you change local (stack) variables within your Try block,
|
86
|
+
AND (b) wish to make use of the updated values after an exception
|
87
|
+
is thrown, those variables should be made volatile. Note that this
|
88
|
+
is ONLY for locals and ONLY when you need access to them after a
|
89
|
+
throw.
|
90
|
+
|
91
|
+
Why?
|
92
|
+
|
93
|
+
Compilers optimize. There is no way to guarantee that the actual
|
94
|
+
memory location was updated and not just a register unless the
|
95
|
+
variable is marked volatile.
|
96
|
+
|
97
|
+
3. Memory which is malloc'd or new'd is not automatically released
|
98
|
+
when an error is thrown. This will sometimes be desirable, and
|
99
|
+
othertimes may not. It will be the responsibility of the Catch
|
100
|
+
block to perform this kind of cleanup.
|
101
|
+
|
102
|
+
Why?
|
103
|
+
|
104
|
+
There's just no easy way to track malloc'd memory, etc., without
|
105
|
+
replacing or wrapping malloc calls or something like that. This
|
106
|
+
is a light framework, so these options were not desirable.
|
107
|
+
|
108
|
+
--------------------------------------------------------------------
|
109
|
+
API
|
110
|
+
--------------------------------------------------------------------
|
111
|
+
|
112
|
+
Try
|
113
|
+
---
|
114
|
+
|
115
|
+
Try is a macro which starts a protected block. It MUST be followed by
|
116
|
+
a pair of braces or a single protected line (similar to an 'if'),
|
117
|
+
enclosing the data that is to be protected. It MUST be followed by a
|
118
|
+
Catch block (don't worry, you'll get compiler errors to let you know if
|
119
|
+
you mess any of that up).
|
120
|
+
|
121
|
+
Catch(e)
|
122
|
+
--------
|
123
|
+
|
124
|
+
Catch is a macro which ends the Try block and starts the error handling
|
125
|
+
block. The catch block is called if and only if an exception was thrown
|
126
|
+
while within the Try block. This error was thrown by a Throw call
|
127
|
+
somewhere within Try (or within a function called within Try, or a function
|
128
|
+
called by a function called within Try, etc).
|
129
|
+
|
130
|
+
The single parameter 'e' is filled with the error code which was thrown.
|
131
|
+
This can be used for reporting, conditional cleanup, etc. (or you can just
|
132
|
+
ignore it if you really want... people ignore return codes all the time,
|
133
|
+
right?). 'e' should be of type EXCEPTION_T;
|
134
|
+
|
135
|
+
Throw(e)
|
136
|
+
--------
|
137
|
+
|
138
|
+
The method of throwing an error. Throws should only occur from within a
|
139
|
+
protected (Try...Catch) block, though it may easily be nested many function
|
140
|
+
calls deep without an impact on performance or functionality. Throw takes
|
141
|
+
a single argument, which is an exception id which will be passed to Catch
|
142
|
+
as the reason for the error.
|
143
|
+
|
144
|
+
If you wish to Rethrow an error, this can be done by calling Throw(e) with
|
145
|
+
the error code you just caught. It IS valid to throw from a catch block.
|
146
|
+
|
147
|
+
--------------------------------------------------------------------
|
148
|
+
CONFIGURATION
|
149
|
+
--------------------------------------------------------------------
|
150
|
+
|
151
|
+
CException is a mostly portable library. It has one universal
|
152
|
+
dependency, and some macros which are required if working in a
|
153
|
+
multi-tasking environment.
|
154
|
+
|
155
|
+
1. The standard C library setjmp must be available. Since this is part
|
156
|
+
of the standard library, chances are good that you'll be fine.
|
157
|
+
|
158
|
+
2. If working in a multitasking environment, methods for obtaining an
|
159
|
+
index into an array of frames and to get the overall number of
|
160
|
+
id's are required. If the OS supports a method to retrieve Task
|
161
|
+
ID's, and those Tasks are number 0, 1, 2... you are in an ideal
|
162
|
+
situation. Otherwise, a more creative mapping function may be
|
163
|
+
required. Note that this function is likely to be called twice
|
164
|
+
for each protected block and once during a throw. This is the
|
165
|
+
only overhead in the system.
|
166
|
+
|
167
|
+
Exception.h
|
168
|
+
-----------------
|
169
|
+
By convention, most projects include Exception.h which defines any
|
170
|
+
further requirements, then calls CException.h to do the gruntwork. All
|
171
|
+
of these are optional. You could directly include CException.h if
|
172
|
+
you wanted and just use the defaults provided.
|
173
|
+
|
174
|
+
EXCEPTION_T - Set this to the type you want your exception id's
|
175
|
+
to be. Defaults to 'unsigned int'.
|
176
|
+
|
177
|
+
EXCEPTION_NONE - Set this to a number which will never be an
|
178
|
+
exception id in your system. Defaults to 0x5a5a5a5a.
|
179
|
+
|
180
|
+
EXCEPTION_GET_ID - If in a multi-tasking environment, this should be
|
181
|
+
set to be a call to the function described in #2 above.
|
182
|
+
Defaults to just return 0 all the time (good for
|
183
|
+
single tasking environments)
|
184
|
+
|
185
|
+
EXCEPTION_NUM_ID - If in a multi-tasking environment, this should be set
|
186
|
+
to the number of ID's required (usually the number of
|
187
|
+
tasks in the system). Defaults to 1 (for single
|
188
|
+
tasking environments).
|
189
|
+
|
190
|
+
CEXCEPTION_NO_CATCH_HANDLER(id) - This macro can be optionally specified.
|
191
|
+
It allows you to specify code to be called when a Throw
|
192
|
+
is made outside of Try...Catch protection. Consider
|
193
|
+
this the emergency fallback plan for when something has
|
194
|
+
gone terribly wrong.
|
195
|
+
|
196
|
+
You may also want to include any header files which will commonly be
|
197
|
+
needed by the rest of your application where it uses exception handling
|
198
|
+
here. For example, OS header files or exception codes would be useful.
|
199
|
+
|
200
|
+
--------------------------------------------------------------------
|
201
|
+
TESTING
|
202
|
+
--------------------------------------------------------------------
|
203
|
+
|
204
|
+
If you want to validate that CException works with your tools or that
|
205
|
+
it works with your custom configuration, you may want to run the test
|
206
|
+
suite.
|
207
|
+
|
208
|
+
The test suite included makes use of the Unity Test Framework. It will
|
209
|
+
require a native C compiler. The example makefile uses MinGW's gcc.
|
210
|
+
Modify the makefile to include the proper paths to tools, then run 'make'
|
211
|
+
to compile and run the test application.
|
212
|
+
|
213
|
+
C_COMPILER - The C compiler to use to perform the tests
|
214
|
+
C_LIBS - The path to the C libraries (including setjmp)
|
215
|
+
UNITY_DIR - The path to the Unity framework (required to run tests)
|
216
|
+
(get it at http://unity.sourceforge.net)
|
217
|
+
|
218
|
+
--------------------------------------------------------------------
|
219
|
+
LICENSE
|
220
|
+
--------------------------------------------------------------------
|
221
|
+
|
222
|
+
This software is licensed under the MIT License
|
223
|
+
|
224
|
+
Copyright (c) 2007-2012 Mark VanderVoord
|
225
|
+
|
226
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
227
|
+
of this software and associated documentation files (the "Software"), to deal
|
228
|
+
in the Software without restriction, including without limitation the rights
|
229
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
230
|
+
copies of the Software, and to permit persons to whom the Software is
|
231
|
+
furnished to do so, subject to the following conditions:
|
232
|
+
|
233
|
+
The above copyright notice and this permission notice shall be included in
|
234
|
+
all copies or substantial portions of the Software.
|
235
|
+
|
236
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
237
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
238
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
239
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
240
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
241
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
242
|
+
THE SOFTWARE.
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#include "CException.h"
|
2
|
+
|
3
|
+
volatile CEXCEPTION_FRAME_T CExceptionFrames[CEXCEPTION_NUM_ID] = { 0 };
|
4
|
+
|
5
|
+
//------------------------------------------------------------------------------------------
|
6
|
+
// Throw
|
7
|
+
//------------------------------------------------------------------------------------------
|
8
|
+
void Throw(CEXCEPTION_T ExceptionID)
|
9
|
+
{
|
10
|
+
unsigned int MY_ID = CEXCEPTION_GET_ID;
|
11
|
+
CExceptionFrames[MY_ID].Exception = ExceptionID;
|
12
|
+
if (CExceptionFrames[MY_ID].pFrame)
|
13
|
+
{
|
14
|
+
longjmp(*CExceptionFrames[MY_ID].pFrame, 1);
|
15
|
+
}
|
16
|
+
CEXCEPTION_NO_CATCH_HANDLER(MY_ID);
|
17
|
+
}
|
18
|
+
|
19
|
+
//------------------------------------------------------------------------------------------
|
20
|
+
// Explanation of what it's all for:
|
21
|
+
//------------------------------------------------------------------------------------------
|
22
|
+
/*
|
23
|
+
#define Try
|
24
|
+
{ <- give us some local scope. most compilers are happy with this
|
25
|
+
jmp_buf *PrevFrame, NewFrame; <- prev frame points to the last try block's frame. new frame gets created on stack for this Try block
|
26
|
+
unsigned int MY_ID = CEXCEPTION_GET_ID; <- look up this task's id for use in frame array. always 0 if single-tasking
|
27
|
+
PrevFrame = CExceptionFrames[CEXCEPTION_GET_ID].pFrame; <- set pointer to point at old frame (which array is currently pointing at)
|
28
|
+
CExceptionFrames[MY_ID].pFrame = &NewFrame; <- set array to point at my new frame instead, now
|
29
|
+
CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; <- initialize my exception id to be NONE
|
30
|
+
if (setjmp(NewFrame) == 0) { <- do setjmp. it returns 1 if longjump called, otherwise 0
|
31
|
+
if (&PrevFrame) <- this is here to force proper scoping. it requires braces or a single line to be but after Try, otherwise won't compile. This is always true at this point.
|
32
|
+
|
33
|
+
#define Catch(e)
|
34
|
+
else { } <- this also forces proper scoping. Without this they could stick their own 'else' in and it would get ugly
|
35
|
+
CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; <- no errors happened, so just set the exception id to NONE (in case it was corrupted)
|
36
|
+
}
|
37
|
+
else <- an exception occurred
|
38
|
+
{ e = CExceptionFrames[MY_ID].Exception; e=e;} <- assign the caught exception id to the variable passed in.
|
39
|
+
CExceptionFrames[MY_ID].pFrame = PrevFrame; <- make the pointer in the array point at the previous frame again, as if NewFrame never existed.
|
40
|
+
} <- finish off that local scope we created to have our own variables
|
41
|
+
if (CExceptionFrames[CEXCEPTION_GET_ID].Exception != CEXCEPTION_NONE) <- start the actual 'catch' processing if we have an exception id saved away
|
42
|
+
*/
|
43
|
+
|
@@ -0,0 +1,86 @@
|
|
1
|
+
#ifndef _CEXCEPTION_H
|
2
|
+
#define _CEXCEPTION_H
|
3
|
+
|
4
|
+
#include <setjmp.h>
|
5
|
+
|
6
|
+
#ifdef __cplusplus
|
7
|
+
extern "C"
|
8
|
+
{
|
9
|
+
#endif
|
10
|
+
|
11
|
+
|
12
|
+
//To Use CException, you have a number of options:
|
13
|
+
//1. Just include it and run with the defaults
|
14
|
+
//2. Define any of the following symbols at the command line to override them
|
15
|
+
//3. Include a header file before CException.h everywhere which defines any of these
|
16
|
+
//4. Create an Exception.h in your path, and just define EXCEPTION_USE_CONFIG_FILE first
|
17
|
+
|
18
|
+
#ifdef CEXCEPTION_USE_CONFIG_FILE
|
19
|
+
#include "CExceptionConfig.h"
|
20
|
+
#endif
|
21
|
+
|
22
|
+
//This is the value to assign when there isn't an exception
|
23
|
+
#ifndef CEXCEPTION_NONE
|
24
|
+
#define CEXCEPTION_NONE (0x5A5A5A5A)
|
25
|
+
#endif
|
26
|
+
|
27
|
+
//This is number of exception stacks to keep track of (one per task)
|
28
|
+
#ifndef CEXCEPTION_NUM_ID
|
29
|
+
#define CEXCEPTION_NUM_ID (1) //there is only the one stack by default
|
30
|
+
#endif
|
31
|
+
|
32
|
+
//This is the method of getting the current exception stack index (0 if only one stack)
|
33
|
+
#ifndef CEXCEPTION_GET_ID
|
34
|
+
#define CEXCEPTION_GET_ID (0) //use the first index always because there is only one anyway
|
35
|
+
#endif
|
36
|
+
|
37
|
+
//The type to use to store the exception values.
|
38
|
+
#ifndef CEXCEPTION_T
|
39
|
+
#define CEXCEPTION_T unsigned int
|
40
|
+
#endif
|
41
|
+
|
42
|
+
//This is an optional special handler for when there is no global Catch
|
43
|
+
#ifndef CEXCEPTION_NO_CATCH_HANDLER
|
44
|
+
#define CEXCEPTION_NO_CATCH_HANDLER(id)
|
45
|
+
#endif
|
46
|
+
|
47
|
+
//exception frame structures
|
48
|
+
typedef struct {
|
49
|
+
jmp_buf* pFrame;
|
50
|
+
CEXCEPTION_T volatile Exception;
|
51
|
+
} CEXCEPTION_FRAME_T;
|
52
|
+
|
53
|
+
//actual root frame storage (only one if single-tasking)
|
54
|
+
extern volatile CEXCEPTION_FRAME_T CExceptionFrames[];
|
55
|
+
|
56
|
+
//Try (see C file for explanation)
|
57
|
+
#define Try \
|
58
|
+
{ \
|
59
|
+
jmp_buf *PrevFrame, NewFrame; \
|
60
|
+
unsigned int MY_ID = CEXCEPTION_GET_ID; \
|
61
|
+
PrevFrame = CExceptionFrames[CEXCEPTION_GET_ID].pFrame; \
|
62
|
+
CExceptionFrames[MY_ID].pFrame = (jmp_buf*)(&NewFrame); \
|
63
|
+
CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; \
|
64
|
+
if (setjmp(NewFrame) == 0) { \
|
65
|
+
if (1)
|
66
|
+
|
67
|
+
//Catch (see C file for explanation)
|
68
|
+
#define Catch(e) \
|
69
|
+
else { } \
|
70
|
+
CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; \
|
71
|
+
} \
|
72
|
+
else \
|
73
|
+
{ e = CExceptionFrames[MY_ID].Exception; e=e; } \
|
74
|
+
CExceptionFrames[MY_ID].pFrame = PrevFrame; \
|
75
|
+
} \
|
76
|
+
if (CExceptionFrames[CEXCEPTION_GET_ID].Exception != CEXCEPTION_NONE)
|
77
|
+
|
78
|
+
//Throw an Error
|
79
|
+
void Throw(CEXCEPTION_T ExceptionID);
|
80
|
+
|
81
|
+
#ifdef __cplusplus
|
82
|
+
} // extern "C"
|
83
|
+
#endif
|
84
|
+
|
85
|
+
|
86
|
+
#endif // _CEXCEPTION_H
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
1.2.1 / 2013-01-08
|
3
|
+
==================
|
4
|
+
|
5
|
+
* add freeing of normalized argv
|
6
|
+
* dont worry about zeroing in command_free()
|
7
|
+
* fix docs
|
8
|
+
|
9
|
+
1.2.0 / 2013-01-08
|
10
|
+
==================
|
11
|
+
|
12
|
+
* add command_free()
|
13
|
+
|
14
|
+
1.1.0 / 2012-12-18
|
15
|
+
==================
|
16
|
+
|
17
|
+
* add short flag expansion
|
18
|
+
|
19
|
+
1.0.0 / 2012-12-13
|
20
|
+
==================
|
21
|
+
|
22
|
+
* fix malloc(), add a byte for nul
|
23
|
+
|
24
|
+
0.0.1 / 2012-06-16
|
25
|
+
==================
|
26
|
+
|
27
|
+
* Initial release
|
@@ -0,0 +1,103 @@
|
|
1
|
+
|
2
|
+
# commander.c
|
3
|
+
|
4
|
+
Commander option parser ported to C.
|
5
|
+
|
6
|
+
## Automated --help
|
7
|
+
|
8
|
+
The previous example would produce the following `--help`:
|
9
|
+
|
10
|
+
```
|
11
|
+
|
12
|
+
Usage: example [options]
|
13
|
+
|
14
|
+
Options:
|
15
|
+
|
16
|
+
-V, --version output program version
|
17
|
+
-h, --help output help information
|
18
|
+
-v, --verbose enable verbose stuff
|
19
|
+
-r, --required <arg> required arg
|
20
|
+
-o, --optional [arg] optional arg
|
21
|
+
|
22
|
+
```
|
23
|
+
|
24
|
+
## Example
|
25
|
+
|
26
|
+
```c
|
27
|
+
#include <stdio.h>
|
28
|
+
#include "commander.h"
|
29
|
+
|
30
|
+
static void
|
31
|
+
verbose(command_t *self) {
|
32
|
+
printf("verbose: enabled\n");
|
33
|
+
}
|
34
|
+
|
35
|
+
static void
|
36
|
+
required(command_t *self) {
|
37
|
+
printf("required: %s\n", self->arg);
|
38
|
+
}
|
39
|
+
|
40
|
+
static void
|
41
|
+
optional(command_t *self) {
|
42
|
+
printf("optional: %s\n", self->arg);
|
43
|
+
}
|
44
|
+
|
45
|
+
int
|
46
|
+
main(int argc, const char **argv){
|
47
|
+
command_t cmd;
|
48
|
+
command_init(&cmd, argv[0], "0.0.1");
|
49
|
+
command_option(&cmd, "-v", "--verbose", "enable verbose stuff", verbose);
|
50
|
+
command_option(&cmd, "-r", "--required <arg>", "required arg", required);
|
51
|
+
command_option(&cmd, "-o", "--optional [arg]", "optional arg", optional);
|
52
|
+
command_parse(&cmd, argc, argv);
|
53
|
+
printf("additional args:\n");
|
54
|
+
for (int i = 0; i < cmd.argc; ++i) {
|
55
|
+
printf(" - '%s'\n", cmd.argv[i]);
|
56
|
+
}
|
57
|
+
command_free(&cmd);
|
58
|
+
return 0;
|
59
|
+
}
|
60
|
+
```
|
61
|
+
|
62
|
+
## Closure
|
63
|
+
|
64
|
+
`cmd.data` is a `void *` so pass along a struct to the callbacks if you want.
|
65
|
+
|
66
|
+
## Usage
|
67
|
+
|
68
|
+
`cmd.usage` defaults to "[options]".
|
69
|
+
|
70
|
+
## Links
|
71
|
+
|
72
|
+
- Used by the [mon(1)](https://github.com/visionmedia/mon/blob/master/src/mon.c) process monitor
|
73
|
+
|
74
|
+
## Short flags
|
75
|
+
|
76
|
+
Compound short flags are automatically expanded to their canonical form. For example `-vLO` would
|
77
|
+
become `-v -L -O`.
|
78
|
+
|
79
|
+
## License
|
80
|
+
|
81
|
+
(The MIT License)
|
82
|
+
|
83
|
+
Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
|
84
|
+
|
85
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
86
|
+
a copy of this software and associated documentation files (the
|
87
|
+
'Software'), to deal in the Software without restriction, including
|
88
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
89
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
90
|
+
permit persons to whom the Software is furnished to do so, subject to
|
91
|
+
the following conditions:
|
92
|
+
|
93
|
+
The above copyright notice and this permission notice shall be
|
94
|
+
included in all copies or substantial portions of the Software.
|
95
|
+
|
96
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
97
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
98
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
99
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
100
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
101
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
102
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
103
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
{
|
2
|
+
"name": "commander",
|
3
|
+
"version": "1.2.1",
|
4
|
+
"repo": "visionmedia/commander.c",
|
5
|
+
"description": "Command-line argument parser",
|
6
|
+
"keywords": ["cli", "command", "parser", "argv", "args", "options"],
|
7
|
+
"license": "MIT",
|
8
|
+
"src": ["src/commander.h", "src/commander.c"]
|
9
|
+
}
|