| 
									
										
										
										
											2020-06-14 03:10:19 +03:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | **********************************************************************/ | 
					
						
							|  |  |  | ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) | 
					
						
							|  |  |  | (function(require){ var module={} // make module AMD/node compatible...
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var colors = require('colors') | 
					
						
							|  |  |  | var object = require('ig-object') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var argv = require('./argv') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 15:48:43 +03:00
										 |  |  | var p =  | 
					
						
							|  |  |  | module.p = | 
					
						
							|  |  |  | argv.Parser({ | 
					
						
							| 
									
										
										
										
											2020-06-25 18:31:06 +03:00
										 |  |  | 		// disable exit on error...
 | 
					
						
							|  |  |  | 		handleErrorExit: false, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 18:31:58 +03:00
										 |  |  | 		'@help': '-help', | 
					
						
							| 
									
										
										
										
											2020-06-16 15:48:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 18:31:58 +03:00
										 |  |  | 		'-verbose': function(){ | 
					
						
							|  |  |  | 			console.log('>>> VERBOSE:', ...arguments) | 
					
						
							|  |  |  | 			return 'verbose' | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-06-16 15:48:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 18:31:58 +03:00
										 |  |  | 		'-c': '@command', | 
					
						
							|  |  |  | 		'@cmd': '@command', | 
					
						
							|  |  |  | 		'@command': { | 
					
						
							|  |  |  | 			priority: -50, | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							|  |  |  | 				console.log('>>> COMMAND:', ...arguments) | 
					
						
							|  |  |  | 				return 'command' | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-06-16 15:48:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 03:53:32 +03:00
										 |  |  | 		'-r': '-required', | 
					
						
							|  |  |  | 		'-required': { | 
					
						
							|  |  |  | 			doc: 'Required option', | 
					
						
							|  |  |  | 			required: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-20 03:05:11 +03:00
										 |  |  | 		'-prefix': { | 
					
						
							|  |  |  | 			doc: 'prefix test', | 
					
						
							|  |  |  | 			handler: function(opts, key, value){ | 
					
						
							|  |  |  | 				console.log('PREFIX:', key[0]) }, }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 00:28:28 +03:00
										 |  |  | 		'-value': { | 
					
						
							|  |  |  | 			doc: 'Value option', | 
					
						
							|  |  |  | 			arg: 'VALUE | valueValue', | 
					
						
							|  |  |  | 			default: 333, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-25 21:48:22 +03:00
										 |  |  | 		'-c': '-collection', | 
					
						
							|  |  |  | 		'-collection': { | 
					
						
							| 
									
										
										
										
											2020-07-26 01:51:36 +03:00
										 |  |  | 			doc: 'collect ELEM', | 
					
						
							| 
									
										
										
										
											2020-07-25 21:48:22 +03:00
										 |  |  | 			arg: 'ELEM | elems', | 
					
						
							|  |  |  | 			collect: 'set', | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-07-26 01:51:36 +03:00
										 |  |  | 		'-t': '-toggle', | 
					
						
							|  |  |  | 		'-toggle': { | 
					
						
							|  |  |  | 			doc: 'toggle value', | 
					
						
							|  |  |  | 			arg: '| toggle', | 
					
						
							|  |  |  | 			collect: 'toggle', | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-07-27 19:02:43 +03:00
										 |  |  | 		'-s': '-string', | 
					
						
							|  |  |  | 		'-string': { | 
					
						
							|  |  |  | 			doc: 'collect STR', | 
					
						
							|  |  |  | 			arg: 'STR | str', | 
					
						
							|  |  |  | 			collect: 'string|\t', | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-07-25 21:48:22 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 16:24:40 +03:00
										 |  |  | 		'-test': argv.Parser({ | 
					
						
							|  |  |  | 			env: 'TEST', | 
					
						
							| 
									
										
										
										
											2020-07-03 03:53:32 +03:00
										 |  |  | 			arg: 'TEST', | 
					
						
							| 
									
										
										
										
											2020-07-02 02:17:28 +03:00
										 |  |  | 			default: 'moo', | 
					
						
							| 
									
										
										
										
											2020-06-29 16:24:40 +03:00
										 |  |  | 		}).then(function(){ | 
					
						
							|  |  |  | 			console.log('TEST', ...arguments) }), | 
					
						
							| 
									
										
										
										
											2020-06-16 18:31:58 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-08 03:58:29 +03:00
										 |  |  | 		'-i': '-int', | 
					
						
							| 
									
										
										
										
											2020-07-06 20:31:20 +03:00
										 |  |  | 		'-int': { | 
					
						
							|  |  |  | 			arg: 'INT|int', | 
					
						
							|  |  |  | 			type: 'int', | 
					
						
							|  |  |  | 			valueRequired: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 18:31:58 +03:00
										 |  |  | 		'@nested': argv.Parser({ | 
					
						
							|  |  |  | 			doc: 'nested parser.', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			'@nested': argv.Parser({ | 
					
						
							|  |  |  | 				doc: 'nested nested parser.', | 
					
						
							| 
									
										
										
										
											2020-06-29 14:05:55 +03:00
										 |  |  | 			}).then(function(){ | 
					
						
							|  |  |  | 				console.log('NESTED NESTED DONE', ...arguments)}), | 
					
						
							|  |  |  | 		}).then(function(){ | 
					
						
							|  |  |  | 			console.log('NESTED DONE', ...arguments) }), | 
					
						
							| 
									
										
										
										
											2020-06-28 02:13:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-25 21:48:22 +03:00
										 |  |  | 		'-n': { | 
					
						
							|  |  |  | 			doc: 'proxy to nested', | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							|  |  |  | 				return this.handle('nested', ...arguments) }, }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 20:31:20 +03:00
										 |  |  | 		'-\\*': { | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							|  |  |  | 				console.log('-\\*:', ...arguments) } }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 22:04:47 +03:00
										 |  |  | 		//'@*': undefined,
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 02:13:43 +03:00
										 |  |  | 		// these aliases will not get shown...
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// dead-end alias...
 | 
					
						
							|  |  |  | 		'-d': '-dead-end', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// alias loops...
 | 
					
						
							| 
									
										
										
										
											2020-07-24 14:45:32 +03:00
										 |  |  | 		// XXX should we detect and complain about these???
 | 
					
						
							|  |  |  | 		// 		...maybe in a test function??
 | 
					
						
							| 
									
										
										
										
											2020-06-28 02:13:43 +03:00
										 |  |  | 		'-z': '-z', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		'-x': '-y', | 
					
						
							|  |  |  | 		'-y': '-x', | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-08 03:58:29 +03:00
										 |  |  | 		'-k': '-l', | 
					
						
							|  |  |  | 		'-l': '-m', | 
					
						
							|  |  |  | 		'-m': '-k', | 
					
						
							| 
									
										
										
										
											2020-06-16 18:31:58 +03:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-07-08 03:58:29 +03:00
										 |  |  | 	//.print(function(...args){
 | 
					
						
							|  |  |  | 	//	console.log('----\n', ...args)
 | 
					
						
							|  |  |  | 	//	return argv.STOP })
 | 
					
						
							| 
									
										
										
										
											2020-06-16 18:31:58 +03:00
										 |  |  | 	.then(function(){ | 
					
						
							| 
									
										
										
										
											2020-06-29 02:51:06 +03:00
										 |  |  | 		console.log('DONE', ...arguments) }) | 
					
						
							| 
									
										
										
										
											2020-06-16 18:31:58 +03:00
										 |  |  | 	.stop(function(){ | 
					
						
							| 
									
										
										
										
											2020-07-01 17:28:10 +03:00
										 |  |  | 		console.log('STOP', ...arguments) }) | 
					
						
							| 
									
										
										
										
											2020-06-16 18:31:58 +03:00
										 |  |  | 	.error(function(){ | 
					
						
							| 
									
										
										
										
											2020-07-01 17:28:10 +03:00
										 |  |  | 		console.log('ERROR', ...arguments) }) | 
					
						
							| 
									
										
										
										
											2020-06-15 17:17:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 15:48:43 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-15 04:36:48 +03:00
										 |  |  | var lang = | 
					
						
							|  |  |  | module.lang = | 
					
						
							|  |  |  | argv.Parser({ | 
					
						
							| 
									
										
										
										
											2020-07-17 05:34:51 +03:00
										 |  |  | 	// handle both +x and -x
 | 
					
						
							| 
									
										
										
										
											2020-07-18 20:17:15 +03:00
										 |  |  | 	optionInputPattern: /^([+-])\1?([^+-].*|)$/, | 
					
						
							| 
									
										
										
										
											2020-07-17 05:34:51 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// XXX for testing, remove when done...
 | 
					
						
							|  |  |  | 	'-echo': function(...args){ | 
					
						
							|  |  |  | 		console.log('ECHO:', ...args)}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-15 04:36:48 +03:00
										 |  |  | 	// helpers...
 | 
					
						
							|  |  |  | 	push: function(...items){ | 
					
						
							|  |  |  | 		this.unhandled.splice(this.unhandled.length, 0, ...items)  | 
					
						
							|  |  |  | 		return this }, | 
					
						
							|  |  |  | 	exec: function(...items){ | 
					
						
							|  |  |  | 		this.rest.splice(0, 0, ...items)  | 
					
						
							|  |  |  | 		return this },  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pre_ns: argv.Parser({ | 
					
						
							|  |  |  | 	}), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX do not like the split namespaces....
 | 
					
						
							|  |  |  | 	ns: { | 
					
						
							|  |  |  | 		'[': [ 'blockto', '[:]' ], | 
					
						
							|  |  |  | 		'(': [ 'blockto', ')', 'exec' ], | 
					
						
							|  |  |  | 		'quote': [ 'quotenn', '0', '1' ], | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	'@*': function(code, value){ | 
					
						
							|  |  |  | 		this.unhandled.push(...( | 
					
						
							|  |  |  | 			// type-convert...
 | 
					
						
							|  |  |  | 			/^[+-]?[0-9]+$/.test(value) ? | 
					
						
							|  |  |  | 				[parseInt(value)] | 
					
						
							|  |  |  | 			: /^[+-]?[0-9.]+$/.test(value) ? | 
					
						
							|  |  |  | 				[parseFloat(value)] | 
					
						
							|  |  |  | 			// call user macros...
 | 
					
						
							|  |  |  | 			: value in this.ns ? | 
					
						
							|  |  |  | 				(this.exec(...this.ns[value]), []) | 
					
						
							|  |  |  | 			// unhandled...
 | 
					
						
							|  |  |  | 			: [value])) }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX hanck...
 | 
					
						
							|  |  |  | 	'@quotenn': function(code){ | 
					
						
							|  |  |  | 		var skip = code.shift() | 
					
						
							|  |  |  | 		var quote = code.shift() | 
					
						
							|  |  |  | 		this.push(...code.splice(skip, quote)) }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX this needs blocks to be already made...
 | 
					
						
							|  |  |  | 	//	:: ( | name code -- | )
 | 
					
						
							|  |  |  | 	'@::': function(code){ | 
					
						
							|  |  |  | 		this.ns[code.shift()] = code.shift() }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX revise...
 | 
					
						
							|  |  |  | 	// 	groupb ( B | .. B -- | [ .. ])
 | 
					
						
							|  |  |  | 	// 	groupb ( A:B | .. A .. B .. B -- | [ .. [ .. ] .. ])
 | 
					
						
							|  |  |  | 	'@blockto': function(code, do_pack, value){ | 
					
						
							|  |  |  | 		value = value || code.shift() | 
					
						
							|  |  |  | 		value = value instanceof Array ? | 
					
						
							|  |  |  | 			value | 
					
						
							|  |  |  | 			: value.split(':') | 
					
						
							|  |  |  | 		var [f, t] = value.length == 1 ?  | 
					
						
							|  |  |  | 			[undefined, ...value]  | 
					
						
							|  |  |  | 			: value | 
					
						
							|  |  |  | 		var pack = [] | 
					
						
							|  |  |  | 		var cur = code.shift() | 
					
						
							|  |  |  | 		while(code.length > 0 && cur != t){ | 
					
						
							|  |  |  | 			cur = cur == f ? | 
					
						
							|  |  |  | 				this['@blockto'](code, false, value) | 
					
						
							|  |  |  | 				: cur | 
					
						
							|  |  |  | 			pack.push(cur)  | 
					
						
							|  |  |  | 			cur = code.shift() } | 
					
						
							|  |  |  | 		do_pack !== false | 
					
						
							|  |  |  | 			&& code.unshift(pack) | 
					
						
							|  |  |  | 		return pack }, | 
					
						
							|  |  |  | 	'@exec': function(code){ | 
					
						
							|  |  |  | 		var c = this.unhandled.pop() | 
					
						
							|  |  |  | 		code.splice(0, 0, ...(c instanceof Array ? c : [c])) }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	'@exit': '-', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	'@dup': function(){ | 
					
						
							|  |  |  | 		this.push(...this.unhandled.slice(-1)) }, | 
					
						
							|  |  |  | 	'@dup2': function(){ | 
					
						
							|  |  |  | 		this.push(...this.unhandled.slice(-2)) }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	'@print': function(){ | 
					
						
							|  |  |  | 		this.print(this.unhandled.pop()) }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	'@add': function(){ | 
					
						
							|  |  |  | 		var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] | 
					
						
							|  |  |  | 		this.unhandled.push(a + b) }, | 
					
						
							|  |  |  | 	'@sub': function(){ | 
					
						
							|  |  |  | 		var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] | 
					
						
							|  |  |  | 		this.unhandled.push(a - b) }, | 
					
						
							|  |  |  | 	'@mul': function(){ | 
					
						
							|  |  |  | 		var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] | 
					
						
							|  |  |  | 		this.unhandled.push(a * b) }, | 
					
						
							|  |  |  | 	'@div': function(){ | 
					
						
							|  |  |  | 		var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] | 
					
						
							|  |  |  | 		this.unhandled.push(a / b) }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | .then(function(){ | 
					
						
							|  |  |  | 	this.print('>>>', this.unhandled) }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 15:48:43 +03:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2020-06-15 17:17:04 +03:00
										 |  |  | console.log('  ->', p(['test', '--verbose', 'a', 'b', 'c'])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.log('  ->', p(['test', '-c', 'a', 'b', 'c'])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.log('  ->', p(['test', 'command', 'a', 'b', 'c'])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.log('---') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 15:48:43 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | p(['test', 'nested', '-h']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-15 17:17:04 +03:00
										 |  |  | p(['test', '-h']) | 
					
						
							| 
									
										
										
										
											2020-06-16 15:48:43 +03:00
										 |  |  | //*/
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 03:18:27 +03:00
										 |  |  | typeof(__filename) != 'undefined' | 
					
						
							|  |  |  | 	&& __filename == (require.main || {}).filename | 
					
						
							| 
									
										
										
										
											2020-07-25 21:48:22 +03:00
										 |  |  | 	&& console.log(p()) | 
					
						
							|  |  |  | 	//&& console.log(lang())
 | 
					
						
							| 
									
										
										
										
											2020-06-14 03:10:19 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                               */ return module }) |