mirror of
				https://github.com/flynx/argv.js.git
				synced 2025-10-31 03:30:08 +00:00 
			
		
		
		
	experimenting, tweaking and refactoring....
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									cd3da11274
								
							
						
					
					
						commit
						390204cb35
					
				
							
								
								
									
										50
									
								
								argv.js
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								argv.js
									
									
									
									
									
								
							| @ -62,6 +62,7 @@ var afterCallbackCall = function(name, context, ...args){ | |||||||
| //		'-test': {
 | //		'-test': {
 | ||||||
| //			doc: 'test option.',
 | //			doc: 'test option.',
 | ||||||
| //			arg: 'VALUE',
 | //			arg: 'VALUE',
 | ||||||
|  | //			env: 'VALUE',
 | ||||||
| //			handler: function(opts, key, value){ 
 | //			handler: function(opts, key, value){ 
 | ||||||
| //				...
 | //				...
 | ||||||
| //			}},
 | //			}},
 | ||||||
| @ -97,17 +98,26 @@ var afterCallbackCall = function(name, context, ...args){ | |||||||
| // 	}
 | // 	}
 | ||||||
| //
 | //
 | ||||||
| //
 | //
 | ||||||
|  | // It is recommended not to do any processing with side-effects in 
 | ||||||
|  | // option/command handlers directly, prepare for the execution and to 
 | ||||||
|  | // the actual work in the .then(..) callback. The reason being that the 
 | ||||||
|  | // option handlers are called while parsing options and thus may not 
 | ||||||
|  | // yet know of any error or stop conditions triggered later in the argv.
 | ||||||
|  | //
 | ||||||
|  | //
 | ||||||
| // 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...
 | ||||||
| // XXX should .options(..), .commands(..) and .handler(..) be:
 | // 		.....or should it be the responsibility of the user defining 
 | ||||||
| // 		.getOptions(..), .getCommands(..) and .getHandler(..) respectively???
 | // 		the command???
 | ||||||
| // XXX should we handle <scriptName>-<command> script calls???
 | // XXX should we handle <scriptName>-<command> script calls???
 | ||||||
| // XXX might be a good idea to add handler.env = x to use the environment 
 | // XXX ENV might be a good idea to add handler.env = x to use the environment 
 | ||||||
| // 		variable x as the default value for option...
 | // 		variable x as the default value for option...
 | ||||||
| // 		...would also need to add this to -help as '(default: $VARIABLE_NAME)'
 | // 		...would also need to add this to -help as '(default: $VARIABLE_NAME)'
 | ||||||
| // XXX should this be split into BaseParser (base functionality) and 
 | // 		.....for this we'll need to set all the env options regardless
 | ||||||
| // 		Parser (defaults)???
 | // 		if they were passed by user or not...
 | ||||||
|  | // XXX should .options(..), .commands(..) and .handler(..) be:
 | ||||||
|  | // 		.getOptions(..), .getCommands(..) and .getHandler(..) respectively???
 | ||||||
| var Parser = | var Parser = | ||||||
| module.Parser = | module.Parser = | ||||||
| object.Constructor('Parser', { | object.Constructor('Parser', { | ||||||
| @ -120,10 +130,10 @@ object.Constructor('Parser', { | |||||||
| 	commandInputPattern: /^([a-zA-Z].*)$/, | 	commandInputPattern: /^([a-zA-Z].*)$/, | ||||||
| 
 | 
 | ||||||
| 	// instance stuff...
 | 	// instance stuff...
 | ||||||
| 	// XXX revise...
 |  | ||||||
| 	argv: null, | 	argv: null, | ||||||
| 	pre_argv: null, | 	pre_argv: null, | ||||||
| 	rest: null, | 	rest: null, | ||||||
|  | 
 | ||||||
| 	scriptNmae: null, | 	scriptNmae: null, | ||||||
| 	scriptPath: null, | 	scriptPath: null, | ||||||
| 
 | 
 | ||||||
| @ -232,6 +242,10 @@ object.Constructor('Parser', { | |||||||
| 			var expandVars = function(str){ | 			var expandVars = function(str){ | ||||||
| 				return str | 				return str | ||||||
| 					.replace(/\$SCRIPTNAME/g, that.scriptName) } | 					.replace(/\$SCRIPTNAME/g, that.scriptName) } | ||||||
|  | 			var formDoc = function(doc, env){ | ||||||
|  | 				return [doc, ...(env ?  | ||||||
|  | 					[`(default value: \$${env})`]  | ||||||
|  | 					: [])] } | ||||||
| 			var getValue = function(name){ | 			var getValue = function(name){ | ||||||
| 				return that[name] ? | 				return that[name] ? | ||||||
| 					['', typeof(that[name]) == 'function' ? | 					['', typeof(that[name]) == 'function' ? | ||||||
| @ -253,14 +267,16 @@ object.Constructor('Parser', { | |||||||
| 					// XXX add option groups...
 | 					// XXX add option groups...
 | ||||||
| 					...section('Options', | 					...section('Options', | ||||||
| 						this.options() | 						this.options() | ||||||
| 							.map(function([opts, arg, doc]){ | 							.map(function([opts, arg, doc, handler]){ | ||||||
| 								return [  | 								return [  | ||||||
| 									opts | 									opts | ||||||
| 										.sort(function(a, b){  | 										.sort(function(a, b){  | ||||||
| 											return a.length - b.length}) | 											return a.length - b.length}) | ||||||
| 										.join(' | -')  | 										.join(' | -')  | ||||||
| 											+' '+ (arg || ''),  | 											+' '+ (arg || ''),  | ||||||
| 									doc] })), | 									// XXX ENV
 | ||||||
|  | 									//...formDoc(doc, handler.env) ] })),
 | ||||||
|  | 									...formDoc(doc) ] })), | ||||||
| 					// dynamic options...
 | 					// dynamic options...
 | ||||||
| 					...section('Dynamic options', | 					...section('Dynamic options', | ||||||
| 						this.handleArgument ?  | 						this.handleArgument ?  | ||||||
| @ -269,12 +285,15 @@ object.Constructor('Parser', { | |||||||
| 					// commands (optional)...
 | 					// commands (optional)...
 | ||||||
| 					...section('Commands', | 					...section('Commands', | ||||||
| 						this.commands() | 						this.commands() | ||||||
| 							.map(function([cmd, _, doc]){ | 							.map(function([cmd, arg, doc, handler]){ | ||||||
| 								return [ | 								return [ | ||||||
| 									cmd | 									cmd | ||||||
| 										.map(function(cmd){ return cmd.slice(1)}) | 										.map(function(cmd){ return cmd.slice(1)}) | ||||||
| 										.join(' | '),  | 										.join(' | ') | ||||||
| 									doc] })), | 											+' '+ (arg || ''),  | ||||||
|  | 									// XXX ENV
 | ||||||
|  | 									//...formDoc(doc, handler.env) ] })),
 | ||||||
|  | 									...formDoc(doc) ] })), | ||||||
| 					// examples (optional)...
 | 					// examples (optional)...
 | ||||||
| 					...section('Examples', | 					...section('Examples', | ||||||
| 						this.examples instanceof Array ? | 						this.examples instanceof Array ? | ||||||
| @ -352,7 +371,7 @@ object.Constructor('Parser', { | |||||||
| 	//				: value },
 | 	//				: value },
 | ||||||
| 	//
 | 	//
 | ||||||
| 	// XXX would be nice to be able to collect arrays...
 | 	// XXX would be nice to be able to collect arrays...
 | ||||||
| 	// XXX should we define a handler.Type handler???
 | 	// XXX should we define a handler.type handler???
 | ||||||
| 	handleArgumentValue: false, | 	handleArgumentValue: false, | ||||||
| 
 | 
 | ||||||
| 	// Handle error exit...
 | 	// Handle error exit...
 | ||||||
| @ -408,6 +427,8 @@ object.Constructor('Parser', { | |||||||
| 
 | 
 | ||||||
| 		this.script = rest[0] | 		this.script = rest[0] | ||||||
| 		this.scriptName = rest.shift().split(/[\\\/]/).pop()  | 		this.scriptName = rest.shift().split(/[\\\/]/).pop()  | ||||||
|  | 		this.scriptPath = this.script.slice(0,  | ||||||
|  | 			this.script.length - this.scriptName.length) | ||||||
| 
 | 
 | ||||||
| 		var opt_pattern = this.optionInputPattern | 		var opt_pattern = this.optionInputPattern | ||||||
| 
 | 
 | ||||||
| @ -428,9 +449,12 @@ object.Constructor('Parser', { | |||||||
| 				value = value  | 				value = value  | ||||||
| 					|| ((handler.arg && !opt_pattern.test(rest[0])) ? | 					|| ((handler.arg && !opt_pattern.test(rest[0])) ? | ||||||
| 							rest.shift() | 							rest.shift() | ||||||
|  | 						// XXX ENV
 | ||||||
|  | 						//: handler.env ?
 | ||||||
|  | 						//	process.env[handler.env]
 | ||||||
| 						: undefined) | 						: undefined) | ||||||
| 				// value conversion...
 | 				// value conversion...
 | ||||||
| 				value = value && this.handleArgumentValue ? | 				value = (value && this.handleArgumentValue) ? | ||||||
| 					this.handleArgumentValue(handler, value) | 					this.handleArgumentValue(handler, value) | ||||||
| 					: value | 					: value | ||||||
| 				// run handler...
 | 				// run handler...
 | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "ig-argv", |   "name": "ig-argv", | ||||||
|   "version": "2.0.2", |   "version": "2.0.3", | ||||||
|   "description": "simple argv parser", |   "description": "simple argv parser", | ||||||
|   "main": "argv.js", |   "main": "argv.js", | ||||||
|   "scripts": { |   "scripts": { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user