| 
									
										
										
										
											2020-08-25 19:12:21 +03:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var argv = require('../argv') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // XXX chaining parsers...
 | 
					
						
							|  |  |  | // 		what we need:
 | 
					
						
							|  |  |  | // 			- go through all the argv and handle specific args...
 | 
					
						
							|  |  |  | // 			- handle the rest of the args...
 | 
					
						
							|  |  |  | // 		ways to implement this:
 | 
					
						
							|  |  |  | // 			- parser chaining (external)
 | 
					
						
							|  |  |  | // 				- define a parser with -* and @* undefined
 | 
					
						
							|  |  |  | // 				- .then(..) calls the next parser on the unprocessed args
 | 
					
						
							|  |  |  | // 			  ...modular but not sure how to document this...
 | 
					
						
							|  |  |  | // 			- parser chaining (internal)
 | 
					
						
							|  |  |  | // 				- root parser has -* and @* undefined
 | 
					
						
							|  |  |  | // 				- .then(..) calls a special command/arg/group that parser 
 | 
					
						
							|  |  |  | // 					the rest of the args...
 | 
					
						
							|  |  |  | // 					...this can be implemented as a special method/command
 | 
					
						
							|  |  |  | // 					something like .next(..) or .handleRest(..)
 | 
					
						
							|  |  |  | // 			- .chain(<parser>)
 | 
					
						
							| 
									
										
										
										
											2020-08-26 13:57:46 +03:00
										 |  |  | // XXX might be a good idea to flip this around and instead of 
 | 
					
						
							|  |  |  | // 		chaining after do a pre-parse....
 | 
					
						
							| 
									
										
										
										
											2020-08-25 19:12:21 +03:00
										 |  |  | var parser =  | 
					
						
							|  |  |  | exports.parser = | 
					
						
							|  |  |  | argv.Parser({ | 
					
						
							| 
									
										
										
										
											2020-08-26 13:57:46 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		'-x': { | 
					
						
							|  |  |  | 			handler: function(){ | 
					
						
							|  |  |  | 				console.log('### high priority option') }}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// setup...
 | 
					
						
							| 
									
										
										
										
											2020-08-25 19:12:21 +03:00
										 |  |  | 		// XXX can we go without this???
 | 
					
						
							|  |  |  | 		splitOptions: false, | 
					
						
							| 
									
										
										
										
											2020-08-26 13:57:46 +03:00
										 |  |  | 		// pass help through to the chained parser...
 | 
					
						
							| 
									
										
										
										
											2020-08-25 19:12:21 +03:00
										 |  |  | 		'-help': undefined, | 
					
						
							| 
									
										
										
										
											2020-08-26 13:57:46 +03:00
										 |  |  | 		// let all the unknown options pass through...
 | 
					
						
							| 
									
										
										
										
											2020-08-25 19:12:21 +03:00
										 |  |  | 		'-*': undefined, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	// XXX this works but we still need:
 | 
					
						
							|  |  |  | 	// 		- threading back the results
 | 
					
						
							|  |  |  | 	// XXX would also be interesting to be able to route to specific 
 | 
					
						
							|  |  |  | 	// 		chained parsers...
 | 
					
						
							|  |  |  | 	.then(argv.Parser({ | 
					
						
							| 
									
										
										
										
											2020-08-26 13:57:46 +03:00
										 |  |  | 		// used for documentation...
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		// this is handled in the root parser and will never get reached 
 | 
					
						
							|  |  |  | 		// here...
 | 
					
						
							|  |  |  | 		'-x': { | 
					
						
							|  |  |  | 			doc: [ | 
					
						
							|  |  |  | 				'high priority option', | 
					
						
							|  |  |  | 				'this will get processed before', | 
					
						
							|  |  |  | 				'any other options']}, | 
					
						
							| 
									
										
										
										
											2020-08-25 19:12:21 +03:00
										 |  |  | 	})) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 16:38:19 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var parser2 = | 
					
						
							|  |  |  | exports.parser2 = | 
					
						
							|  |  |  | argv.Parser.chain({ | 
					
						
							|  |  |  | 	'-a': { | 
					
						
							|  |  |  | 		doc: [ | 
					
						
							|  |  |  | 			'high priority option', | 
					
						
							|  |  |  | 			'this will get processed before', | 
					
						
							|  |  |  | 			'any other options'], | 
					
						
							|  |  |  | 		handler: function(){ | 
					
						
							|  |  |  | 			console.log('### high priority option') }}, | 
					
						
							|  |  |  | },{ | 
					
						
							|  |  |  | 	'-b': { | 
					
						
							|  |  |  | 		doc: 'medium priority option', | 
					
						
							|  |  |  | 		handler: function(){ | 
					
						
							| 
									
										
										
										
											2020-08-27 16:52:25 +03:00
										 |  |  | 			console.log('### medium priority option') }}, | 
					
						
							| 
									
										
										
										
											2020-08-26 16:38:19 +03:00
										 |  |  | },{ | 
					
						
							|  |  |  | 	'-c': { | 
					
						
							|  |  |  | 		doc: 'normal priority option', | 
					
						
							|  |  |  | 		handler: function(){ | 
					
						
							|  |  |  | 			console.log('### normal priority option') }}, | 
					
						
							| 
									
										
										
										
											2020-12-08 18:33:40 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	'@manual': parser, | 
					
						
							| 
									
										
										
										
											2020-08-26 16:38:19 +03:00
										 |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-25 19:12:21 +03:00
										 |  |  | // run the parser...
 | 
					
						
							|  |  |  | __filename == (require.main || {}).filename | 
					
						
							| 
									
										
										
										
											2020-08-26 16:38:19 +03:00
										 |  |  | 	&& parser2() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-25 19:12:21 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | // vim:set ts=4 sw=4 spell :
 |