annotate lwlib/lw_cmdline.c @ 533:543b5d1341d2

Add note to manual about numeric entry addresses in scripts Link scripts require a numeric entry address to begin with a digit. Make a note about that in the manual since hex addresses starting with letters could be mistaken for symbols.
author William Astle <lost@l-w.ca>
date Sat, 14 May 2022 12:57:50 -0600
parents b138b4005125
children
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
441
b138b4005125 Make missing command line arguments fail properly
William Astle <lost@l-w.ca>
parents: 374
diff changeset
22 #include <errno.h>
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
23 #include <stdio.h>
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
24 #include <stdlib.h>
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
25 #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
26 #include <ctype.h>
4
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 #include "lw_alloc.h"
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
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
31 #define DOCCOL 30
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
32 #define LLEN 78
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
33
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
34 static struct lw_cmdline_options builtin[3] =
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
35 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
36 { "help", '?', 0, 0, "give this help list" },
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
37 { "usage", 0, 0, 0, "give a short usage message" },
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
38 { "version", 'V', 0, 0, "print program version" }
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
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
41 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
42 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
43 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
44 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
45 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
46
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
47 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
48 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
49
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
50 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
51 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
52 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
53 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
54 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
55
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
56 if ((*o1) -> key < ((*o2) -> key))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
57 return -1;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
58 if ((*o1) -> key > ((*o2) -> key))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
59 return 1;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
60 return 0;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
61 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
62
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
63 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
64 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
65 struct lw_cmdline_options **slist, **llist;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
66 int nopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
67 int i;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
68 int t;
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
69 int col;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
70
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
71 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
72 /* do nothing */ ;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
73
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
74 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
75 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
76
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
77 for (i = 0; i < nopt; i++)
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 slist[i] = &(parser -> options[i]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
80 llist[i] = &(parser -> options[i]);
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
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
83 /* 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
84 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
85 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
86
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
87 /* now append the automatic options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
88 slist[nopt] = &(builtin[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
89 slist[nopt + 1] = &(builtin[1]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
90 slist[nopt + 2] = &(builtin[2]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
91
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
92 llist[nopt] = &(builtin[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
93 llist[nopt + 1] = &(builtin[1]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
94 llist[nopt + 2] = &(builtin[2]);
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 /* now show the usage message */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
97 printf("Usage: %s", name);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
98
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
99 col = 7 + strlen(name);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
100
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
101 /* print short options that take no args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
102 t = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
103 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
104 {
192
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
105 if (slist[i]->flags & lw_cmdline_opt_hidden)
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
106 continue;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
107 if (slist[i]->key > 0x20 && slist[i]->key < 0x7f)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
108 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
109 if (slist[i]->arg == NULL)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
110 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
111 if (!t)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
112 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
113 printf(" [-");
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
114 t = 1;
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
115 col += 3;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
116 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
117 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
118 col++;
4
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 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
122 if (t)
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
123 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
124 col++;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
125 printf("]");
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
126 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
127
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
128 /* print short options that take args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
129 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
130 {
192
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
131 if (slist[i]->flags & lw_cmdline_opt_hidden)
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
132 continue;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
133 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
134 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
135 if (slist[i]->flags & lw_cmdline_opt_optional)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
136 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
137 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
138 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
139 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
140 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
141 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
142 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
143 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
144 col += t;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
145 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
146 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
147 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
148 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
149 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
150 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
151 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
152 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
153 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
154 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
155 col += t;
4
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
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
160 /* print long options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
161 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
162 {
192
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
163 if (slist[i]->flags & lw_cmdline_opt_hidden)
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
164 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
165 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
166 continue;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
167 if (llist[i]->arg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
168 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
169 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
170 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
171 t += 2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
172 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
173 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
174 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
175 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
176 }
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
177 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
178 {
198
d2bed389e94a Fix --help and --usage to display = correctly for long option args
William Astle <lost@l-w.ca>
parents: 192
diff changeset
179 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
180 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
181 }
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 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
183 {
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 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
185 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
186 (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
187 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
188 (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
189 }
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
190 col += t;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
191 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
192 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
193 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
194 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
195 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
196 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
197 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
198 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
199 }
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
200 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
201 {
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 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
203 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
204 }
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 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
206 {
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 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
208 }
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
209 col += t;
4
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
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
213 /* print "non option" text */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
214 if (parser -> args_doc)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
215 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
216 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
217 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
218 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
219 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
220 printf(" %s", parser -> args_doc);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
221 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
222 printf("\n");
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 /* clean up scratch lists */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
225 lw_free(slist);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
226 lw_free(llist);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
227 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
228
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
229 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
230 {
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
231 struct lw_cmdline_options **llist;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
232 int nopt;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
233 int i;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
234 char *tstr;
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
235 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
236 int noequ;
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
237
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
238 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
239 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
240 /* do nothing */ ;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
241
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
242 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
243
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
244 for (i = 0; i < nopt; i++)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
245 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
246 llist[i] = &(parser -> options[i]);
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
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
249 /* now sort the list */
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
250 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
251
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
252 /* now append the automatic options */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
253 llist[nopt] = &(builtin[0]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
254 llist[nopt + 1] = &(builtin[1]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
255 llist[nopt + 2] = &(builtin[2]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
256
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
257 /* print brief usage */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
258 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
259 if (tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
260 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
261 while (*tstr && *tstr != '\v')
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
262 fputc(*tstr++, stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
263 if (*tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
264 tstr++;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
265 }
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 fputc('\n', stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
268
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
269 /* 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
270 for (i = 0; i < (nopt + 3); i++)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
271 {
192
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
272 if (llist[i]->flags & lw_cmdline_opt_hidden)
8ae2670377ca Added hidden options to lw_cmdline
lost@l-w.ca
parents: 191
diff changeset
273 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
274 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
275 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
276 {
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 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
278 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
279 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
280 }
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
281 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
282 {
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
283 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
284 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
285 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
286 {
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 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
288 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
289 }
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
290 fputc(' ', stdout);
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
291 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
292 else
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
293 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
294 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
295 col = 6;
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
296 }
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
297 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
298 {
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 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
300 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
301 }
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
302 if (llist[i] -> arg)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
303 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
304 if (llist[i] -> flags & lw_cmdline_opt_optional)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
305 {
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
306 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
307 fputc('[', stdout);
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
308 }
198
d2bed389e94a Fix --help and --usage to display = correctly for long option args
William Astle <lost@l-w.ca>
parents: 192
diff changeset
309 if (!noequ)
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
310 {
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
311 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
312 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
313 }
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 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
315 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
316 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
317 {
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 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
319 fputc(']', stdout);
5
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 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
322 if (llist[i] -> doc)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
323 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
324 char *s = llist[i] -> doc;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
325 char *s2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
326
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
327 while (*s && isspace(*s))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
328 s++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
329
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
330 if (col > DOCCOL)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
331 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
332 fputc('\n', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
333 col = 0;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
334 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
335 while (*s)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
336 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
337 while (col < (DOCCOL - 1))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
338 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
339 fputc(' ', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
340 col++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
341 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
342 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
343 /* do nothing */ ;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
344 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
345 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
346 /* next line */
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
347 fputc('\n', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
348 col = 0;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
349 continue;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
350 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
351 col++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
352 fputc(' ', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
353 while (s != s2)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
354 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
355 fputc(*s, stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
356 col++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
357 s++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
358 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
359 while (*s && isspace(*s))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
360 s++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
361 }
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
362 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
363 fputc('\n', stdout);
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
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
366 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
367
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
368 if (*tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
369 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
370 printf("\n%s\n", tstr);
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
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
373 /* clean up scratch lists */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
374 lw_free(llist);
4
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
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
377 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
378 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
379 int i, j, r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
380 int firstarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
381 int nextarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
382 char *tstr;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
383 int cch;
441
b138b4005125 Make missing command line arguments fail properly
William Astle <lost@l-w.ca>
parents: 374
diff changeset
384
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
385 /* 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
386 for (i = 1, firstarg = 1; i < argc; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
387 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
388 if (argv[i][0] == '-' && argv[i][1])
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
389 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
390 /* have an option arg */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
391 if (firstarg == i)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
392 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
393 firstarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
394 continue;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
395 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
396 tstr = argv[i];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
397 for (j = i; j > firstarg; j--)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
398 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
399 argv[j] = argv[j - 1];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
400 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
401 argv[firstarg] = tstr;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
402 firstarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
403 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
404 break;
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
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
408 /* now start parsing options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
409 nextarg = firstarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
410 i = 1;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
411 cch = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
412 while (i < firstarg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
413 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
414 if (cch > 0 && argv[i][cch] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
415 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
416 i++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
417 cch = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
418 continue;
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
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
421 if (cch > 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
422 goto shortopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
423
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
424 /* skip the "--" option */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
425 if (argv[i][1] == '-' && argv[i][2] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
426 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
427
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
428 if (argv[i][1] == '-')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
429 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
430 goto longopt;
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
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
433 cch = 1;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
434 shortopt:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
435 /* handle a short option here */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
436
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
437 /* automatic options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
438 if (argv[i][cch] == '?')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
439 goto do_help;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
440 if (argv[i][cch] == 'V')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
441 goto do_version;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
442 /* 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
443 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
444 if (parser -> options[j].key == argv[i][cch])
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
445 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
446 cch++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
447 tstr = argv[i] + cch;
105
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
448 if (*tstr == 0)
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
449 tstr = NULL;
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
450 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
451 {
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
452 /* 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
453 if (nextarg < argc)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
454 tstr = argv[nextarg];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
455 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
456 tstr = NULL;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
457 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
458 goto common;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
459
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
460 longopt:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
461 if (strcmp(argv[i], "--help") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
462 goto do_help;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
463 if (strcmp(argv[i], "--usage") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
464 goto do_usage;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
465 if (strcmp(argv[i], "--version") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
466 goto do_version;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
467 /* look up name */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
468
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
469 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
470 /* do nothing */ ;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
471 tstr = lw_alloc(j - 1);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
472 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
473 tstr[j - 2] = 0;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
474 if (argv[i][j] == '=')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
475 j++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
476 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
477 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
478 {
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
479 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
480 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
481 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
482 lw_free(tstr);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
483 tstr = argv[i] + cch;
105
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
484 if (*tstr == 0)
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
485 tstr = NULL;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
486 cch = 0;
37
a5d4693483af Fix hang while parsing long options
lost@l-w.ca
parents: 8
diff changeset
487 i++;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
488
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
489 common:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
490 /* 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
491 /* 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
492 /* 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
493 /* this string or "nextarg" */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
494 if (parser -> options[j].name == NULL)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
495 {
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
496 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
497 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
498 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
499 fprintf(stderr, "Unknown option '%s'. See %s --usage.\n", argv[i - 1], argv[0]);
441
b138b4005125 Make missing command line arguments fail properly
William Astle <lost@l-w.ca>
parents: 374
diff changeset
500 return EINVAL;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
501 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
502 if (parser -> options[j].arg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
503 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
504 if (tstr && cch && argv[i][cch] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
505 nextarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
506
105
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
507 //if (!*tstr)
3e9057059a43 Even more command line parser fixing
lost@l-w.ca
parents: 104
diff changeset
508 // tstr = NULL;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
509
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
510 /* 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
511 if (tstr && cch && argv[i][cch] != 0)
104
bc82df7f6bbe More command line args fixing
lost@l-w.ca
parents: 103
diff changeset
512 {
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
513 i++;
104
bc82df7f6bbe More command line args fixing
lost@l-w.ca
parents: 103
diff changeset
514 cch = 0;
bc82df7f6bbe More command line args fixing
lost@l-w.ca
parents: 103
diff changeset
515 }
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
516
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
517 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
518 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
519 fprintf(stderr, "Option %s requires argument.\n", parser -> options[j].name);
441
b138b4005125 Make missing command line arguments fail properly
William Astle <lost@l-w.ca>
parents: 374
diff changeset
520 return EINVAL;
4
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 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
523 r = (*(parser -> parser))(parser -> options[j].key, tstr, input);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
524 if (r != 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
525 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
526 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
527 /* handle non-option args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
528 if (arg_index)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
529 *arg_index = nextarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
530 for (i = nextarg; i < argc; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
531 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
532 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
533 if (r != 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
534 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
535 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
536 r = (*(parser -> parser))(lw_cmdline_key_end, NULL, input);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
537 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
538
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
539 do_help:
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
540 lw_cmdline_help(parser, argv[0]);
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
541 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
542
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
543 do_version:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
544 printf("%s\n", parser -> program_version);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
545 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
546
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
547 do_usage:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
548 lw_cmdline_usage(parser, argv[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
549 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
550 }