Skip to content

Commit

Permalink
(also) support --flag=argument style for long options
Browse files Browse the repository at this point in the history
- add test
- mention in manpage

Fixes #294
  • Loading branch information
bagder committed May 13, 2024
1 parent 78d0fb6 commit 04a5f1d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
13 changes: 13 additions & 0 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@
"returncode": 0
}
},
{
"input": {
"arguments": [
"--set=host=moo",
"--set=scheme=http"
]
},
"expected": {
"stdout": "http://moo/\n",
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
Expand Down
2 changes: 2 additions & 0 deletions trurl.1
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ The first argument that is exactly two dashes ("--"), marks the end of
options; any argument after the end of options is interpreted as a URL
argument even if it starts with a dash.

Long options can be provided either as "--flag argument" or as
"--flag=argument".
.IP "-a, --append [component]=[data]"
Append data to a component. This can only append data to the path and the
query components.
Expand Down
31 changes: 25 additions & 6 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,24 @@ static void replaceadd(struct option *o,
if(n)
o->replace_list = n;
}

static bool longarg(const char *flag, const char *check)
{
/* the given flag might end with an equals sign */
size_t len = strlen(flag);
return (!strcmp(flag, check) ||
(!strncmp(flag, check, len) && check[len] == '='));
}

static bool checkoptarg(struct option *o, const char *flag,
const char *given,
const char *arg)
{
bool single = false;
bool shortopt = false;
if((flag[0] == '-') && (flag[1] != '-'))
single = true;
if((!strcmp(flag, given) && !single) ||
(!strncmp(flag, given, 2) && single)) {
shortopt = true;
if((!shortopt && longarg(flag, given)) ||
(!strncmp(flag, given, 2) && shortopt)) {
if(!arg)
errorf(o, ERROR_ARG, "Missing argument for %s", flag);
return true;
Expand All @@ -516,6 +525,13 @@ static int getarg(struct option *o,
arg = (char *)&flag[2];
gap = false;
}
else if((flag[0] == '-') && (flag[1] == '-')) {
char *equals = strchr(&flag[2], '=');
if(equals) {
arg = (char *)&equals[1];
gap = false;
}
}

if(!strcmp("--", flag))
o->end_of_options = true;
Expand Down Expand Up @@ -1578,8 +1594,11 @@ int main(int argc, const char **argv)
bool usedarg = false;
if(!o.end_of_options && argv[0][0] == '-') {
/* dash-dash prefixed */
if(getarg(&o, argv[0], argv[1], &usedarg))
errorf(&o, ERROR_FLAG, "unknown option: %s", argv[0]);
if(getarg(&o, argv[0], argv[1], &usedarg)) {
/* if the option ends with an equals sign, cut it there */
size_t not_e = strcspn(argv[0], "=");
errorf(&o, ERROR_FLAG, "unknown option: %.*s", (int)not_e, argv[0]);
}
}
else {
/* this is a URL */
Expand Down

0 comments on commit 04a5f1d

Please sign in to comment.