working on commands...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-06-13 18:07:54 +03:00
parent 63c8e7447e
commit c684fc0f08

30
test.js
View File

@ -117,6 +117,9 @@ var instances = function(obj){
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
module.OPTION_PATTERN = /^--?/
module.COMMAND_PATTERN = /^[a-zA-Z]/
// basic argv parser... // basic argv parser...
// //
// Format: // Format:
@ -217,7 +220,9 @@ var ArgvParser = function(spec){
process.exit() }}, process.exit() }},
// special values and methods... // special values and methods...
__opt_pattern__: /^--?/, __pre_check__: true,
__opt_pattern__: module.OPTION_PATTERN,
__cmd_pattern__: module.COMMAND_PATTERN,
__opts_width__: 3, __opts_width__: 3,
__doc_prefix__: '- ', __doc_prefix__: '- ',
@ -234,6 +239,7 @@ var ArgvParser = function(spec){
__usage__: function(){ __usage__: function(){
return `${ this.scriptname } [OPTIONS]` }, return `${ this.scriptname } [OPTIONS]` },
__doc__: undefined,
__examples__: undefined, __examples__: undefined,
__footer__: undefined, __footer__: undefined,
@ -242,13 +248,17 @@ var ArgvParser = function(spec){
process.exit(1) }, process.exit(1) },
// these are run in the context of spec... // these are run in the context of spec...
__getoptions__: function(){ __getoptions__: function(pattern){
var that = this var that = this
var handlers = {} var handlers = {}
pattern = pattern
|| this.__opt_pattern__
|| module.OPTION_PATTERN
Object.keys(this) Object.keys(this)
.forEach(function(opt){ .forEach(function(opt){
// skip special methods... // skip special methods...
if(/^__.*__$/.test(opt)){ if(/^__.*__$/.test(opt)
&& !pattern.test(opt)){
return } return }
var [k, h] = that.__gethandler__(opt) var [k, h] = that.__gethandler__(opt)
handlers[k] ? handlers[k] ?
@ -257,10 +267,14 @@ var ArgvParser = function(spec){
return Object.values(handlers) }, return Object.values(handlers) },
// XXX get all instances of ArgvParser in spec... // XXX get all instances of ArgvParser in spec...
__getcommands__: function(){ __getcommands__: function(){
return [] return this.__getoptions__(
}, this.__cmd_pattern__
|| module.COMMAND_PATTERN) },
__gethandler__: function(key){ __gethandler__: function(key){
key = key.replace(this.__opt_pattern__ || /^--?/, '-') key = key.replace(
this.__opt_pattern__
|| module.OPTION_PATTERN,
'-')
var seen = new Set([key]) var seen = new Set([key])
while(key in this while(key in this
&& typeof(this[key]) == typeof('str')){ && typeof(this[key]) == typeof('str')){
@ -273,7 +287,9 @@ var ArgvParser = function(spec){
}, spec) }, spec)
// sanity check -- this will detect argument loops... // sanity check -- this will detect argument loops...
spec.__getoptions__() if(!!spec.__pre_check__){
spec.__getoptions__()
spec.__getcommands__()}
return function(argv){ return function(argv){
var pattern = /^--?[a-zA-Z-]*$/ var pattern = /^--?[a-zA-Z-]*$/