annotate lwlib/lw_cmdline.c @ 5:0e01d1343c02

Added basic --help routine to lw_cmdline
author lost@l-w.ca
date Fri, 21 Jan 2011 23:19:19 -0700
parents a4812d57ed13
children 1e5e8ec406fb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
1 /*
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
2 lw_cmdline.c
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
3
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
4 Copyright © 2010 William Astle
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
5
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
6 This file is part of LWTOOLS.
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
7
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
11 version.
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
12
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
16 more details.
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
17
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
20 */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
21
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
22 #include <stdio.h>
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
23 #include <stdlib.h>
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
24 #include <string.h>
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
25
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
26 #include "lw_alloc.h"
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
27
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
28 #define ___lw_cmdline_c_seen___
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
29 #include "lw_cmdline.h"
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
30
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
31 static struct lw_cmdline_options builtin[3] =
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
32 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
33 { "help", '?', 0, 0, "give this help list" },
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
34 { "usage", 0, 0, 0, "give a short usage message" },
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
35 { "version", 'V', 0, 0, "print program version" }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
36 };
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
37
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
38 static void lw_cmdline_usage(struct lw_cmdline_parser *parser, char *name)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
39 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
40 struct lw_cmdline_options **slist, **llist;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
41 int nopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
42 int i;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
43 int t;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
44
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
45 for (nopt = 0; parser -> options[nopt].name; nopt++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
46 /* do nothing */ ;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
47
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
48 slist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3));
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
49 llist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3));
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
50
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
51 for (i = 0; i < nopt; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
52 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
53 slist[i] = &(parser -> options[i]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
54 llist[i] = &(parser -> options[i]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
55 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
56
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
57 /* now sort the two lists */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
58
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
59 /* now append the automatic options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
60 slist[nopt] = &(builtin[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
61 slist[nopt + 1] = &(builtin[1]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
62 slist[nopt + 2] = &(builtin[2]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
63
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
64 llist[nopt] = &(builtin[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
65 llist[nopt + 1] = &(builtin[1]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
66 llist[nopt + 2] = &(builtin[2]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
67
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
68 /* now show the usage message */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
69 printf("Usage: %s", name);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
70
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
71 /* print short options that take no args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
72 t = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
73 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
74 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
75 if (slist[i]->key > 0x20 && slist[i]->key < 0x7f)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
76 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
77 if (slist[i]->arg == NULL)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
78 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
79 if (!t)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
80 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
81 printf(" [-");
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
82 t = 1;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
83 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
84 printf("%c", slist[i]->key);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
85 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
86 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
87 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
88 if (t)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
89 printf("]");
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
90
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
91 /* print short options that take args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
92 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
93 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
94 if (slist[i]->key > 0x20 && slist[i]->key < 0x7f && slist[i] -> arg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
95 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
96 if (slist[i]->flags & lw_cmdline_opt_optional)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
97 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
98 printf(" [-%c[%s]]", slist[i]->key, slist[i]->arg);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
99 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
100 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
101 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
102 printf(" [-%c %s]", slist[i]->key, slist[i]->arg);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
103 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
104 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
105 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
106
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
107 /* print long options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
108 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
109 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
110 if (llist[i]->arg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
111 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
112 printf(" [--%s=%s%s%s]",
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
113 llist[i] -> name,
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
114 (llist[i] -> flags & lw_cmdline_opt_optional) ? "[" : "",
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
115 llist[i] -> arg,
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
116 (llist[i] -> flags & lw_cmdline_opt_optional) ? "]" : "");
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
117 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
118 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
119 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
120 printf(" [--%s]", llist[i] -> name);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
121 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
122 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
123
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
124 /* print "non option" text */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
125 if (parser -> args_doc)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
126 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
127 printf(" %s", parser -> args_doc);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
128 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
129 printf("\n");
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
130
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
131 /* clean up scratch lists */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
132 lw_free(slist);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
133 lw_free(llist);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
134 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
135
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
136 static void lw_cmdline_help(struct lw_cmdline_parser *parser, char *name)
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
137 {
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
138 struct lw_cmdline_options **llist;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
139 int nopt;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
140 int i;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
141 int t;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
142 char *tstr;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
143
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
144 tstr = parser -> doc;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
145 for (nopt = 0; parser -> options[nopt].name; nopt++)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
146 /* do nothing */ ;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
147
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
148 llist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3));
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
149
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
150 for (i = 0; i < nopt; i++)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
151 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
152 llist[i] = &(parser -> options[i]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
153 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
154
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
155 /* now sort the list */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
156
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
157 /* now append the automatic options */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
158 llist[nopt] = &(builtin[0]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
159 llist[nopt + 1] = &(builtin[1]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
160 llist[nopt + 2] = &(builtin[2]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
161
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
162 /* print brief usage */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
163 printf("Usage: %s [OPTION...] %s\n", name, parser -> args_doc ? parser -> args_doc : "");
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
164 if (tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
165 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
166 while (*tstr && *tstr != '\v')
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
167 fputc(*tstr++, stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
168 if (*tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
169 tstr++;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
170 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
171 fputc('\n', stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
172 fputc('\n', stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
173
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
174 /* display options - do it the naïve way for now */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
175 for (i = 0; i < (nopt + 3); i++)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
176 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
177 if (llist[i] -> key > 0x20 && llist[i] -> key < 0x7F)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
178 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
179 printf(" -%c, ", llist[i] -> key);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
180 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
181 else
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
182 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
183 printf(" ");
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
184 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
185
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
186 printf("--%s", llist[i] -> name);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
187 if (llist[i] -> arg)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
188 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
189 if (llist[i] -> flags & lw_cmdline_opt_optional)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
190 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
191 printf("[=%s]", llist[i] -> arg);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
192 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
193 else
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
194 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
195 printf("=%s", llist[i] -> arg);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
196 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
197 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
198 if (llist[i] -> doc)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
199 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
200 printf("\t\t%s", llist[i] -> doc);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
201 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
202 fputc('\n', stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
203 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
204
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
205 printf("\nMandatory or optional arguments to long options are also mandatory or optional\nfor any corresponding short options.\n");
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
206
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
207 if (*tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
208 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
209 printf("\n%s\n", tstr);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
210 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
211
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
212 /* clean up scratch lists */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
213 lw_free(llist);
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
214 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
215
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
216 int lw_cmdline_parse(struct lw_cmdline_parser *parser, int argc, char **argv, unsigned flags, int *arg_index, void *input)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
217 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
218 int i, j, r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
219 int firstarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
220 int nextarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
221 char *tstr;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
222 int cch;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
223
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
224 /* first, permute the argv array so that all option arguments are at the start */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
225 for (i = 1, firstarg = 1; i < argc; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
226 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
227 if (argv[i][0] == '-' && argv[i][1])
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
228 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
229 /* have an option arg */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
230 if (firstarg == i)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
231 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
232 firstarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
233 continue;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
234 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
235 tstr = argv[i];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
236 for (j = i; j > firstarg; j--)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
237 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
238 argv[j] = argv[j - 1];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
239 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
240 argv[firstarg] = tstr;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
241 firstarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
242 if (argv[firstarg - 1][1] == '-' && argv[firstarg - 1][2] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
243 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
244 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
245 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
246
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
247 /* now start parsing options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
248 nextarg = firstarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
249 i = 1;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
250 cch = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
251 while (i < firstarg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
252 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
253 if (cch > 0 && argv[i][cch] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
254 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
255 i++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
256 cch = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
257 continue;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
258 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
259
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
260 if (cch > 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
261 goto shortopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
262
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
263 /* skip the "--" option */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
264 if (argv[i][1] == '-' && argv[i][2] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
265 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
266
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
267 if (argv[i][1] == '-')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
268 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
269 goto longopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
270 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
271
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
272 cch = 1;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
273 shortopt:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
274 /* handle a short option here */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
275
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
276 /* automatic options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
277 if (argv[i][cch] == '?')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
278 goto do_help;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
279 if (argv[i][cch] == 'V')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
280 goto do_version;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
281 /* look up key */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
282 for (j = 0; parser -> options[j].name; j++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
283 if (parser -> options[j].key == argv[i][cch])
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
284 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
285 cch++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
286 tstr = argv[i] + cch;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
287 if (!*tstr)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
288 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
289 if (nextarg < argc)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
290 tstr = argv[nextarg];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
291 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
292 tstr = NULL;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
293 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
294 goto common;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
295
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
296 longopt:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
297 if (strcmp(argv[i], "--help") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
298 goto do_help;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
299 if (strcmp(argv[i], "--usage") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
300 goto do_usage;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
301 if (strcmp(argv[i], "--version") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
302 goto do_version;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
303 /* look up name */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
304
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
305 for (j = 2; argv[i][j] && argv[i][j] != '='; j++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
306 /* do nothing */ ;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
307 tstr = lw_alloc(j - 1);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
308 strncpy(tstr, argv[i] + 2, j - 2);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
309 tstr[j - 1] = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
310 if (argv[i][j] == '=')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
311 j++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
312 cch = j;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
313
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
314 for (j = 0; parser -> options[j].name; j++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
315 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
316 if (strcmp(parser -> options[j].name, tstr) == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
317 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
318 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
319 lw_free(tstr);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
320 tstr = argv[i] + cch;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
321 cch = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
322
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
323 common:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
324 /* j will be the offset into the option table when we get here */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
325 /* cch will be zero and tstr will point to the arg if it's a long option */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
326 /* cch will be > 0 and tstr points to the theoretical option, either within */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
327 /* this string or "nextarg" */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
328 if (parser -> options[j].name == NULL)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
329 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
330 fprintf(stderr, "Unknown option. See %s --usage.\n", argv[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
331 exit(1);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
332 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
333 if (parser -> options[j].arg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
334 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
335 if (tstr && cch && argv[i][cch] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
336 nextarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
337
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
338 if (!*tstr)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
339 tstr = NULL;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
340
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
341 if (!tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
342 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
343 fprintf(stderr, "Option %s requires argument.\n", parser -> options[j].name);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
344 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
345 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
346 r = (*(parser -> parser))(parser -> options[j].key, tstr, input);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
347 if (r != 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
348 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
349 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
350 /* handle non-option args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
351 if (arg_index)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
352 *arg_index = nextarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
353 for (i = nextarg; i < argc; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
354 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
355 r = (*(parser -> parser))(lw_cmdline_key_arg, argv[i], input);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
356 if (r != 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
357 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
358 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
359 r = (*(parser -> parser))(lw_cmdline_key_end, NULL, input);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
360 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
361
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
362 do_help:
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
363 lw_cmdline_help(parser, argv[0]);
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
364 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
365
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
366 do_version:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
367 printf("%s\n", parser -> program_version);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
368 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
369
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
370 do_usage:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
371 lw_cmdline_usage(parser, argv[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
372 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
373 }