Skip to content

Commit a3ddacc

Browse files
authored
feat: requiresArg is now simply an alias for nargs(1) (#1054)
BREAKING CHANGE: requiresArg now has significantly different error output, matching nargs.
1 parent 2b56812 commit a3ddacc

File tree

6 files changed

+10
-57
lines changed

6 files changed

+10
-57
lines changed

lib/validation.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,40 +54,6 @@ module.exports = function validation (yargs, usage, y18n) {
5454
}
5555
}
5656

57-
// make sure that any args that require an
58-
// value (--foo=bar), have a value.
59-
self.missingArgumentValue = function missingArgumentValue (argv) {
60-
const options = yargs.getOptions()
61-
if (options.requiresArg.length > 0) {
62-
const missingRequiredArgs = []
63-
const defaultValues = new Set([true, false, ''])
64-
65-
options.requiresArg.forEach((key) => {
66-
// if the argument is not set in argv no need to check
67-
// whether a right-hand-side has been provided.
68-
if (!argv.hasOwnProperty(key)) return
69-
70-
const value = argv[key]
71-
// if a value is explicitly requested,
72-
// flag argument as missing if it does not
73-
// look like foo=bar was entered.
74-
if (defaultValues.has(value) ||
75-
(Array.isArray(value) && !value.length)) {
76-
missingRequiredArgs.push(key)
77-
}
78-
})
79-
80-
if (missingRequiredArgs.length > 0) {
81-
usage.fail(__n(
82-
'Missing argument value: %s',
83-
'Missing argument values: %s',
84-
missingRequiredArgs.length,
85-
missingRequiredArgs.join(', ')
86-
))
87-
}
88-
}
89-
}
90-
9157
// make sure all the required arguments are present.
9258
self.requiredArguments = function requiredArguments (argv) {
9359
const demandedOptions = yargs.getDemandedOptions()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"string-width": "^2.0.0",
2424
"which-module": "^2.0.0",
2525
"y18n": "^3.2.1",
26-
"yargs-parser": "^8.1.0"
26+
"yargs-parser": "^9.0.2"
2727
},
2828
"devDependencies": {
2929
"chai": "^4.1.2",

test/usage.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ describe('usage tests', () => {
693693
' --version Show version number [boolean]',
694694
' --foo, -f foo option',
695695
' --bar, -b bar option',
696-
'Missing argument value: foo'
696+
'Not enough arguments following: f'
697697
])
698698
})
699699

@@ -722,7 +722,7 @@ describe('usage tests', () => {
722722
' --version Show version number [boolean]',
723723
' --foo, -f foo option',
724724
' --bar, -b bar option',
725-
'Missing argument values: foo, bar'
725+
'Not enough arguments following: bar'
726726
])
727727
})
728728
})
@@ -754,7 +754,7 @@ describe('usage tests', () => {
754754
' --version Show version number [boolean]',
755755
' --foo, -f foo option',
756756
' --bar, -b bar option',
757-
'Missing argument value: foo'
757+
'Not enough arguments following: f'
758758
])
759759
})
760760
})
@@ -769,7 +769,7 @@ describe('usage tests', () => {
769769
.argv
770770
)
771771

772-
r.errors[1].should.equal('Missing argument values: foo, bar')
772+
r.errors[1].should.equal('Not enough arguments following: bar')
773773
})
774774
})
775775

test/validation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ describe('validation tests', () => {
441441
.option('w', {type: 'string', requiresArg: true})
442442
.parse('-w', (err, argv, output) => {
443443
expect(err).to.exist
444-
expect(err).to.have.property('message', 'Missing argument value: w')
444+
expect(err).to.have.property('message', 'Not enough arguments following: w')
445445
return done()
446446
})
447447
})
@@ -451,7 +451,7 @@ describe('validation tests', () => {
451451
.option('w', {type: 'boolean', requiresArg: true})
452452
.parse('-w', (err, argv, output) => {
453453
expect(err).to.exist
454-
expect(err).to.have.property('message', 'Missing argument value: w')
454+
expect(err).to.have.property('message', 'Not enough arguments following: w')
455455
return done()
456456
})
457457
})
@@ -461,7 +461,7 @@ describe('validation tests', () => {
461461
.option('w', {type: 'array', requiresArg: true})
462462
.parse('-w', (err, argv, output) => {
463463
expect(err).to.exist
464-
expect(err).to.have.property('message', 'Missing argument value: w')
464+
expect(err).to.have.property('message', 'Not enough arguments following: w')
465465
return done()
466466
})
467467
})

test/yargs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ describe('yargs dsl tests', () => {
226226
defaultDescription: {},
227227
choices: {},
228228
coerce: {},
229-
requiresArg: [],
230229
skipValidation: [],
231230
count: [],
232231
normalize: [],

yargs.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function Yargs (processArgs, cwd, parentRequire) {
9292
groups = {}
9393

9494
const arrayOptions = [
95-
'array', 'boolean', 'string', 'requiresArg', 'skipValidation',
95+
'array', 'boolean', 'string', 'skipValidation',
9696
'count', 'normalize', 'number'
9797
]
9898

@@ -204,7 +204,7 @@ function Yargs (processArgs, cwd, parentRequire) {
204204

205205
self.requiresArg = function (keys) {
206206
argsert('<array|string>', [keys], arguments.length)
207-
populateParserHintArray('requiresArg', keys)
207+
populateParserHintObject(self.nargs, false, 'narg', keys, 1)
208208
return self
209209
}
210210

@@ -965,17 +965,6 @@ function Yargs (processArgs, cwd, parentRequire) {
965965
options.__ = y18n.__
966966
options.configuration = pkgUp()['yargs'] || {}
967967

968-
// numbers are defaulted to `undefined`, which makes it impossible to verify requiresArg
969-
// so _unlike_ other types, we will implicitly add them to narg when requiresArg is true
970-
const numberTyped = new Set(options.number)
971-
options.requiresArg
972-
.filter(key => numberTyped.has(key))
973-
.forEach(key => {
974-
// if narg _and_ requiresArg are configured for an option,
975-
// we should probably throw an error somewhere indicating conflicting configuration
976-
options.narg[key] = 1
977-
})
978-
979968
const parsed = Parser.detailed(args, options)
980969
let argv = parsed.argv
981970
if (parseContext) argv = Object.assign({}, argv, parseContext)
@@ -1118,7 +1107,6 @@ function Yargs (processArgs, cwd, parentRequire) {
11181107
self._runValidation = function runValidation (argv, aliases, positionalMap, parseErrors) {
11191108
if (parseErrors) throw new YError(parseErrors.message)
11201109
validation.nonOptionCount(argv)
1121-
validation.missingArgumentValue(argv)
11221110
validation.requiredArguments(argv)
11231111
if (strict) validation.unknownArguments(argv, aliases, positionalMap)
11241112
validation.customChecks(argv, aliases)

0 commit comments

Comments
 (0)