Mercurial > hg > th-libs
annotate th_args.c @ 128:c22caa6e3fcb
Cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 22 Jun 2014 07:06:46 +0300 |
parents | 37bf3d8766b7 |
children | 286b2249c5d2 |
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 |
128 | 4 * (C) Copyright 2002-2008 Tecnic Software productions (TNSP) |
0
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 |
128 | 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 { | |
128 | 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 | |
128 | 177 // Check if option argument is required |
39 | 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 |
128 | 184 // Option was given succesfully, try to handle it |
39 | 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 | |
128 | 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); | |
127
37bf3d8766b7
Fix long option parsing. I wonder how long that has been broken.
Matti Hamalainen <ccr@tnsp.org>
parents:
73
diff
changeset
|
229 if (strncmp(currArg, optList[i].optLong, optLen) == 0) |
39 | 230 optN = i; |
231 } | |
232 | |
128 | 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) |
127
37bf3d8766b7
Fix long option parsing. I wonder how long that has been broken.
Matti Hamalainen <ccr@tnsp.org>
parents:
73
diff
changeset
|
237 optArg = (currArg[optLen] == '=') ? &currArg[optLen + 1] : NULL; |
39 | 238 else |
10 | 239 optArg = NULL; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 |
128 | 241 // Check if option argument is required |
10 | 242 if (!th_args_check_arg(&optList[optN], optArg)) |
39 | 243 return FALSE; |
244 else | |
245 { | |
128 | 246 // Option was given succesfully, try to handle it |
10 | 247 wasGiven[optN] = TRUE; |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
248 if (!handleOpt(optList[optN].id, optArg, currArg)) |
10 | 249 return FALSE; |
250 } | |
39 | 251 } |
252 else | |
253 { | |
10 | 254 THERR("Unknown long option '--%s'\n", currArg); |
255 return FALSE; | |
256 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 |
10 | 258 return TRUE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 /* 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
|
263 * calling the given callback functions. |
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 BOOL th_args_process(int argc, char *argv[], |
39 | 266 optarg_t optList[], int optListN, |
267 BOOL(*handleOpt) (int, char *, char *), | |
268 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
|
269 { |
10 | 270 BOOL endOptions, optionsOK; |
271 int argIndex, newArgIndex, i; | |
272 char *currArg; | |
273 BOOL *wasGiven; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
274 |
128 | 275 // Allocate wasGiven |
10 | 276 wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL)); |
39 | 277 if (!wasGiven) |
278 { | |
50 | 279 THERR("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n"); |
10 | 280 exit(128); |
281 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
282 |
128 | 283 // Parse arguments |
10 | 284 argIndex = 1; |
285 optionsOK = TRUE; | |
286 endOptions = FALSE; | |
39 | 287 while (argIndex < argc) |
288 { | |
10 | 289 currArg = argv[argIndex]; |
39 | 290 if ((currArg[0] == '-') && !endOptions) |
291 { | |
10 | 292 newArgIndex = argIndex; |
293 currArg++; | |
39 | 294 if (*currArg == '-') |
295 { | |
128 | 296 // Check for "--", which ends the options-list |
10 | 297 currArg++; |
39 | 298 if (*currArg == 0) |
299 { | |
10 | 300 endOptions = TRUE; |
301 continue; | |
302 } | |
39 | 303 |
128 | 304 // Long options |
10 | 305 if (!th_args_process_long(currArg, &newArgIndex, |
39 | 306 wasGiven, argc, argv, optList, |
307 optListN, handleOpt)) | |
10 | 308 optionsOK = FALSE; |
39 | 309 } |
310 else | |
311 { | |
128 | 312 // Short options |
10 | 313 if (!th_args_process_short(currArg, &newArgIndex, |
39 | 314 wasGiven, argc, argv, optList, |
315 optListN, handleOpt)) | |
10 | 316 optionsOK = FALSE; |
317 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
318 |
10 | 319 argIndex = newArgIndex; |
39 | 320 } |
321 else | |
322 { | |
128 | 323 // Was not option argument |
39 | 324 if (handleNonOption == NULL |
325 || (handleNonOption != NULL && !handleNonOption(currArg))) | |
326 { | |
10 | 327 THERR("Invalid argument '%s'\n", currArg); |
328 optionsOK = FALSE; | |
329 } | |
330 } | |
39 | 331 |
128 | 332 // Check if we bail out on invalid argument |
39 | 333 if (!optionsOK && bailOut) |
334 { | |
10 | 335 th_free(wasGiven); |
336 return FALSE; | |
337 } | |
39 | 338 |
10 | 339 argIndex++; |
340 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
341 |
128 | 342 // Check wasGiven by isRequired |
10 | 343 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
|
344 if ((optList[i].flags & OPT_REQUIRED) != 0 && !wasGiven[i]) |
39 | 345 { |
346 THERR("Option -%s (--%s) is required.\n", | |
347 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
|
348 |
39 | 349 optionsOK = FALSE; |
350 if (bailOut) | |
351 break; | |
352 } | |
353 | |
10 | 354 th_free(wasGiven); |
355 return optionsOK; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
356 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
357 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
358 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
359 /* 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
|
360 */ |
50 | 361 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
|
362 { |
10 | 363 int i, nrequired; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
364 |
39 | 365 for (i = nrequired = 0; i < optListN; i++) |
366 { | |
10 | 367 optarg_t *o = &optList[i]; |
39 | 368 |
128 | 369 // Print short option |
10 | 370 if (o->optShort != 0) |
371 fprintf(outFile, " -%c, ", o->optShort); | |
372 else | |
373 fprintf(outFile, " "); | |
39 | 374 |
128 | 375 // Print long option |
39 | 376 if (o->optLong) |
377 { | |
10 | 378 char tmpStr[64], *p; |
39 | 379 |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
380 if ((o->flags & OPT_ARGMASK) == OPT_ARGOPT) |
39 | 381 { |
382 snprintf(tmpStr, sizeof(tmpStr), "%s[=ARG]", | |
383 optList[i].optLong); | |
10 | 384 p = tmpStr; |
39 | 385 } |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
386 else if ((o->flags & OPT_ARGMASK) == OPT_ARGREQ) |
39 | 387 { |
388 snprintf(tmpStr, sizeof(tmpStr), "%s=ARG", | |
389 optList[i].optLong); | |
10 | 390 p = tmpStr; |
39 | 391 } |
392 else | |
10 | 393 p = o->optLong; |
39 | 394 |
10 | 395 fprintf(outFile, "--%-15s", p); |
39 | 396 } |
397 else | |
10 | 398 fprintf(outFile, " "); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
399 |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
400 fprintf(outFile, " %s.", optList[i].desc); |
39 | 401 |
46
5f73c8cd333a
Change some structure member names in optarg_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
402 if (o->flags & OPT_REQUIRED) |
39 | 403 { |
10 | 404 fprintf(outFile, " [*]\n"); |
405 nrequired++; | |
39 | 406 } |
407 else | |
10 | 408 fprintf(outFile, "\n"); |
409 } | |
39 | 410 |
10 | 411 if (nrequired > 0) |
412 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
|
413 } |