Skip to content

Commit

Permalink
Remove rest key from shargs args by applying the removeRest args stag…
Browse files Browse the repository at this point in the history
…e inside fromArgs
  • Loading branch information
Yord committed Jun 16, 2020
1 parent d8b567f commit 9ed9d14
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {flagsAsBools, restrictToOnly} = require('shargs-parser')
const {bestGuess} = require('./bestGuess')
const {groupOptions} = require('./groupOptions')
const {nestKeys} = require('./nestKeys')
const {removeRest} = require('./removeRest')
const {strToArgv} = require('./strToArgv')

const lexer = lexerSync({
Expand Down Expand Up @@ -40,7 +41,13 @@ function fromArgs (rawCommandNewline) {
)

return ({errs, args: [opts, cmd, ...rest]}) => {
const {errs: errs2, args: {...args2}} = fromArgsDefault({errs, args: [opts, addRawCommand(cmd), ...rest]})
return {errs: errs2, args: {...args2, rawCommand}}
const res = fromArgsDefault({errs, args: [opts, addRawCommand(cmd), ...rest]})

const {errs: errs2, args: args2} = removeRest(res)

return {
errs: errs2,
args: {...args2, rawCommand}
}
}
}
19 changes: 19 additions & 0 deletions src/parser/removeRest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {traverseArgs} = require('shargs-parser')

const removeRest = traverseArgs({
array: ({key, val, errs, args}) => {
const {[key]: _, ...rest} = args

return {
errs,
args: {
...rest,
...(key === '_' ? {} : {[key]: val})
}
}
}
})

module.exports = {
removeRest
}

0 comments on commit 9ed9d14

Please sign in to comment.