Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

short options need no space separation for the argument anymore #292

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@
"returncode": 0
}
},
{
"input": {
"arguments": [
"-shost=moo",
"-sscheme=http"
]
},
"expected": {
"stdout": "http://moo/\n",
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
Expand Down Expand Up @@ -319,6 +332,20 @@
"returncode": 0
}
},
{
"input": {
"arguments": [
"--url",
"https://curl.se/we/are.html",
"-g{path}"
]
},
"expected": {
"stdout": "/we/are.html\n",
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
Expand Down
38 changes: 24 additions & 14 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,17 @@ static void replaceadd(struct option *o,
if(n)
o->replace_list = n;
}
static bool checkoptarg(struct option *o, const char *str,
static bool checkoptarg(struct option *o, const char *flag,
const char *given,
const char *arg)
{
if(!strcmp(str, given)) {
bool single = false;
if((flag[0] == '-') && (flag[1] != '-'))
single = true;
if((!strcmp(flag, given) && !single) ||
(!strncmp(flag, given, 2) && single)) {
if(!arg)
errorf(o, ERROR_ARG, "Missing argument for %s", str);
errorf(o, ERROR_ARG, "Missing argument for %s", flag);
return true;
}
return false;
Expand All @@ -505,8 +509,14 @@ static int getarg(struct option *o,
const char *arg,
bool *usedarg)
{
bool gap = true;
*usedarg = false;

if((flag[0] == '-') && (flag[1] != '-') && flag[2]) {
arg = (char *)&flag[2];
gap = false;
}

if(!strcmp("--", flag))
o->end_of_options = true;
else if(!strcmp("-v", flag) || !strcmp("--version", flag))
Expand All @@ -515,32 +525,32 @@ static int getarg(struct option *o,
help();
else if(checkoptarg(o, "--url", flag, arg)) {
urladd(o, arg);
*usedarg = true;
*usedarg = gap;
}
else if(checkoptarg(o, "-f", flag, arg) ||
checkoptarg(o, "--url-file", flag, arg)) {
urlfile(o, arg);
*usedarg = true;
*usedarg = gap;
}
else if(checkoptarg(o, "-a", flag, arg) ||
checkoptarg(o, "--append", flag, arg)) {
appendadd(o, arg);
*usedarg = true;
*usedarg = gap;
}
else if(checkoptarg(o, "-s", flag, arg) ||
checkoptarg(o, "--set", flag, arg)) {
setadd(o, arg);
*usedarg = true;
*usedarg = gap;
}
else if(checkoptarg(o, "--iterate", flag, arg)) {
iteradd(o, arg);
*usedarg = true;
*usedarg = gap;
}
else if(checkoptarg(o, "--redirect", flag, arg)) {
if(o->redirect)
errorf(o, ERROR_FLAG, "only one --redirect is supported");
o->redirect = arg;
*usedarg = true;
*usedarg = gap;
}
else if(checkoptarg(o, "--query-separator", flag, arg)) {
if(o->qsep)
Expand All @@ -549,11 +559,11 @@ static int getarg(struct option *o,
errorf(o, ERROR_FLAG,
"only single-letter query separators are supported");
o->qsep = arg;
*usedarg = true;
*usedarg = gap;
}
else if(checkoptarg(o, "--trim", flag, arg)) {
trimadd(o, arg);
*usedarg = true;
*usedarg = gap;
}
else if(checkoptarg(o, "-g", flag, arg) ||
checkoptarg(o, "--get", flag, arg)) {
Expand All @@ -563,7 +573,7 @@ static int getarg(struct option *o,
errorf(o, ERROR_FLAG,
"--get is mututally exclusive with --json");
o->format = arg;
*usedarg = true;
*usedarg = gap;
}
else if(!strcmp("--json", flag)) {
if(o->format)
Expand Down Expand Up @@ -606,12 +616,12 @@ static int getarg(struct option *o,
o->quiet_warnings = true;
else if(!strcmp("--replace", flag)) {
replaceadd(o, arg);
*usedarg = true;
*usedarg = gap;
}
else if(!strcmp("--force-replace", flag)) {
replaceadd(o, arg);
o->force_replace = true;
*usedarg = true;
*usedarg = gap;
}
else
return 1; /* unrecognized option */
Expand Down