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

feat(parser): support short-option groups #59

Merged
merged 4 commits into from
Feb 6, 2022
Merged

feat(parser): support short-option groups #59

merged 4 commits into from
Feb 6, 2022

Conversation

bcoe
Copy link
Collaborator

@bcoe bcoe commented Feb 5, 2022

This is a proposal for supporting short option groups that is quite simple (which I like), and at the same time agrees with an edge-case documented in yargs-parser -- which make me think it's a reasonable approach (I at least haven't noticed many folks complain about this edge-case in the past).

The implementation

Quite simply, -foo expands out to -f, -o, -o, and each individual value is handled by the next step in the parser.

The interesting edge-case of this is that if withValue is configured for the terminal value in the short option list, and the short-option list is followed by a positional, it will be consumed:

const passedArgs = ['-rf', 'foo'];
const passedOptions = { withValue: ['f'] };
const expected = { flags: { r: true, f: true }, values: { r: undefined, f: 'foo' }, positionals: [] };
const args = parseArgs(passedArgs, passedOptions);

As mentioned, this behavior agrees with unit tests in yargs-parser:

// from yargs' test suite:
it('should set the value of the final option in a group to the next supplied value', function () {
    const parse = parser(['-cats', 'meow'])
    parse.should.have.property('c', true)
    parse.should.have.property('a', true)
    parse.should.have.property('t', true)
    parse.should.have.property('s', 'meow')
    parse.should.have.property('_').with.length(0)
})

TODO: document this decision in README.
Fixes #2

test/index.js Outdated Show resolved Hide resolved
test/index.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@shadowspawn shadowspawn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of non-blocking suggestions.

@bcoe bcoe requested a review from shadowspawn February 5, 2022 23:38
@bcoe
Copy link
Collaborator Author

bcoe commented Feb 5, 2022

@shadowspawn take a look at the updated FAQ, if you don't mind.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
bcoe and others added 3 commits February 5, 2022 18:58
Co-authored-by: John Gee <[email protected]>
Co-authored-by: John Gee <[email protected]>
Co-authored-by: John Gee <[email protected]>
README.md Show resolved Hide resolved
@shadowspawn
Copy link
Collaborator

LGTM again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for short option groups
4 participants