annotate lwlib/lw_cmdline.c @ 374:8e25147c2aa8

Clean up various "externs" Apparently some compilers seriously choke on the "extern" keyword in some circumstances where GCC does not. Remove most instances of "extern" to guard against that. Thanks to Erik G <erik@6809.org> for the patch.
author William Astle <lost@l-w.ca>
date Mon, 13 Jul 2015 20:31:56 -0600
parents 806e5fc6dd93
children b138b4005125
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>
8
fdc11ef4115b Switched lwlink to lw_cmdline from argp and also brought in lw_alloc and lw_string to replace util.c
lost@l-w.ca
parents: 6
diff changeset
25 #include <ctype.h>
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
26
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
27 #include "lw_alloc.h"
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
28 #include "lw_cmdline.h"
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
29
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
30 #define DOCCOL 30
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
31 #define LLEN 78
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
32
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
33 static struct lw_cmdline_options builtin[3] =
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
34 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
35 { "help", '?', 0, 0, "give this help list" },
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
36 { "usage", 0, 0, 0, "give a short usage message" },
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
37 { "version", 'V', 0, 0, "print program version" }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
38 };
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
39
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
40 static int cmpr(const void *e1, const void *e2)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
41 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
42 struct lw_cmdline_options **o1, **o2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
43 o1 = (struct lw_cmdline_options **)e1;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
44 o2 = (struct lw_cmdline_options **)e2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
45
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
46 return strcmp((*o1) -> name ? (*o1) -> name : "", (*o2) -> name ? (*o2) -> name : "");
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
47 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
48
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
49 static int cmpr2(const void *e1, const void *e2)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
50 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
51 struct lw_cmdline_options **o1, **o2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
52 o1 = (struct lw_cmdline_options **)e1;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
53 o2 = (struct lw_cmdline_options **)e2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
54
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
55 if ((*o1) -> key < ((*o2) -> key))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
56 return -1;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
57 if ((*o1) -> key > ((*o2) -> key))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
58 return 1;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
59 return 0;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
60 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
61
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
62 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
63 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
64 struct lw_cmdline_options **slist, **llist;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
65 int nopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
66 int i;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
67 int t;
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
68 int col;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
69
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
70 for (nopt = 0; parser -> options[nopt].name || parser -> options[nopt].key || parser -> options[nopt].doc; nopt++)
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
71 /* do nothing */ ;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
72
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
73 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
74 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
75
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
76 for (i = 0; i < nopt; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
77 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
78 slist[i] = &(parser -> options[i]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
79 llist[i] = &(parser -> options[i]);
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
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
82 /* now sort the two lists */
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
83 qsort(slist, nopt, sizeof(struct lw_cmdline_options *), cmpr2);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
84 qsort(llist, nopt, sizeof(struct lw_cmdline_options *), cmpr);
4
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 /* now append the automatic options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
87 slist[nopt] = &(builtin[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
88 slist[nopt + 1] = &(builtin[1]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
89 slist[nopt + 2] = &(builtin[2]);
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 llist[nopt] = &(builtin[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
92 llist[nopt + 1] = &(builtin[1]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
93 llist[nopt + 2] = &(builtin[2]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
94
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
95 /* now show the usage message */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
96 printf("Usage: %s", name);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
97
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
98 col = 7 + strlen(name);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
99
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
100 /* print short options that take no args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
101 t = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
102 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
103 {
192
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
104 if (slist[i]->flags & lw_cmdline_opt_hidden)
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
105 continue;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
106 if (slist[i]->key > 0x20 && slist[i]->key < 0x7f)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
107 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
108 if (slist[i]->arg == NULL)
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 (!t)
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(" [-");
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
113 t = 1;
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
114 col += 3;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
115 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
116 printf("%c", slist[i]->key);
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
117 col++;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
118 }
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 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
121 if (t)
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
122 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
123 col++;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
124 printf("]");
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
125 }
4
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 /* print short options that take args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
128 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
129 {
192
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
130 if (slist[i]->flags & lw_cmdline_opt_hidden)
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
131 continue;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
132 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
133 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
134 if (slist[i]->flags & lw_cmdline_opt_optional)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
135 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
136 t = 7 + strlen(slist[i] -> arg);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
137 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
138 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
139 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
140 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
141 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
142 printf(" [-%c[%s]]", slist[i]->key, slist[i]->arg);
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
143 col += t;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
144 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
145 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
146 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
147 t = 6 + strlen(slist[i] -> arg);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
148 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
149 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
150 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
151 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
152 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
153 printf(" [-%c %s]", slist[i]->key, slist[i]->arg);
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
154 col += t;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
155 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
156 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
157 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
158
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
159 /* print long options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
160 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
161 {
192
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
162 if (slist[i]->flags & lw_cmdline_opt_hidden)
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
163 continue;
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
164 if (!(llist[i]->name))
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
165 continue;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
166 if (llist[i]->arg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
167 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
168 t = strlen(llist[i] -> name) + 6 + strlen(llist[i] -> arg);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
169 if (llist[i] -> flags & lw_cmdline_opt_optional)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
170 t += 2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
171 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
172 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
173 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
174 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
175 }
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
176 if (llist[i] -> flags & lw_cmdline_opt_doc)
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
177 {
198
d2bed389e94a Fix --help and --usage to display = correctly for long option args
William Astle <lost@l-w.ca>
parents: 192
diff changeset
178 printf(" [%s=%s]", llist[i] -> name, llist[i] -> arg);
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
179 t = strlen(llist[i] -> name) + strlen(llist[i] -> arg) + 3;
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
180 }
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
181 else
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
182 {
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
183 printf(" [--%s%s=%s%s]",
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
184 llist[i] -> name,
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
185 (llist[i] -> flags & lw_cmdline_opt_optional) ? "[" : "",
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
186 llist[i] -> arg,
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
187 (llist[i] -> flags & lw_cmdline_opt_optional) ? "]" : "");
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
188 }
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
189 col += t;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
190 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
191 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
192 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
193 t = strlen(llist[i] -> name) + 5;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
194 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
195 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
196 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
197 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
198 }
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
199 if (llist[i] -> flags & lw_cmdline_opt_doc)
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
200 {
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
201 t -= 2;
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
202 printf(" [%s]", llist[i] -> name);
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
203 }
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
204 else
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
205 {
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
206 printf(" [--%s]", llist[i] -> name);
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
207 }
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
208 col += t;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
209 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
210 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
211
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
212 /* print "non option" text */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
213 if (parser -> args_doc)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
214 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
215 if (col + strlen(parser -> args_doc) + 1 > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
216 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
217 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
218 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
219 printf(" %s", parser -> args_doc);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
220 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
221 printf("\n");
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
222
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
223 /* clean up scratch lists */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
224 lw_free(slist);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
225 lw_free(llist);
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
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
228 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
229 {
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
230 struct lw_cmdline_options **llist;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
231 int nopt;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
232 int i;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
233 char *tstr;
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
234 int col = 0;
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
235 int noequ;
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
236
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
237 tstr = parser -> doc;
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
238 for (nopt = 0; parser -> options[nopt].name || parser -> options[nopt].key || parser -> options[nopt].doc; nopt++)
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
239 /* do nothing */ ;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
240
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
241 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
242
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
243 for (i = 0; i < nopt; i++)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
244 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
245 llist[i] = &(parser -> options[i]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
246 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
247
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
248 /* now sort the list */
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
249 qsort(llist, nopt, sizeof(struct lw_cmdline_options *), cmpr);
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
250
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
251 /* now append the automatic options */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
252 llist[nopt] = &(builtin[0]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
253 llist[nopt + 1] = &(builtin[1]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
254 llist[nopt + 2] = &(builtin[2]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
255
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
256 /* print brief usage */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
257 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
258 if (tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
259 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
260 while (*tstr && *tstr != '\v')
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
261 fputc(*tstr++, stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
262 if (*tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
263 tstr++;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
264 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
265 fputc('\n', stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
266 fputc('\n', stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
267
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
268 /* 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
269 for (i = 0; i < (nopt + 3); i++)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
270 {
192
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
271 if (llist[i]->flags & lw_cmdline_opt_hidden)
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
272 continue;
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
273 noequ = 0;
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
274 if (llist[i] -> flags & lw_cmdline_opt_doc)
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
275 {
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
276 col = strlen(llist[i] -> name) + 2;
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
277 printf(" %s", llist[i] -> name);
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
278 noequ = 1;
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
279 }
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
280 else if (llist[i] -> key > 0x20 && llist[i] -> key < 0x7F)
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
281 {
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
282 printf(" -%c", llist[i] -> key);
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
283 col = 5;
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
284 if (llist[i] -> name)
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
285 {
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
286 col++;
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
287 fputc(',', stdout);
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
288 }
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
289 fputc(' ', stdout);
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
290 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
291 else
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
292 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
293 printf(" ");
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
294 col = 6;
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
295 }
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
296 if (llist[i] -> name && !(llist[i] -> flags & lw_cmdline_opt_doc))
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
297 {
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
298 col += 2 + strlen(llist[i] -> name);
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
299 printf("--%s", llist[i] -> name);
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
300 }
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
301 if (llist[i] -> arg)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
302 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
303 if (llist[i] -> flags & lw_cmdline_opt_optional)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
304 {
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
305 col++;
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
306 fputc('[', stdout);
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
307 }
198
d2bed389e94a Fix --help and --usage to display = correctly for long option args
William Astle <lost@l-w.ca>
parents: 192
diff changeset
308 if (!noequ)
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
309 {
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
310 fputc('=', stdout);
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
311 col++;
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
312 }
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
313 printf("%s", llist[i] -> arg);
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
314 col += strlen(llist[i] -> arg);
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
315 if (llist[i] -> flags & lw_cmdline_opt_optional)
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
316 {
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
317 col++;
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
318 fputc(']', stdout);
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
319 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
320 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
321 if (llist[i] -> doc)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
322 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
323 char *s = llist[i] -> doc;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
324 char *s2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
325
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
326 while (*s && isspace(*s))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
327 s++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
328
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
329 if (col > DOCCOL)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
330 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
331 fputc('\n', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
332 col = 0;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
333 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
334 while (*s)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
335 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
336 while (col < (DOCCOL - 1))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
337 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
338 fputc(' ', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
339 col++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
340 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
341 for (s2 = s; *s2 && !isspace(*s2); s2++)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
342 /* do nothing */ ;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
343 if ((col + (s2 - s) + 1) > LLEN && col >= DOCCOL)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
344 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
345 /* next line */
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
346 fputc('\n', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
347 col = 0;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
348 continue;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
349 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
350 col++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
351 fputc(' ', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
352 while (s != s2)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
353 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
354 fputc(*s, stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
355 col++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
356 s++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
357 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
358 while (*s && isspace(*s))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
359 s++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
360 }
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
361 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
362 fputc('\n', stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
363 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
364
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
365 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
366
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
367 if (*tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
368 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
369 printf("\n%s\n", tstr);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
370 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
371
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
372 /* clean up scratch lists */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
373 lw_free(llist);
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
374 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
375
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
376 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
377 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
378 int i, j, r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
379 int firstarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
380 int nextarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
381 char *tstr;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
382 int cch;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
383
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
384 /* 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
385 for (i = 1, firstarg = 1; i < argc; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
386 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
387 if (argv[i][0] == '-' && argv[i][1])
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
388 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
389 /* have an option arg */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
390 if (firstarg == i)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
391 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
392 firstarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
393 continue;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
394 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
395 tstr = argv[i];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
396 for (j = i; j > firstarg; j--)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
397 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
398 argv[j] = argv[j - 1];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
399 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
400 argv[firstarg] = tstr;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
401 firstarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
402 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
403 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
404 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
405 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
406
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
407 /* now start parsing options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
408 nextarg = firstarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
409 i = 1;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
410 cch = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
411 while (i < firstarg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
412 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
413 if (cch > 0 && argv[i][cch] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
414 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
415 i++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
416 cch = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
417 continue;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
418 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
419
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
420 if (cch > 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
421 goto shortopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
422
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
423 /* skip the "--" option */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
424 if (argv[i][1] == '-' && argv[i][2] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
425 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
426
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
427 if (argv[i][1] == '-')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
428 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
429 goto longopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
430 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
431
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
432 cch = 1;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
433 shortopt:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
434 /* handle a short option here */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
435
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
436 /* automatic options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
437 if (argv[i][cch] == '?')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
438 goto do_help;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
439 if (argv[i][cch] == 'V')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
440 goto do_version;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
441 /* look up key */
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
442 for (j = 0; parser -> options[j].name || parser -> options[j].key || parser -> options[j].doc; j++)
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
443 if (parser -> options[j].key == argv[i][cch])
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
444 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
445 cch++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
446 tstr = argv[i] + cch;
105
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
447 if (*tstr == 0)
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
448 tstr = NULL;
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
449 if (!tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0)
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
450 {
103
8b0be0fc42cf Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
lost@l-w.ca
parents: 54
diff changeset
451 /* only consume the next arg if the argument is optional */
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
452 if (nextarg < argc)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
453 tstr = argv[nextarg];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
454 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
455 tstr = NULL;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
456 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
457 goto common;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
458
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
459 longopt:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
460 if (strcmp(argv[i], "--help") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
461 goto do_help;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
462 if (strcmp(argv[i], "--usage") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
463 goto do_usage;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
464 if (strcmp(argv[i], "--version") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
465 goto do_version;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
466 /* look up name */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
467
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
468 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
469 /* do nothing */ ;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
470 tstr = lw_alloc(j - 1);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
471 strncpy(tstr, argv[i] + 2, j - 2);
52
51c840679a0e Fixed off by one bug parsing long options
lost@l-w.ca
parents: 37
diff changeset
472 tstr[j - 2] = 0;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
473 if (argv[i][j] == '=')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
474 j++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
475 cch = j;
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
476 for (j = 0; parser -> options[j].name || parser -> options[j].key || parser -> options[j].doc; j++)
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
477 {
191
ddffceb3c331 Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
lost@l-w.ca
parents: 105
diff changeset
478 if (parser -> options[j].name && strcmp(parser -> options[j].name, tstr) == 0)
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
479 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
480 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
481 lw_free(tstr);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
482 tstr = argv[i] + cch;
105
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
483 if (*tstr == 0)
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
484 tstr = NULL;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
485 cch = 0;
37
a5d4693483af Fix hang while parsing long options
lost@l-w.ca
parents: 8
diff changeset
486 i++;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
487
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
488 common:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
489 /* 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
490 /* 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
491 /* 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
492 /* this string or "nextarg" */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
493 if (parser -> options[j].name == NULL)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
494 {
103
8b0be0fc42cf Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
lost@l-w.ca
parents: 54
diff changeset
495 if (cch)
8b0be0fc42cf Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
lost@l-w.ca
parents: 54
diff changeset
496 fprintf(stderr, "Unknown option '%c'. See %s --usage.\n", argv[i][cch - 1], argv[0]);
8b0be0fc42cf Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
lost@l-w.ca
parents: 54
diff changeset
497 else
8b0be0fc42cf Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
lost@l-w.ca
parents: 54
diff changeset
498 fprintf(stderr, "Unknown option '%s'. See %s --usage.\n", argv[i - 1], argv[0]);
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
499 exit(1);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
500 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
501 if (parser -> options[j].arg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
502 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
503 if (tstr && cch && argv[i][cch] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
504 nextarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
505
105
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
506 //if (!*tstr)
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
507 // tstr = NULL;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
508
103
8b0be0fc42cf Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
lost@l-w.ca
parents: 54
diff changeset
509 /* move on to next argument if we have an arg specified */
8b0be0fc42cf Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
lost@l-w.ca
parents: 54
diff changeset
510 if (tstr && cch && argv[i][cch] != 0)
104
bc82df7f6bbe More command line args fixing
lost@l-w.ca
parents: 103
diff changeset
511 {
103
8b0be0fc42cf Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
lost@l-w.ca
parents: 54
diff changeset
512 i++;
104
bc82df7f6bbe More command line args fixing
lost@l-w.ca
parents: 103
diff changeset
513 cch = 0;
bc82df7f6bbe More command line args fixing
lost@l-w.ca
parents: 103
diff changeset
514 }
103
8b0be0fc42cf Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
lost@l-w.ca
parents: 54
diff changeset
515
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
516 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
517 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
518 fprintf(stderr, "Option %s requires argument.\n", parser -> options[j].name);
205
806e5fc6dd93 Fix segfault during command parsing
William Astle <lost@l-w.ca>
parents: 198
diff changeset
519 continue;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
520 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
521 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
522 r = (*(parser -> parser))(parser -> options[j].key, tstr, input);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
523 if (r != 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
524 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
525 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
526 /* handle non-option args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
527 if (arg_index)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
528 *arg_index = nextarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
529 for (i = nextarg; i < argc; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
530 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
531 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
532 if (r != 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
533 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
534 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
535 r = (*(parser -> parser))(lw_cmdline_key_end, NULL, input);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
536 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
537
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
538 do_help:
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
539 lw_cmdline_help(parser, argv[0]);
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
540 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
541
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
542 do_version:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
543 printf("%s\n", parser -> program_version);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
544 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
545
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
546 do_usage:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
547 lw_cmdline_usage(parser, argv[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
548 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
549 }