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

Specify program-wide options? #11

Closed Thesola10 closed 7 years ago

Thesola10 commented 7 years ago

Hello, I would like to be able to define options that would apply to all commands, just like --help and --version. Here is my current YUCK file

Usage: cz [OPTION]...
A simple tool to reorganize your code.

    -               do not modify the file, write to stdout instead
    -v, --verbose   output more info
    -s, --silent    output less info

Usage: cz copy [FILE] [SECTION] [LINE] [OPTION]...
Copy a section to a target line number

Usage: cz move [FILE] [SECTION] [LINE] [OPTION]...
Move a section to a target line number

Usage: cz rm [FILE] [SECTION] [OPTION]...
Delete a section. 

    -c, --cache=DIR Change the cache directory to DIR

Usage: cz restore [FILE] [SECTION] [OPTION]...
Restore a cached section to where it previously was.

    -c, --cache=DIR Change the cache directory to DIR

I would like options -, --verbose and --silent to apply on any subcommand.

hroptatyr commented 7 years ago

In principle that works as you suggested, however the option consisting of just a dash (-) isn't supported.

Thesola10 commented 7 years ago

Ok, if I have time I will have a look at how to implement that.

Le lun. 27 mars 2017 à 20:00, Sebastian Freundt notifications@github.com a écrit :

In principle that works as you suggested, however the option consisting of just a dash (-) isn't supported.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hroptatyr/yuck/issues/11#issuecomment-289357124, or mute the thread https://github.com/notifications/unsubscribe-auth/AG8x5NGU_eqL4rXUza2cg9hxwuBLwgNvks5rp0jngaJpZM4MpaNI .

hroptatyr commented 7 years ago

Well, to be fair it's not hard to implement, the problem is that the single dash is often used to denote stdin or stdout and used as a positional argument. For instance: join FILE -. The thing about dash options is that they're non-positional, i.e. it doesn't matter if -s comes before -v or after. And then there's the thing that single char options can be agglutinated, like -sv to mean -s -v, the single dash doesn't fit in here either.

To be honest it took me a while to understand how you mean it: cz copy FILE foo 400 - would not modify file but instead reproduce the modification on stdout, right?

Why don't you do it sed style and reproduce the modifications on stdout by default with a -i|--in-place option to make the operation destructive. If you look at Unix-ish tools, none of them are destructive by default, thankfully, I rarely get a call to sed right in the first place which would leave me with a bogus file had sed chosen to operate destructively by default.

On the other hand I'm not here to tell you how to do your job, so here's another suggestion that doesn't interfere with current yuck: Use the - as a positional argument, something like:

yuck_parse(argi, argc, argv);
...
for (size_t i = 0; i < argi->nargs; i++) {
        if (!strcmp(argi->args[i], "-") {
                /* There's been a - argument on the command line */
                 ...
        }
}

And then turn the line with the dash into a comment like so:

Usage: cz copy [FILE] [SECTION] [LINE] [OPTION]... [-]
Copy a section to a target line number.  Use final - to not modify the file, but to write to stdout instead.

It's just a suggestion.

Thesola10 commented 7 years ago

What I mean is, can I make it so regular options can be applied to several commands such as verbose/silent and stdout. Stdout does not have to be a single dash.

Le mar. 28 mars 2017 à 05:27, Sebastian Freundt notifications@github.com a écrit :

Well, to be fair it's not hard to implement, the problem is that the single dash is often used to denote stdin or stdout and used as a positional argument. For instance: join FILE -. The thing about dash options is that they're non-positional, i.e. it doesn't matter if -s comes before -v or after. And then there's the thing that single char options can be agglutinated, like -sv to mean -s -v, the single dash doesn't fit in here either.

To be honest it took me a while to understand how you mean it: cz copy FILE foo 400 - would not modify file but instead reproduce the modification on stdout, right?

Why don't you do it sed style and reproduce the modifications on stdout by default with a -i|--in-place option to make the operation destructive. If you look at Unix-ish tools, none of them are destructive by default, thankfully, I rarely get a call to sed right in the first place which would leave me with a bogus file had sed chosen to operate destructively by default.

On the other hand I'm not here to tell you how to do your job, so here's another suggestion that doesn't interfere with current yuck: Use the - as a positional argument, something like:

yuck_parse(argi, argc, argv); ... for (size_t i = 0; i < argi->nargs; i++) { if (!strcmp(argi->args[i], "-") { / There's been a - argument on the command line / ... } }

And then turn the line with the dash into a comment like so:

Usage: cz copy [FILE] [SECTION] [LINE] [OPTION]... [-] Copy a section to a target line number. Use final - to not modify the file, but to write to stdout instead.

It's just a suggestion.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hroptatyr/yuck/issues/11#issuecomment-289652228, or mute the thread https://github.com/notifications/unsubscribe-auth/AG8x5HC8sC0iYtgPRd-CfWRfrZDE1bdRks5rqH4dgaJpZM4MpaNI .

hroptatyr commented 7 years ago

Yes, that's the default already. If you have a general Usage: line with no subcommands, all options there can be applied to all subcommands.

Thesola10 commented 7 years ago

Okay I see