mirror of
https://github.com/flynx/argv.js.git
synced 2025-10-28 02:10:08 +00:00
experimenting with parser chaining....
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
d1be185187
commit
4bfc2b19de
9
argv.js
9
argv.js
@ -1155,7 +1155,14 @@ object.Constructor('Parser', {
|
||||
|| parsed.handler(dfl)[1]
|
||||
// no handler found and '-*' or '@*' not defined...
|
||||
if(handler == null){
|
||||
throw ParserError(`Unknown ${ type == 'opt' ? 'option' : 'command:' } $ARG`, arg) }
|
||||
// XXX if nether the whole arg nor it split are found
|
||||
// we need to push the original to unhandled...
|
||||
console.log('!!!!!!!!!!!!!!!!!!!!!', arg)
|
||||
unhandled.push(arg)
|
||||
continue }
|
||||
//throw module.ParserError(
|
||||
// `Unknown ${ type == 'opt' ? 'option' : 'command:' } $ARG`,
|
||||
// arg) }
|
||||
|
||||
// mark/unmark handlers...
|
||||
values.delete(handler)
|
||||
|
||||
47
examples/chain.js
Normal file
47
examples/chain.js
Normal file
@ -0,0 +1,47 @@
|
||||
#!/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>)
|
||||
//
|
||||
var parser =
|
||||
exports.parser =
|
||||
argv.Parser({
|
||||
// XXX can we go without this???
|
||||
splitOptions: false,
|
||||
|
||||
'-help': undefined,
|
||||
|
||||
'-*': undefined,
|
||||
})
|
||||
// XXX this works but we still need:
|
||||
// - threading back the results
|
||||
// - -help
|
||||
// XXX would also be interesting to be able to route to specific
|
||||
// chained parsers...
|
||||
.then(argv.Parser({
|
||||
'-moo': {
|
||||
handler: function(){
|
||||
console.log('MOO!!!') }},
|
||||
}))
|
||||
|
||||
// run the parser...
|
||||
__filename == (require.main || {}).filename
|
||||
&& parser(process.argv)
|
||||
|
||||
// vim:set ts=4 sw=4 spell :
|
||||
5
test.js
5
test.js
@ -32,6 +32,8 @@ test.Setups({
|
||||
return require('./examples/options').parser },
|
||||
lang: function(){
|
||||
return require('./examples/lang').parser },
|
||||
chain: function(){
|
||||
return require('./examples/chain').parser },
|
||||
|
||||
// NOTE: this will also load .bare, .options and .lang
|
||||
basic: function(assert){
|
||||
@ -45,6 +47,8 @@ test.Setups({
|
||||
console.log('>>> VERBOSE:', ...arguments)
|
||||
return 'verbose' },
|
||||
|
||||
'-blank': {},
|
||||
|
||||
'-c': '@command',
|
||||
'@cmd': '@command',
|
||||
'@command': {
|
||||
@ -173,6 +177,7 @@ test.Setups({
|
||||
'@bare': setups.bare(assert),
|
||||
'@opts': setups.options(assert),
|
||||
'@lang': setups.lang(assert),
|
||||
'@chain': setups.chain(assert),
|
||||
|
||||
|
||||
// collision test...
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user