annotate lwlib/lw_cmdline.c @ 318:bcd1daabdb2e

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