mirror of
https://github.com/flynx/argv.js.git
synced 2025-12-18 09:31:40 +00:00
split out the lang.js example....
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
8b8f9139fb
commit
4699e0434e
8
argv.js
8
argv.js
@ -44,6 +44,10 @@ module.THEN =
|
|||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
|
// NOTE: it would be great to make .message a prop and handle '$ARG'
|
||||||
|
// replacement but JS uses it internally in a non standard way
|
||||||
|
// so the prop is circumvented internally... (XXX)
|
||||||
|
// ...currently the substitution is done in .printError(..)
|
||||||
module.ParserError =
|
module.ParserError =
|
||||||
object.Constructor('ParserError', Error, {
|
object.Constructor('ParserError', Error, {
|
||||||
// NOTE: I do not get why JavaScript's Error implements this
|
// NOTE: I do not get why JavaScript's Error implements this
|
||||||
@ -286,7 +290,7 @@ object.Constructor('Parser', {
|
|||||||
}, {
|
}, {
|
||||||
// config...
|
// config...
|
||||||
//
|
//
|
||||||
// NOTE: this must contain two goups the first is the prefix and the
|
// NOTE: this must contain two groups the first is the prefix and the
|
||||||
// second must contain the option name...
|
// second must contain the option name...
|
||||||
// NOTE: we only care about differentiating an option from a command
|
// NOTE: we only care about differentiating an option from a command
|
||||||
// here by design...
|
// here by design...
|
||||||
@ -851,7 +855,7 @@ object.Constructor('Parser', {
|
|||||||
|
|
||||||
// Post parsing callbacks...
|
// Post parsing callbacks...
|
||||||
//
|
//
|
||||||
// .then(callback(unhandleed, root_value, rest))
|
// .then(callback(unhandled, root_value, rest))
|
||||||
//
|
//
|
||||||
// .stop(callback(arg, rest))
|
// .stop(callback(arg, rest))
|
||||||
// .error(callback(reason, arg, rest))
|
// .error(callback(reason, arg, rest))
|
||||||
|
|||||||
110
examples/lang.js
Normal file
110
examples/lang.js
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
var argv = require('../argv')
|
||||||
|
|
||||||
|
var parser =
|
||||||
|
exports.parser =
|
||||||
|
argv.Parser({
|
||||||
|
// XXX for testing, remove when done...
|
||||||
|
'-echo': function(...args){
|
||||||
|
console.log('ECHO:', ...args)},
|
||||||
|
|
||||||
|
// 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) })
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// vim:set ts=4 sw=4 spell :
|
||||||
109
test.js
109
test.js
@ -16,6 +16,7 @@ var argv = require('./argv')
|
|||||||
|
|
||||||
var bare = module.bare = require('./examples/bare').parser
|
var bare = module.bare = require('./examples/bare').parser
|
||||||
var options = module.options = require('./examples/options').parser
|
var options = module.options = require('./examples/options').parser
|
||||||
|
var lang = module.lang = require('./examples/lang').parser
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -158,6 +159,8 @@ argv.Parser({
|
|||||||
'@bare': bare,
|
'@bare': bare,
|
||||||
'@opts': options,
|
'@opts': options,
|
||||||
|
|
||||||
|
'@lang': lang,
|
||||||
|
|
||||||
|
|
||||||
// collision test...
|
// collision test...
|
||||||
// NOTE: values of these will shadow the API...
|
// NOTE: values of these will shadow the API...
|
||||||
@ -176,112 +179,6 @@ argv.Parser({
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
var lang =
|
|
||||||
module.lang =
|
|
||||||
argv.Parser({
|
|
||||||
// handle both +x and -x
|
|
||||||
optionInputPattern: /^([+-])\1?([^+-].*|)$/,
|
|
||||||
|
|
||||||
// XXX for testing, remove when done...
|
|
||||||
'-echo': function(...args){
|
|
||||||
console.log('ECHO:', ...args)},
|
|
||||||
|
|
||||||
// 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) })
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
console.log(' ->', p(['test', '--verbose', 'a', 'b', 'c']))
|
console.log(' ->', p(['test', '--verbose', 'a', 'b', 'c']))
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user