arcanis / clipanion

Type-safe CLI library / framework with no runtime dependencies
https://mael.dev/clipanion/
1.1k stars 61 forks source link

Support for default-enabled boolean command options #143

Closed albertvaka closed 1 year ago

albertvaka commented 1 year ago

I want my users to be able to disable certain behavior in my CLI, by passing --no-<something>.

Currently I can do that by adding a boolean option, eg: --no-my-optional-feature and store that as noMyOptionalFeature boolean. But then in my code I have to do double negations: if (!noMyOptionalFeature) { enableOptionalFeature(); } because my boolean is true when I don't want that feature enabled.

It would be nice to have support for positive boolean arguments that default to true and that can be disabled by default by prefixing them with no-. This way, also, if as a user I want to explicitly enable the optional feature I could also write --my-optional-feature (without the no- prefix) and that would also be a valid argument.

merceyz commented 1 year ago

This is already how Option.Boolean works;

import { Command, Option, runExit } from 'clipanion';

class Foo extends Command {
    optionalFeature = Option.Boolean(`--my-optional-feature`, true);

    async execute() {
        console.log(this.optionalFeature);

        return 0;
    }
}

runExit([Foo]);
$ my-cli
true
$ my-cli --no-my-optional-feature
false
$ my-cli --my-optional-feature
true