oclif / core

Node.js Open CLI Framework. Built by Salesforce.

Home Page:https://oclif.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make some of the optional flags required

bader-nasser opened this issue · comments

Is your feature request related to a problem? Please describe.
I have many optional flags, but I want at least one of these to be required.

https://github.com/bader-nasser/pdftools#pdftools-extract:

pdftools extract -i <value> -o <value> [-D] [-s] [-c] [-f <value> | -p <value> | -d <value>] [-l <value>
    |  | ] [-q even|odd] [-r north|south|east|west|left|right|down] [-k]

I want to set one of flags: -f, -l, -p or -d to be required. By the way, [-l <value> | | ] seems like a bug to me because I think it should look like [-l <value> | -p <value> | -d <value>] but I'm not sure!

Describe the solution you'd like
Maybe using a static property like:

static flagsRelations = FlagsRelations({
  required: {type: 'any', flags: [ 'firstPage', 'lastPage', 'pages', 'data' ]}
})

Describe alternatives you've considered
I tried to use if statements to make sure one of these flags is provided by the user!

firstPage = firstPage ?? lastPage;
lastPage = lastPage ?? firstPage;


if (firstPage && isUndefinedOrEmptyString(lastPage)) {
        lastPage = firstPage;
}


if (lastPage && isUndefinedOrEmptyString(firstPage)) {
        firstPage = lastPage;
}


if (
        isUndefinedOrEmptyString(firstPage) &&
        isUndefinedOrEmptyString(lastPage) &&
        isUndefinedOrEmptyString(pageRanges) &&
        isUndefinedOrEmptyString(data)
) {
        this.log(
                'This command is useless without using one of the following flags:',
        );
        this.log('--firstPage, --lastPage, --pageRanges, --data');
        this.log('See more help with --help');
} else {
       // ...
}

@bader-nasser Does using either the exactlyOne or relationships properties solve your use case?

If those don't get you what you need I'd recommend sticking with your current solution or submitting a PR for the feature yourself since I'm not sure we'd be able to prioritize this feature at the moment.

@mdonnalley No. I use relationships & exclusive and it handles some of my use cases.

firstPage: Flags.string({
        char: 'f',
        description: 'First page (defaults to lastPage)',
        // This flag cannot be specified alongside these other flags
        exclusive: ['pageRanges', 'data'],
}),
lastPage: Flags.string({
        char: 'l',
        description: 'Last page (defaults to firstPage)',
        // This flag cannot be specified alongside these other flags
        exclusive: ['pageRanges', 'data'],
}),
pageRanges: Flags.string({
        char: 'p',
        description: `Comma/Space-seperated list of page ranges (eg. "1-3, 5east, 4, 7-10even, 22-11odd")
See: https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-cat
See also: https://github.com/bader-nasser/pdftools/blob/main/test/docs/data.txt`,
        // This flag cannot be specified alongside these other flags
        exclusive: ['firstPage', 'lastPage', 'data'],
}),
data: Flags.string({
        char: 'd',
        description: `Data file (lines of page ranges)
See: https://github.com/bader-nasser/pdftools/blob/main/test/docs/data.txt`,
        // This flag cannot be specified alongside these other flags
        exclusive: ['firstPage', 'lastPage', 'pageRanges'],
}),
qualifier: Flags.string({
        char: 'q',
        options: ['even', 'odd'],
        description:
                'See: https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-cat',
        relationships: [
                // Make this flag dependent on at least one of these flags
                {type: 'some', flags: ['firstPage', 'lastPage']},
                // Make this flag exclusive of all these flags
                {type: 'none', flags: ['pageRanges', 'data']},
        ],
}),
rotation: Flags.string({
        char: 'r',
        options: ['north', 'south', 'east', 'west', 'left', 'right', 'down'],
        description:
                'See: https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-cat',
        relationships: [
                // Make this flag dependent on at least one of these flags
                {type: 'some', flags: ['firstPage', 'lastPage']},
                // Make this flag exclusive of all these flags
                {type: 'none', flags: ['pageRanges', 'data']},
        ],
}),

The current flags' configuration is great but I don't see a way to make some these flags required. I'll stick with my solution and I'm not sure about sending a PR for the time being.

This issue has been linked to a new work item: W-15187594