Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 75 lines (60 sloc) 3.13 kB
dc5be1c Initial check-in.
uli authored
1 /* =============================================================================
2 PROJECT: ToolParams
3 FILE: ToolParams.h
4
5 COPYRIGHT: Copyright (c) 2001 M. Uli Kusterer. All rights reserved.
6
7 DIRECTIONS:
8 This simplifies parsing of parameters passed to a command line tool
9 somewhat. This understands two parameters: Those preceded by a switch,
10 which starts with a minus sign and continues with only one more
11 character (i.e. -f, or -i), and those that do not have a switch,
12 and are thus "unlabeled".
13
14 Unlabeled parameters are processed in the order they appear in the
15 parameter mapping table (see below), while switched parameters are
16 looked up by name, case-sensitively.
17
18 To have ToolParams automatically store your parameters in the proper
19 variables for you, simply create an array of ToolParamEntry structures,
20 one per parameter, and pass that and the argc and argv parameters
21 passed to your main() function to the ParseParamArray() function.
22 Unlabeled parameters must have mSwitch set to an empty string,
23 while labeled parameters must contain the entire label (i.e. "-i"
24 or "-F" or whatever).
25
26 Put a pointer to the variable that is to contain the char* from the
27 parameter array into mDest. The variable pointed to by this will be
28 set to NULL if the parameter was not found, or otherwise will point
29 to the string.
30
31 If a label is immediately followed by a second label, or if a label
32 is the last entry in the parameter list, its string is set to an empty
33 string.
34
35 EXAMPLE:
36 int main( int argc, const char* argv )
37 {
38 char* vFilename, *vOverwrite;
39 ToolParamEntry mEntries[3] = { { "", &vFilename },
40 { "-o", &vOverwrite },
41 { "", NULL } };
42 ParseParamArray(argc, argv, mEntries );
43 if( vOverwrite != NULL ) ; // overwite.
44 if( vFilename == NULL ) return EXIT_FAIL;
45 fopen( vFilename );
46 }
47
48 REVISIONS:
49 Wed Sep 05 2001 witness Created.
50 ========================================================================== */
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 /* -----------------------------------------------------------------------------
57 Data Structures:
58 -------------------------------------------------------------------------- */
59
60 typedef struct ToolParamEntry
61 {
62 const char* mSwitch; // Enter indicator, like "-i" here. Use empty string for unlabeled param.
63 const char** const mDest; // Pointer to variable to hold param, if found. Otherwise var pointed to by this is set to NULL.
64 } ToolParamEntry;
65
66
67 /* -----------------------------------------------------------------------------
68 Prototypes:
69 -------------------------------------------------------------------------- */
70
71 void ParseParamArray( int argc, const char* argv[], ToolParamEntry* pMap );
72
73 #ifdef __cplusplus
74 }
75 #endif
Something went wrong with that request. Please try again.