make arg.name imply that an argument is required
This commit is contained in:
parent
42219c391d
commit
cd5acee3bd
18
readarg.h
18
readarg.h
|
@ -45,9 +45,6 @@ struct readarg_opt
|
|||
{
|
||||
/* Two null-terminated arrays of either long or short option names. */
|
||||
char **names[2];
|
||||
|
||||
int req;
|
||||
|
||||
struct readarg_arg arg;
|
||||
};
|
||||
|
||||
|
@ -249,7 +246,7 @@ static void readarg_parse_opt(struct readarg_parser *rp, enum readarg_form form,
|
|||
{
|
||||
const char *strpos = *pos;
|
||||
|
||||
if (!match->req && *strpos)
|
||||
if (!match->arg.name && *strpos)
|
||||
{
|
||||
rp->state.grppos = strpos;
|
||||
readarg_update_opt(rp, NULL, match);
|
||||
|
@ -348,7 +345,7 @@ static struct readarg_opt *readarg_match_finish(struct readarg_parser *rp, const
|
|||
|
||||
static void readarg_update_opt(struct readarg_parser *rp, const char *attach, struct readarg_opt *opt)
|
||||
{
|
||||
if (opt->req)
|
||||
if (opt->arg.name)
|
||||
{
|
||||
if (attach)
|
||||
{
|
||||
|
@ -630,17 +627,10 @@ int readarg_helpgen_put_usage(struct readarg_parser *rp, struct readarg_helpgen_
|
|||
{
|
||||
READARG_HELPGEN_TRY_LIT(writer, ", ");
|
||||
}
|
||||
else if (opts[i].req)
|
||||
else if (opts[i].arg.name)
|
||||
{
|
||||
READARG_HELPGEN_TRY_LIT(writer, " ");
|
||||
if (opts[i].arg.name)
|
||||
{
|
||||
READARG_HELPGEN_TRY_STR(writer, opts[i].arg.name);
|
||||
}
|
||||
else
|
||||
{
|
||||
READARG_HELPGEN_TRY_LIT(writer, "value");
|
||||
}
|
||||
READARG_HELPGEN_TRY_STR(writer, opts[i].arg.name);
|
||||
|
||||
if (inf)
|
||||
{
|
||||
|
|
29
test/test.c
29
test/test.c
|
@ -38,40 +38,45 @@ int main(int argc, char **argv)
|
|||
[READARG_FORM_SHORT] = READARG_STRINGS("e", "x"),
|
||||
[READARG_FORM_LONG] = READARG_STRINGS("expr", "expression"),
|
||||
},
|
||||
.req = 1,
|
||||
.arg.bounds.val = {
|
||||
1,
|
||||
4,
|
||||
},
|
||||
.arg = {
|
||||
.name = "expression",
|
||||
.bounds.val = {
|
||||
1,
|
||||
4,
|
||||
},
|
||||
}
|
||||
},
|
||||
{
|
||||
.names = {
|
||||
[READARG_FORM_SHORT] = READARG_STRINGS("c"),
|
||||
[READARG_FORM_LONG] = READARG_STRINGS("config"),
|
||||
},
|
||||
.req = 1,
|
||||
.arg = {
|
||||
.name = "file",
|
||||
.bounds.val = {
|
||||
2,
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
{
|
||||
.names = {
|
||||
[READARG_FORM_SHORT] = READARG_STRINGS("i"),
|
||||
[READARG_FORM_LONG] = READARG_STRINGS("uri"),
|
||||
},
|
||||
.req = 1,
|
||||
.arg.bounds.inf = 1,
|
||||
.arg = {
|
||||
.name = "uri",
|
||||
.bounds.inf = 1,
|
||||
}
|
||||
},
|
||||
{
|
||||
.names = {
|
||||
[READARG_FORM_SHORT] = READARG_STRINGS("b"),
|
||||
[READARG_FORM_LONG] = READARG_STRINGS("backup", "backup-file"),
|
||||
},
|
||||
.req = 1,
|
||||
.arg.bounds.inf = 1,
|
||||
.arg = {
|
||||
.name = "file",
|
||||
.bounds.inf = 1,
|
||||
}
|
||||
},
|
||||
{
|
||||
.names = {
|
||||
|
@ -178,7 +183,7 @@ int main(int argc, char **argv)
|
|||
}
|
||||
}
|
||||
printf("{ [%zu] ", curr[i].arg.val.len);
|
||||
if (curr[i].req)
|
||||
if (curr[i].arg.name)
|
||||
{
|
||||
struct readarg_view_strings val = curr[i].arg.val;
|
||||
for (size_t j = 0; j < val.len; j++)
|
||||
|
|
Loading…
Reference in a new issue