hroptatyr / yuck

Your Umbrella Command Kit, a bog-standard command line option parser for C with all the knickknackery and whatnots.
Other
61 stars 7 forks source link

do not exit when parse error or help #20

Closed zzs213 closed 5 years ago

zzs213 commented 5 years ago

I'm using yuck in a embedded system, This is a shell like program, It has several commands, So using yuck to parse option for them. But when using '--help' on these command, It call exit(..). This cause the main program crash.

So, How to change yuck behavior when encounters '--help' or parse error: Just return a error code.

I'm not familiar with m4, It looks like just change 'exit' to 'return' in "src/yuck-coru.c.m4" is enough. Right?

Maybe need to call yuck_free() before return?

hroptatyr commented 5 years ago

Hey, yes, you're right, instead of exit(EXIT_FAILURE) you'd put return -1, instead of exit(EXIT_SUCCESS) you could put return 1. And whenever yuck_parse returns with non-0 you'd call yuck_free() and invoke your own exit routine.

zzs213 commented 5 years ago

But this cause another issue: The function main() return 1 when we call yuck like this: "yuck --help" or "yuck -V" But it should return 0, So that somebody can test whether it is in th PATH

So may be yuck.c should modify too!

hroptatyr commented 5 years ago

Oh well it's up to you what you return. I was thinking like

int main()
{
    int rc = yuck_parse(...);
    if (rc < 0) {
        /* error */
        ...
        goto final;
    } else if (rc > 0) {
        /* success, probably -V or --help */
        ...
        goto final;
    }

     /* normal case with no parser error, and not --help nor -V */
     ...

final:
    yuck_free(...)
    return rc < 0;
}
zzs213 commented 5 years ago

Yeah! I got it. I'll test your new commit soon after Thanks!