Mercurial > hg > th-libs
annotate th_args.c @ 50:1fa969d0f551
Sync changes.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 03 Oct 2011 18:39:31 +0300 |
parents | 598609fb49b0 |
children | 34d58b0f2d52 |
rev | line source |
---|---|
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Simple commandline argument processing |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * (C) Copyright 2002-2008 Tecnic Software productions (TNSP) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 /* |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 Description |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 =========== |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 Structures and functions for simple commandline argument processing, |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 option argument handling (short and long forms supported). |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 Output function for printing out short on-line help for said options |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 and program commandline usage. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 Format for typical commandline: |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 $ program -a -b -c --long-d -e argument-for-e --long-f="argument for f" |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 where -a, -b and -c are short options without required arguments; |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 --long-d is a long option without argument; -e is short option with |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 argument and finally --long-f is long option with argument. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 Introduction |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 ============ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 Handling of commandline options in th_args_process() has various |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 options, which effect how an option is handled. Also the error |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 situations (unknown option/no required argument) can be handled |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 in different ways. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 Typically th_args_process() is called as follows: |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 BOOL optionHandlerCallback(int optionNumber, char *optionArgument, char *optionName) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 { |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 if (option is OK) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 return TRUE; |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 else |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 return FALSE; |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 BOOL nonoptionHandlerCallback(char *argumentString) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 { |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 // return value same as in optionHandlerCallback |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 int main(int argc, char *argv[]) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 { |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 if (th_args_process(argc, argv, optList, optListN, |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 optionHandlerCallback, nonoptionHandlerCallback)) { |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 ... arguments OK ... |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 } else { |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 ... arguments invalid or required arguments missing ... |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 NOTICE! |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 ------- |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 The return value from handler callbacks affects the return value of |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 th_args_process(). Additionally, a failure in callback (returns FALSE) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 effects the argument processing if bailOut argument of th_args_process() |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 is TRUE! |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 If bailOut is TRUE, any error/failure in argument processing (including |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 callbacks) immediately stops the argument processing and FALSE is |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 returned from th_args_process(). |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 If bailOut is FALSE, most errors are "ignored", but FALSE is still returned |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 if any errors occured. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 NOTICE #2! |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 ---------- |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 A small temporary buffer of N*sizeof(BOOL) (where N is number of |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 options in optList[]) is allocated for processing required options. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 If this allocation fails, the program is immediately exited with |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 code 128. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 Examples |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 ======== |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 Example of different options, in a fictional optionlist struct: |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 optarg_t optList[] = { |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 // Option without arguments |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 { 0, '?', "help", "Show this help", OPT_NONE }, |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 // Option with a required argument |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 { 1, 'o', "output", "Output file name", OPT_ARGREQ }, |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 // Option with optional argument |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 { 2, 'f', "foobar", "Use foobar, with optional string", OPT_ARGOPT }, |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 // This option is required to be given, though without other flags |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 // it may not make much sense. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 { 4, 'S', "stupid", "You must give this option", OPT_REQUIRED }, |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 // The flags can be combined with OR operator: this option is both |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 // required to be specified, and also requires argument (the filename) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 { 5, 'i', "input", "Input file name", OPT_REQUIRED | OPT_ARGREQ }, |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 // Option with only long form |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
108 { 0, 0, "only-long", "Long option", OPT_NONE }, |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 // Option with only short form |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 { 0, 's', NULL, "Short option", OPT_NONE }, |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 }; |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 |
2 | 115 const int optListN = (sizeof(optList) / sizeof(optarg_t)); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 */ |
50 | 119 #ifndef TH_EXTERNAL |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 #include "th_util.h" |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 #include "th_args.h" |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 #include "th_string.h" |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
47
diff
changeset
|
123 #endif |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 /* Check if option requires an argument |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 */ |
47 | 128 static BOOL th_args_check_arg(const optarg_t *o, const char *optArg) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 { |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
130 if ((o->flags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL) |
39 | 131 { |
132 if (o->optShort != 0 && o->optLong != NULL) | |
133 { | |
10 | 134 THERR("Option '-%c ARG' (--%s=ARG) requires an argument!\n", |
39 | 135 o->optShort, o->optLong); |
136 } | |
137 else if (o->optShort != 0) | |
138 { | |
10 | 139 THERR("Option '-%c ARG' requires an argument!\n", o->optShort); |
39 | 140 } |
141 else if (o->optLong != NULL) | |
142 { | |
10 | 143 THERR("Option --%s=ARG requires an argument!\n", o->optLong); |
144 } | |
39 | 145 |
10 | 146 return FALSE; |
39 | 147 } |
148 else | |
10 | 149 return TRUE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 /* Handle short options |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 static BOOL th_args_process_short(char *currArg, int *newArgIndex, |
10 | 156 BOOL *wasGiven, int argc, char *argv[], |
157 optarg_t optList[], int optListN, | |
158 BOOL (*handleOpt)(int, char *, char *)) | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 { |
10 | 160 char *tmpArg = currArg, *optArg; |
161 int optN; | |
162 BOOL isFound; | |
39 | 163 |
10 | 164 /* Short options can be combined: -a -b -c == -abc */ |
39 | 165 while (*tmpArg) |
166 { | |
10 | 167 for (optN = 0, isFound = FALSE; (optN < optListN) && !isFound; optN++) |
39 | 168 if (*tmpArg == optList[optN].optShort) |
169 { | |
170 /* Get possible option argument, if needed */ | |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
171 if ((optList[optN].flags & OPT_ARGMASK) != 0 |
39 | 172 && (++(*newArgIndex) < argc)) |
173 optArg = argv[*newArgIndex]; | |
174 else | |
175 optArg = NULL; | |
176 | |
177 /* Check if option argument is required */ | |
178 if (!th_args_check_arg(&optList[optN], optArg)) | |
179 return FALSE; | |
180 else | |
181 { | |
182 char tmpStr[2] = { 0, 0 }; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 |
39 | 184 /* Option was given succesfully, try to handle it */ |
185 wasGiven[optN] = TRUE; | |
186 | |
187 tmpStr[0] = *tmpArg; | |
188 | |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
189 if (!handleOpt(optList[optN].id, optArg, tmpStr)) |
39 | 190 return FALSE; |
191 } | |
192 | |
193 isFound = TRUE; | |
10 | 194 } |
39 | 195 |
196 if (!isFound) | |
197 { | |
10 | 198 THERR("Unknown short option '%c' in argument '-%s'\n", |
39 | 199 *tmpArg, currArg); |
10 | 200 return FALSE; |
201 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 |
10 | 203 tmpArg++; |
204 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 |
10 | 206 return TRUE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
209 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
210 /* Handle long options |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
212 static BOOL th_args_process_long(char *currArg, int *newArgIndex, |
10 | 213 BOOL *wasGiven, int argc, char *argv[], |
214 optarg_t optList[], int optListN, | |
215 BOOL (*handleOpt)(int, char *, char *)) | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 { |
10 | 217 int optN, optLen, i; |
218 char *optArg; | |
39 | 219 |
220 (void) argc; | |
221 (void) argv; | |
222 (void) newArgIndex; | |
223 | |
10 | 224 /* Long option */ |
225 for (optN = -1, optLen = i = 0; (i < optListN) && (optN < 0); i++) | |
39 | 226 if (optList[i].optLong) |
227 { | |
228 optLen = strlen(optList[i].optLong); | |
229 if (strncmp(currArg, optList[i].optLong, optLen) == 0) | |
230 optN = i; | |
231 } | |
232 | |
10 | 233 /* Get possible option argument, if needed */ |
39 | 234 if (optN >= 0) |
235 { | |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
236 if ((optList[optN].flags & OPT_ARGMASK) != 0) |
39 | 237 { |
10 | 238 if (currArg[optLen] == '=') |
239 optArg = &currArg[optLen + 1]; | |
240 else | |
241 optArg = NULL; | |
39 | 242 } |
243 else | |
10 | 244 optArg = NULL; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 |
10 | 246 /* Check if option argument is required */ |
247 if (!th_args_check_arg(&optList[optN], optArg)) | |
39 | 248 return FALSE; |
249 else | |
250 { | |
10 | 251 /* Option was given succesfully, try to handle it */ |
252 wasGiven[optN] = TRUE; | |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
253 if (!handleOpt(optList[optN].id, optArg, currArg)) |
10 | 254 return FALSE; |
255 } | |
39 | 256 } |
257 else | |
258 { | |
10 | 259 THERR("Unknown long option '--%s'\n", currArg); |
260 return FALSE; | |
261 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 |
10 | 263 return TRUE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
266 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
267 /* Process arguments, handling short and long options by |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
268 * calling the given callback functions. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
270 BOOL th_args_process(int argc, char *argv[], |
39 | 271 optarg_t optList[], int optListN, |
272 BOOL(*handleOpt) (int, char *, char *), | |
273 BOOL(*handleNonOption) (char *), BOOL bailOut) | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
274 { |
10 | 275 BOOL endOptions, optionsOK; |
276 int argIndex, newArgIndex, i; | |
277 char *currArg; | |
278 BOOL *wasGiven; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
279 |
10 | 280 /* Allocate wasGiven */ |
281 wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL)); | |
39 | 282 if (!wasGiven) |
283 { | |
50 | 284 THERR("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n"); |
10 | 285 exit(128); |
286 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
287 |
10 | 288 /* Parse arguments */ |
289 argIndex = 1; | |
290 optionsOK = TRUE; | |
291 endOptions = FALSE; | |
39 | 292 while (argIndex < argc) |
293 { | |
10 | 294 currArg = argv[argIndex]; |
39 | 295 if ((currArg[0] == '-') && !endOptions) |
296 { | |
10 | 297 newArgIndex = argIndex; |
298 currArg++; | |
39 | 299 if (*currArg == '-') |
300 { | |
10 | 301 /* Check for "--", which ends the options-list */ |
302 currArg++; | |
39 | 303 if (*currArg == 0) |
304 { | |
10 | 305 endOptions = TRUE; |
306 continue; | |
307 } | |
39 | 308 |
10 | 309 /* Long options */ |
310 if (!th_args_process_long(currArg, &newArgIndex, | |
39 | 311 wasGiven, argc, argv, optList, |
312 optListN, handleOpt)) | |
10 | 313 optionsOK = FALSE; |
39 | 314 } |
315 else | |
316 { | |
10 | 317 /* Short options */ |
318 if (!th_args_process_short(currArg, &newArgIndex, | |
39 | 319 wasGiven, argc, argv, optList, |
320 optListN, handleOpt)) | |
10 | 321 optionsOK = FALSE; |
322 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
323 |
10 | 324 argIndex = newArgIndex; |
39 | 325 } |
326 else | |
327 { | |
10 | 328 /* Was not option argument */ |
39 | 329 if (handleNonOption == NULL |
330 || (handleNonOption != NULL && !handleNonOption(currArg))) | |
331 { | |
10 | 332 THERR("Invalid argument '%s'\n", currArg); |
333 optionsOK = FALSE; | |
334 } | |
335 } | |
39 | 336 |
10 | 337 /* Check if we bail out on invalid argument */ |
39 | 338 if (!optionsOK && bailOut) |
339 { | |
10 | 340 th_free(wasGiven); |
341 return FALSE; | |
342 } | |
39 | 343 |
10 | 344 argIndex++; |
345 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
346 |
10 | 347 /* Check wasGiven by isRequired */ |
348 for (i = 0; i < optListN; i++) | |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
349 if ((optList[i].flags & OPT_REQUIRED) != 0 && !wasGiven[i]) |
39 | 350 { |
351 THERR("Option -%s (--%s) is required.\n", | |
352 optList[i].optShort, optList[i].optLong); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
353 |
39 | 354 optionsOK = FALSE; |
355 if (bailOut) | |
356 break; | |
357 } | |
358 | |
10 | 359 th_free(wasGiven); |
360 return optionsOK; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
361 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
362 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
363 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
364 /* Print help for commandline arguments/options |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
365 */ |
50 | 366 void th_args_help(FILE *outFile, optarg_t optList[], int optListN) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
367 { |
10 | 368 int i, nrequired; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
369 |
39 | 370 for (i = nrequired = 0; i < optListN; i++) |
371 { | |
10 | 372 optarg_t *o = &optList[i]; |
39 | 373 |
10 | 374 /* Print short option */ |
375 if (o->optShort != 0) | |
376 fprintf(outFile, " -%c, ", o->optShort); | |
377 else | |
378 fprintf(outFile, " "); | |
39 | 379 |
10 | 380 /* Print long option */ |
39 | 381 if (o->optLong) |
382 { | |
10 | 383 char tmpStr[64], *p; |
39 | 384 |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
385 if ((o->flags & OPT_ARGMASK) == OPT_ARGOPT) |
39 | 386 { |
387 snprintf(tmpStr, sizeof(tmpStr), "%s[=ARG]", | |
388 optList[i].optLong); | |
10 | 389 p = tmpStr; |
39 | 390 } |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
391 else if ((o->flags & OPT_ARGMASK) == OPT_ARGREQ) |
39 | 392 { |
393 snprintf(tmpStr, sizeof(tmpStr), "%s=ARG", | |
394 optList[i].optLong); | |
10 | 395 p = tmpStr; |
39 | 396 } |
397 else | |
10 | 398 p = o->optLong; |
39 | 399 |
10 | 400 fprintf(outFile, "--%-15s", p); |
39 | 401 } |
402 else | |
10 | 403 fprintf(outFile, " "); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
404 |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
405 fprintf(outFile, " %s.", optList[i].desc); |
39 | 406 |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
407 if (o->flags & OPT_REQUIRED) |
39 | 408 { |
10 | 409 fprintf(outFile, " [*]\n"); |
410 nrequired++; | |
39 | 411 } |
412 else | |
10 | 413 fprintf(outFile, "\n"); |
414 } | |
39 | 415 |
10 | 416 if (nrequired > 0) |
417 fprintf(outFile, "(Options marked with [*] are required)\n"); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
418 } |