Skip to content
Merged
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
13 changes: 12 additions & 1 deletion lib/nopt-lib.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const abbrev = require('abbrev')
const debug = require('./debug')
const defaultTypeDefs = require('./type-defs')
const { log } = require('proc-log')

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

Expand Down Expand Up @@ -298,7 +299,10 @@ function parse (args, data, remain, {
arg = arg.slice(3)
}

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

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

if (typeof argType === 'undefined' && !hadEq && la && !la?.startsWith('-')) {
// npm itself will log the warning about the undefined argType
log.warn(`"${la}" is being parsed as a normal command line argument.`)
}

if (isBool) {
// just set and move along
val = !no
Expand Down Expand Up @@ -458,6 +467,8 @@ function resolveShort (arg, ...rest) {

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

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"license": "ISC",
"dependencies": {
"abbrev": "^3.0.0"
"abbrev": "^3.0.0",
"proc-log": "^5.0.0"
},
"devDependencies": {
"@npmcli/eslint-config": "^5.0.0",
Expand Down
62 changes: 56 additions & 6 deletions test/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ const t = require('tap')
const noptLib = require('../lib/nopt-lib.js')
const Stream = require('stream')

const nopt = (t, argv, opts, expected) => {
const logs = []
t.afterEach(() => {
logs.length = 0
})
process.on('log', (...msg) => {
logs.push(msg)
})

const nopt = (t, argv, opts, expected, expectedLogs) => {
if (Array.isArray(argv)) {
t.strictSame(noptLib.nopt(argv, { typeDefs: noptLib.typeDefs, ...opts }), expected)
} else {
noptLib.clean(argv, { typeDefs: noptLib.typeDefs, ...opts })
t.match(argv, expected)
}
if (expectedLogs) {
t.match(expectedLogs, logs)
}
t.end()
}

Expand Down Expand Up @@ -125,6 +136,43 @@ t.test('false invalid handler', (t) => {
})
})

t.test('longhand abbreviation', (t) => {
nopt(t, ['--lon', 'text'], {
types: {
long: String,
},
}, {
long: 'text',
argv: {
remain: [],
cooked: ['--lon', 'text'],
original: ['--lon', 'text'],
},
}, [
/* eslint-disable-next-line max-len */
['warn', 'Expanding "--lon" to "--long". This will stop working in the next major version of npm.'],
])
})

t.test('shorthand abbreviation', (t) => {
nopt(t, ['--shor'], {
types: {},
shorthands: {
short: '--shorthand',
},
}, {
shorthand: true,
argv: {
remain: [],
cooked: ['--shorthand'],
original: ['--shor'],
},
}, [
/* eslint-disable-next-line max-len */
['warn', 'Expanding "--shor" to "--short". This will stop working in the next major version of npm.'],
])
})

t.test('shorthands that is the same', (t) => {
nopt(t, ['--sh'], {
types: {},
Expand All @@ -142,14 +190,16 @@ t.test('shorthands that is the same', (t) => {
})

t.test('unknown multiple', (t) => {
nopt(t, ['--mult', '--mult', '--mult'], {
nopt(t, ['--mult', '--mult', '--mult', 'extra'], {
types: {},
}, {
mult: [true, true, true],
argv: {
remain: [],
cooked: ['--mult', '--mult', '--mult'],
original: ['--mult', '--mult', '--mult'],
remain: ['extra'],
cooked: ['--mult', '--mult', '--mult', 'extra'],
original: ['--mult', '--mult', '--mult', 'extra'],
},
})
}, [
['warn', '"extra" is being parsed as a normal command line argument.'],
])
})
Loading