You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lloyd Brookes edited this page Jan 25, 2018
·
3 revisions
We can use stopAtFirstUnknown to parse an input argv array several times, making complex scenarios possible.
As an example we'll recreate the git merge command, which has this form.
$ git <command> [options]
Example
This is the full example script. After the first commandLineArgs() invocation, the _unknown list is used as input to the next invocation.
constcommandLineArgs=require('command-line-args')/* first - parse the main command */constmainDefinitions=[{name: 'command',defaultOption: true}]constmainOptions=commandLineArgs(mainDefinitions,{stopAtFirstUnknown: true})constargv=mainOptions._unknown||[]console.log('mainOptions\n===========')console.log(mainOptions)/* second - parse the merge command options */if(mainOptions.command==='merge'){constmergeDefinitions=[{name: 'squash',type: Boolean},{name: 'message',alias: 'm'}]constmergeOptions=commandLineArgs(mergeDefinitions,{ argv })console.log('\nmergeOptions\n============')console.log(mergeOptions)}
This command produces the following output.
$ node example.js merge --squash -m "This is my commit message"
mainOptions
===========
{ _unknown: [ '--squash', '-m', 'This is my commit message' ],
command: 'merge' }
mergeOptions
============
{ squash: true, message: 'This is my commit message' }