added option expansion...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-07-01 02:22:10 +03:00
parent fb0566f54c
commit 62fc0f62d5

28
argv.js
View File

@ -105,6 +105,8 @@ var afterCallbackCall = function(name, context, ...args){
// yet know of any error or stop conditions triggered later in the argv. // yet know of any error or stop conditions triggered later in the argv.
// //
// //
// XXX add merged options...
// -a -b -c -> -abc
// XXX --help should work for any command and not just for the nested // XXX --help should work for any command and not just for the nested
// parser commands... (???) // parser commands... (???)
// ...not sure how to implement this... // ...not sure how to implement this...
@ -117,6 +119,7 @@ var Parser =
module.Parser = module.Parser =
object.Constructor('Parser', { object.Constructor('Parser', {
// config... // config...
splitOptions: true,
optionPrefix: '-', optionPrefix: '-',
commandPrefix: '@', commandPrefix: '@',
// NOTE: we only care about differentiating an option from a command // NOTE: we only care about differentiating an option from a command
@ -311,6 +314,7 @@ object.Constructor('Parser', {
return module.STOP }}, return module.STOP }},
// common short-hands... // common short-hands...
//
// NOTE: defining this as a loop will enable the user to define any // NOTE: defining this as a loop will enable the user to define any
// of the aliases as the handler and thus breaking the loop... // of the aliases as the handler and thus breaking the loop...
// NOTE: unless the loop is broken this set of options is not usable. // NOTE: unless the loop is broken this set of options is not usable.
@ -360,7 +364,9 @@ object.Constructor('Parser', {
// ... // ...
// }, // },
// handleArgumentValue: function(handler, value){ // handleArgumentValue: function(handler, value){
// var convert = this.typeHandler[handler.type] // var convert = typeof(handler.type) == 'function' ?
// handler.type
// : this.typeHandler[handler.type]
// return convert ? // return convert ?
// convert(value) // convert(value)
// : value }, // : value },
@ -429,6 +435,7 @@ object.Constructor('Parser', {
var opt_pattern = this.optionInputPattern var opt_pattern = this.optionInputPattern
// helpers...
var runHandler = function(handler, arg, value, rest){ var runHandler = function(handler, arg, value, rest){
// get option value... // get option value...
value = value value = value
@ -458,6 +465,18 @@ object.Constructor('Parser', {
&& this.handleErrorExit && this.handleErrorExit
&& this.handleErrorExit(arg) } && this.handleErrorExit(arg) }
return res } return res }
var splitArgs = function(arg, rest){
// skip single letter unknown options or '--' options...
if(arg.length <= 2
|| arg.startsWith(that.optionPrefix.repeat(2))){
return undefined }
// split and normalize...
var [a, ...r] =
[...arg.slice(1)]
.map(function(e){ return '-'+ e })
// push new options back to option "stack"...
rest.splice(0, 0, ...r)
return that.handler(a)[1] }
var env = new Set() var env = new Set()
var unhandled = [] var unhandled = []
@ -472,7 +491,12 @@ object.Constructor('Parser', {
if(type != 'unhandled'){ if(type != 'unhandled'){
// get handler... // get handler...
var handler = this.handler(arg)[1] var handler = this.handler(arg)[1]
|| this.handleArgument // handle merged options...
|| (type == 'opt'
&& this.splitOptions
&& splitArgs(arg, rest))
// dynamic/error...
|| this.handleArgument
// env handler called... // env handler called...
handler.env handler.env
&& env.add(handler) && env.add(handler)