fix again

This commit is contained in:
2026-07-01 13:46:22 +03:00
parent 566ca36af8
commit 503325b68f
3 changed files with 16 additions and 8 deletions

View File

@@ -56,7 +56,7 @@ static glob_pars const Gdefault = {
// you can repeat the same `val` for several long-only options without any problem
static sl_option_t cmdlnopts[] = {
// short option in only-long options should be zeroed, or you can add flag to set it to given value
{"lo0", NEED_ARG, NULL, 0, arg_int, APTR(&G.lo0), _("only long arg 0 (int)")},
{"longonly",NEED_ARG, NULL, 0, arg_int, APTR(&G.lo0), _("only long arg 0 (int)")},
// for short-only options long option can be NULL
{NULL, NEED_ARG, NULL, '0', arg_string, APTR(&G.so1), _("only short arg 1 (string)")},
// if you change `arg_int` to `arg_none`, value will be incremented each `-h`
@@ -73,7 +73,7 @@ static sl_option_t cmdlnopts[] = {
{"Int", MULT_PAR, NULL, 'I', arg_int, APTR(&G.intarr), _("integer parameter")},
{"Dbl", MULT_PAR, NULL, 'D', arg_double, APTR(&G.dblarr), _("double parameter")},
{"Str", MULT_PAR, NULL, 'S', arg_string, APTR(&G.strarr), _("string parameter")},
{"lo1", NEED_ARG, NULL, 0, arg_int, APTR(&G.lo1), _("only long arg 1 (int)")},
{"long1", NEED_ARG, NULL, 0, arg_int, APTR(&G.lo1), _("only long arg 1 (int)")},
end_option
};

View File

@@ -93,9 +93,9 @@ int main(int argc, char *argv[]){
printf("String[%d]: \"%s\"\n", i, *p++);
}
}
if(GP->lo0 != INT_MIN) printf("You set lo0 to %d\n", GP->lo0);
if(GP->lo1 != INT_MIN) printf("You set lo1 to %d\n", GP->lo1);
if(GP->lo2 != INT_MIN) printf("You set lo2 to %d\n", GP->lo2);
if(GP->lo0 != INT_MIN) printf("You set long 0 to %d\n", GP->lo0);
if(GP->lo1 != INT_MIN) printf("You set long 1 to %d\n", GP->lo1);
if(GP->lo2 != INT_MIN) printf("You set long 2 to %d\n", GP->lo2);
if(GP->so1){
sl_remove_quotes(GP->so1);
printf("String so1=%s\n", GP->so1);

View File

@@ -127,6 +127,7 @@ static int get_optind(const char *key, int opt, sl_option_t *options, void (*hel
int oind = 0, theopt = opt;
sl_option_t *opts = options;
assert(opts);
//printf("key: %s\n", key);
// `opt` should be ':' for "missed arguments", '?' for "not found" and short flag if found and checked
if(opt == '?'){ // not found
fprintf(stderr, _("No such parameter: `%s`\n"), key);
@@ -134,18 +135,25 @@ static int get_optind(const char *key, int opt, sl_option_t *options, void (*hel
return -1; // never reached until `helpfun` changed
}else if(opt == ':') theopt = optopt; // search to show helpstring "need parameter"
if(key[1] != '-'){ // search by unique short option
//printf("Short %c\n", theopt);
for(oind = 0; opts->help && opts->val != theopt; oind++, opts++){
DBG("cmp %c and %c", theopt, opts->val);
}
}else{ // search by long option
key += 2;
int l = strlen(key);
for(oind = 0; opts->help; oind++, opts++){
if(opts->name && 0 == strcmp(key, opts->name)) break;
//printf("comp %s and %s\n", key, opts->name);
if(opts->name && 0 == strncmp(key, opts->name, l)) break;
}
}
if(!opts->help) return -1;
if(!opts->help){
//printf("Not found\n");
return -1;
}
if(opt == ':'){
fprintf(stderr, _("Parameter `%s` needs value\n"), key);
if(options[oind].name) fprintf(stderr, _("Parameter `--%s` needs value\n"), options[oind].name);
else fprintf(stderr, _("Parameter `-%c` needs value\n"), options[oind].val);
helpfun(oind, options);
return -1; // never reached until `helpfun` changed
}