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: support single dash as option value for long option #472

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/yargs-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class YargsParser {
} else {
next = args[i + 1]

if (next !== undefined && (!next.match(/^-/) ||
if (next !== undefined && (!next.match(/^-./) ||
next.match(negative)) &&
!checkAllAliases(key, flags.bools) &&
!checkAllAliases(key, flags.counts)) {
Expand Down
26 changes: 23 additions & 3 deletions test/yargs-parser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,23 +1487,31 @@ describe('yargs-parser', function () {
})

describe('-', function () {
it('should set - as value of n', function () {
it('should set - as space separated value of n', function () {
const argv = parser(['-n', '-'])
argv.should.have.property('n', '-')
argv.should.have.property('_').with.length(0)
})

it('should set - as a non-hyphenated value', function () {
it('should set - as a non-hyphenated value (positional)', function () {
const argv = parser(['-'])
argv.should.have.property('_').and.deep.equal(['-'])
})

it('should set - as a value of f', function () {
it('should set - as an embedded value of f', function () {
// special case dash trailing short option
const argv = parser(['-f-'])
argv.should.have.property('f', '-')
argv.should.have.property('_').with.length(0)
})

it('should set - as an embedded value of f with =', function () {
// usual style for embedded short option value
const argv = parser(['-f=-'])
argv.should.have.property('f', '-')
argv.should.have.property('_').with.length(0)
})

it('should set b to true and set - as a non-hyphenated value when b is set as a boolean', function () {
const argv = parser(['-b', '-'], {
boolean: ['b']
Expand All @@ -1521,6 +1529,18 @@ describe('yargs-parser', function () {
argv.should.have.property('s', '-')
argv.should.have.property('_').with.length(0)
})

it('should set - as space separated value of foo', function () {
const argv = parser(['--foo', '-'])
argv.should.have.property('foo', '-')
argv.should.have.property('_').with.length(0)
})

it('should set - as embedded value of foo', function () {
const argv = parser(['--foo=-'])
argv.should.have.property('foo', '-')
argv.should.have.property('_').with.length(0)
})
})

describe('count', function () {
Expand Down