| 
									
										
										
										
											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-08-01 19:25:31 +03:00
										 |  |  | var bare = module.bare = require('./examples/bare').parser | 
					
						
							|  |  |  | var options = module.options = require('./examples/options').parser | 
					
						
							| 
									
										
										
										
											2020-08-02 02:50:54 +03:00
										 |  |  | var lang = module.lang = require('./examples/lang').parser | 
					
						
							| 
									
										
										
										
											2020-08-01 19:25:31 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-14 03:10:19 +03:00
										 |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 01:26:42 +03:00
										 |  |  | argv.Parser.typeHandlers.error = function(){ | 
					
						
							|  |  |  | 	throw new argv.ParserTypeError('type error') } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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', | 
					
						
							| 
									
										
										
										
											2020-07-29 17:59:03 +03:00
										 |  |  | 			arg: '| toggle_value', | 
					
						
							| 
									
										
										
										
											2020-07-26 01:51:36 +03:00
										 |  |  | 			collect: 'toggle', | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-07-27 19:02:43 +03:00
										 |  |  | 		'-s': '-string', | 
					
						
							|  |  |  | 		'-string': { | 
					
						
							| 
									
										
										
										
											2020-07-28 03:09:12 +03:00
										 |  |  | 			doc: 'collect tab-separated strings', | 
					
						
							| 
									
										
										
										
											2020-07-27 19:02:43 +03:00
										 |  |  | 			arg: 'STR | str', | 
					
						
							|  |  |  | 			collect: 'string|\t', | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-07-27 19:33:57 +03:00
										 |  |  | 		//'-a': '-ab',
 | 
					
						
							|  |  |  | 		'-sh': { | 
					
						
							|  |  |  | 			doc: 'short option', }, | 
					
						
							| 
									
										
										
										
											2020-07-25 21:48:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 19:18:44 +03:00
										 |  |  | 		'-env': { | 
					
						
							|  |  |  | 			doc: 'env value', | 
					
						
							|  |  |  | 			arg: 'VALUE | env_value', | 
					
						
							|  |  |  | 			env: 'VALUE', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			//default: 5,
 | 
					
						
							|  |  |  | 			handler: function(args, key, value){ console.log('GOT ENV:', value) }, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 01:26:42 +03:00
										 |  |  | 		'-type-error': { | 
					
						
							| 
									
										
										
										
											2020-07-28 03:09:12 +03:00
										 |  |  | 			doc: 'throw a type error', | 
					
						
							| 
									
										
										
										
											2020-07-28 01:26:42 +03:00
										 |  |  | 			type: 'error',  | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		'-error': { | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 			doc: 'throw an error', | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							| 
									
										
										
										
											2020-08-01 19:25:31 +03:00
										 |  |  | 				throw argv.ParserError('error: $ARG') }}, | 
					
						
							| 
									
										
										
										
											2020-07-29 22:30:43 +03:00
										 |  |  | 		'-passive-error': { | 
					
						
							| 
									
										
										
										
											2020-07-28 01:26:42 +03:00
										 |  |  | 			doc: 'throw an error', | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							| 
									
										
										
										
											2020-07-28 03:09:12 +03:00
										 |  |  | 				return argv.ParserError('error') }}, | 
					
						
							| 
									
										
										
										
											2020-07-28 01:26:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-08-01 19:25:31 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		'@bare': bare, | 
					
						
							|  |  |  | 		'@opts': options, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 02:50:54 +03:00
										 |  |  | 		'@lang': lang, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 19:25:31 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// collision test...
 | 
					
						
							|  |  |  | 		// NOTE: values of these will shadow the API...
 | 
					
						
							|  |  |  | 		'@options': {}, | 
					
						
							|  |  |  | 		'-handler': {}, | 
					
						
							| 
									
										
										
										
											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-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 }) |