Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make some of the optional flags required #829

Open
bader-nasser opened this issue Oct 14, 2023 · 3 comments
Open

Make some of the optional flags required #829

bader-nasser opened this issue Oct 14, 2023 · 3 comments
Labels
enhancement New feature or request help wanted Accepting PRs

Comments

@bader-nasser
Copy link

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 {
       // ...
}
@mdonnalley
Copy link
Contributor

@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.

@bader-nasser
Copy link
Author

@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.

Copy link

git2gus bot commented Mar 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Accepting PRs
Projects
None yet
Development

No branches or pull requests

2 participants