Skip to content

Commit a3936aa

Browse files
coreyfarrellbcoe
authored andcommitted
feat: add strip-aliased and strip-dashed configuration options. (#172)
1 parent 0ae7fcb commit a3936aa

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,49 @@ node example.js -a run b -x y
340340
{ _: [ 'run', 'b', '-x', 'y' ], a: true }
341341
```
342342
343+
### strip aliased
344+
345+
* default: `false`
346+
* key: `strip-aliased`
347+
348+
Should aliases be removed before returning results?
349+
350+
_If disabled:_
351+
352+
```sh
353+
node example.js --test-field 1
354+
{ _: [], 'test-field': 1, testField: 1, 'test-alias': 1, testAlias: 1 }
355+
```
356+
357+
_If enabled:_
358+
359+
```sh
360+
node example.js --test-field 1
361+
{ _: [], 'test-field': 1, testField: 1 }
362+
```
363+
364+
### strip dashed
365+
366+
* default: `false`
367+
* key: `strip-dashed`
368+
369+
Should dashed keys be removed before returning results? This option has no effect if
370+
`camel-case-exansion` is disabled.
371+
372+
_If disabled:_
373+
374+
```sh
375+
node example.js --test-field 1
376+
{ _: [], 'test-field': 1, testField: 1 }
377+
```
378+
379+
_If enabled:_
380+
381+
```sh
382+
node example.js --test-field 1
383+
{ _: [], testField: 1 }
384+
```
385+
343386
## Special Thanks
344387
345388
The yargs project evolves from optimist and minimist. It owes its

index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ function parse (args, opts) {
2424
'populate--': false,
2525
'combine-arrays': false,
2626
'set-placeholder-key': false,
27-
'halt-at-non-option': false
27+
'halt-at-non-option': false,
28+
'strip-aliased': false,
29+
'strip-dashed': false
2830
}, opts.configuration)
2931
var defaults = opts.default || {}
3032
var configObjects = opts.configObjects || []
@@ -331,6 +333,23 @@ function parse (args, opts) {
331333
argv[notFlagsArgv].push(key)
332334
})
333335

336+
if (configuration['camel-case-expansion'] && configuration['strip-dashed']) {
337+
Object.keys(argv).filter(key => key !== '--' && key.includes('-')).forEach(key => {
338+
delete argv[key]
339+
})
340+
}
341+
342+
if (configuration['strip-aliased']) {
343+
// XXX Switch to [].concat(...Object.values(aliases)) once node.js 6 is dropped
344+
;[].concat(...Object.keys(aliases).map(k => aliases[k])).forEach(alias => {
345+
if (configuration['camel-case-expansion']) {
346+
delete argv[alias.split('.').map(prop => camelCase(prop)).join('.')]
347+
}
348+
349+
delete argv[alias]
350+
})
351+
}
352+
334353
// how many arguments should we consume, based
335354
// on the nargs option?
336355
function eatNargs (i, key, args) {

test/yargs-parser.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,4 +2874,71 @@ describe('yargs-parser', function () {
28742874
})
28752875
argv.bar.should.equal('hello')
28762876
})
2877+
2878+
describe('stripping', function () {
2879+
it('strip-dashed removes expected fields from argv', function () {
2880+
const argv = parser([ '--test-value', '1' ], {
2881+
number: ['test-value'],
2882+
alias: {
2883+
'test-value': ['alt-test']
2884+
},
2885+
configuration: {
2886+
'strip-dashed': true
2887+
}
2888+
})
2889+
argv.should.deep.equal({
2890+
_: [],
2891+
'testValue': 1,
2892+
'altTest': 1
2893+
})
2894+
})
2895+
2896+
it('strip-aliased removes expected fields from argv', function () {
2897+
const argv = parser([ '--test-value', '1' ], {
2898+
number: ['test-value'],
2899+
alias: {
2900+
'test-value': ['alt-test']
2901+
},
2902+
configuration: {
2903+
'strip-aliased': true
2904+
}
2905+
})
2906+
argv.should.deep.equal({
2907+
_: [],
2908+
'test-value': 1,
2909+
'testValue': 1
2910+
})
2911+
})
2912+
2913+
it('strip-aliased and strip-dashed combined removes expected fields from argv', function () {
2914+
const argv = parser([ '--test-value', '1' ], {
2915+
number: ['test-value'],
2916+
alias: {
2917+
'test-value': ['alt-test']
2918+
},
2919+
configuration: {
2920+
'strip-aliased': true,
2921+
'strip-dashed': true
2922+
}
2923+
})
2924+
argv.should.deep.equal({
2925+
_: [],
2926+
'testValue': 1
2927+
})
2928+
})
2929+
2930+
it('ignores strip-dashed if camel-case-expansion is disabled', function () {
2931+
const argv = parser([ '--test-value', '1' ], {
2932+
number: ['test-value'],
2933+
configuration: {
2934+
'camel-case-expansion': false,
2935+
'strip-dashed': true
2936+
}
2937+
})
2938+
argv.should.deep.equal({
2939+
_: [],
2940+
'test-value': 1
2941+
})
2942+
})
2943+
})
28772944
})

0 commit comments

Comments
 (0)