Skip to content

Commit 304b903

Browse files
committed
fix: log warnings about unknown config situations
1 parent 8dd86dd commit 304b903

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

lib/nopt-lib.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const abbrev = require('abbrev')
22
const debug = require('./debug')
33
const defaultTypeDefs = require('./type-defs')
4+
const { log } = require('proc-log')
45

56
const hasOwn = (o, k) => Object.prototype.hasOwnProperty.call(o, k)
67

@@ -298,7 +299,10 @@ function parse (args, data, remain, {
298299
arg = arg.slice(3)
299300
}
300301

301-
if (abbrevs[arg]) {
302+
// abbrev includes the original full string in its abbrev list
303+
if (abbrevs[arg] && abbrevs[arg] !== arg) {
304+
/* eslint-disable-next-line max-len */
305+
log.warn(`Expanding "--${arg}" to "--${abbrevs[arg]}". This will stop working in the next major version of npm.`)
302306
arg = abbrevs[arg]
303307
}
304308

@@ -331,6 +335,11 @@ function parse (args, data, remain, {
331335
(argType === null ||
332336
isTypeArray && ~argType.indexOf(null)))
333337

338+
if (typeof argType === 'undefined' && !hadEq && la && !la?.startsWith('-')) {
339+
// npm itself will log the warning about the undefined argType
340+
log.warn(`"${la}" is being parsed as a normal command line argument.`)
341+
}
342+
334343
if (isBool) {
335344
// just set and move along
336345
val = !no
@@ -458,6 +467,8 @@ function resolveShort (arg, ...rest) {
458467

459468
// if it's an abbr for a shorthand, then use that
460469
if (shortAbbr[arg]) {
470+
/* eslint-disable-next-line max-len */
471+
log.warn(`Expanding "--${arg}" to "--${shortAbbr[arg]}". This will stop working in the next major version of npm.`)
461472
arg = shortAbbr[arg]
462473
}
463474

test/lib.js

+56-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@ const t = require('tap')
22
const noptLib = require('../lib/nopt-lib.js')
33
const Stream = require('stream')
44

5-
const nopt = (t, argv, opts, expected) => {
5+
const logs = []
6+
t.afterEach(() => {
7+
logs.length = 0
8+
})
9+
process.on('log', (...msg) => {
10+
logs.push(msg)
11+
})
12+
13+
const nopt = (t, argv, opts, expected, expectedLogs) => {
614
if (Array.isArray(argv)) {
715
t.strictSame(noptLib.nopt(argv, { typeDefs: noptLib.typeDefs, ...opts }), expected)
816
} else {
917
noptLib.clean(argv, { typeDefs: noptLib.typeDefs, ...opts })
1018
t.match(argv, expected)
1119
}
20+
if (expectedLogs) {
21+
t.match(expectedLogs, logs)
22+
}
1223
t.end()
1324
}
1425

@@ -125,6 +136,43 @@ t.test('false invalid handler', (t) => {
125136
})
126137
})
127138

139+
t.test('longhand abbreviation', (t) => {
140+
nopt(t, ['--lon', 'text'], {
141+
types: {
142+
long: String,
143+
},
144+
}, {
145+
long: 'text',
146+
argv: {
147+
remain: [],
148+
cooked: ['--lon', 'text'],
149+
original: ['--lon', 'text'],
150+
},
151+
}, [
152+
/* eslint-disable-next-line max-len */
153+
['warn', 'Expanding "--lon" to "--long". This will stop working in the next major version of npm.'],
154+
])
155+
})
156+
157+
t.test('shorthand abbreviation', (t) => {
158+
nopt(t, ['--shor'], {
159+
types: {},
160+
shorthands: {
161+
short: '--shorthand',
162+
},
163+
}, {
164+
shorthand: true,
165+
argv: {
166+
remain: [],
167+
cooked: ['--shorthand'],
168+
original: ['--shor'],
169+
},
170+
}, [
171+
/* eslint-disable-next-line max-len */
172+
['warn', 'Expanding "--shor" to "--short". This will stop working in the next major version of npm.'],
173+
])
174+
})
175+
128176
t.test('shorthands that is the same', (t) => {
129177
nopt(t, ['--sh'], {
130178
types: {},
@@ -142,14 +190,16 @@ t.test('shorthands that is the same', (t) => {
142190
})
143191

144192
t.test('unknown multiple', (t) => {
145-
nopt(t, ['--mult', '--mult', '--mult'], {
193+
nopt(t, ['--mult', '--mult', '--mult', 'extra'], {
146194
types: {},
147195
}, {
148196
mult: [true, true, true],
149197
argv: {
150-
remain: [],
151-
cooked: ['--mult', '--mult', '--mult'],
152-
original: ['--mult', '--mult', '--mult'],
198+
remain: ['extra'],
199+
cooked: ['--mult', '--mult', '--mult', 'extra'],
200+
original: ['--mult', '--mult', '--mult', 'extra'],
153201
},
154-
})
202+
}, [
203+
['warn', '"extra" is being parsed as a normal command line argument.'],
204+
])
155205
})

0 commit comments

Comments
 (0)