Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 93 lines (80 sloc) 3.785 kb
720b0e7 Uli Kusterer Added licensing information to the source files.
authored
1 //
2 // ToolParams.h
3 // ToolParams
4 //
5 // Created by Uli Kusterer on Wed Sep 05 2001.
6 // Copyright 2001 Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
dc5be1c Initial check-in.
uli authored
27
720b0e7 Uli Kusterer Added licensing information to the source files.
authored
28 /*
dc5be1c Initial check-in.
uli authored
29 This simplifies parsing of parameters passed to a command line tool
30 somewhat. This understands two parameters: Those preceded by a switch,
31 which starts with a minus sign and continues with only one more
32 character (i.e. -f, or -i), and those that do not have a switch,
33 and are thus "unlabeled".
34
35 Unlabeled parameters are processed in the order they appear in the
36 parameter mapping table (see below), while switched parameters are
37 looked up by name, case-sensitively.
38
39 To have ToolParams automatically store your parameters in the proper
40 variables for you, simply create an array of ToolParamEntry structures,
41 one per parameter, and pass that and the argc and argv parameters
42 passed to your main() function to the ParseParamArray() function.
43 Unlabeled parameters must have mSwitch set to an empty string,
44 while labeled parameters must contain the entire label (i.e. "-i"
45 or "-F" or whatever).
46
47 Put a pointer to the variable that is to contain the char* from the
48 parameter array into mDest. The variable pointed to by this will be
49 set to NULL if the parameter was not found, or otherwise will point
50 to the string.
51
52 If a label is immediately followed by a second label, or if a label
53 is the last entry in the parameter list, its string is set to an empty
54 string.
55
56 EXAMPLE:
57 int main( int argc, const char* argv )
58 {
59 char* vFilename, *vOverwrite;
60 ToolParamEntry mEntries[3] = { { "", &vFilename },
61 { "-o", &vOverwrite },
62 { "", NULL } };
63 ParseParamArray(argc, argv, mEntries );
64 if( vOverwrite != NULL ) ; // overwite.
65 if( vFilename == NULL ) return EXIT_FAIL;
66 fopen( vFilename );
67 }
720b0e7 Uli Kusterer Added licensing information to the source files.
authored
68 */
dc5be1c Initial check-in.
uli authored
69
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73
74 /* -----------------------------------------------------------------------------
75 Data Structures:
76 -------------------------------------------------------------------------- */
77
78 typedef struct ToolParamEntry
79 {
80 const char* mSwitch; // Enter indicator, like "-i" here. Use empty string for unlabeled param.
81 const char** const mDest; // Pointer to variable to hold param, if found. Otherwise var pointed to by this is set to NULL.
82 } ToolParamEntry;
83
84
85 /* -----------------------------------------------------------------------------
86 Prototypes:
87 -------------------------------------------------------------------------- */
88
89 void ParseParamArray( int argc, const char* argv[], ToolParamEntry* pMap );
90
91 #ifdef __cplusplus
92 }
93 #endif
Something went wrong with that request. Please try again.