Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2025-03-31 02:14:12 +03:00
parent a4a2a29db2
commit 262c5a3763
2 changed files with 1792 additions and 1792 deletions

3102
argv.js

File diff suppressed because it is too large Load Diff

482
test.js
View File

@ -1,241 +1,241 @@
#!/usr/bin/env node #!/usr/bin/env node
/********************************************************************** /**********************************************************************
* *
* *
* *
**********************************************************************/ **********************************************************************/
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
(function(require){ var module={} // make module AMD/node compatible... (function(require){ var module={} // make module AMD/node compatible...
/*********************************************************************/ /*********************************************************************/
var colors = require('colors') var colors = require('colors')
var object = require('ig-object') var object = require('ig-object')
var test = require('ig-test') var test = require('ig-test')
var argv = require('./argv') var argv = require('./argv')
//--------------------------------------------------------------------- //---------------------------------------------------------------------
argv.Parser.typeHandlers.error = function(){ argv.Parser.typeHandlers.error = function(){
throw new argv.ParserTypeError('type error') } throw new argv.ParserTypeError('type error') }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
var setups = var setups =
test.Setups({ test.Setups({
bare: function(){ bare: function(){
return require('./examples/bare').parser }, return require('./examples/bare').parser },
opts: function(){ opts: function(){
return require('./examples/options').parser }, return require('./examples/options').parser },
lang: function(){ lang: function(){
return require('./examples/lang').parser }, return require('./examples/lang').parser },
chain: function(){ chain: function(){
return require('./examples/chain').parser }, return require('./examples/chain').parser },
// NOTE: this will also load .bare, .opts and .lang // NOTE: this will also load .bare, .opts and .lang
basic: function(assert){ basic: function(assert){
return argv.Parser({ return argv.Parser({
examples: [ examples: [
'$SCRIPTNAME moo foo boo' '$SCRIPTNAME moo foo boo'
], ],
// disable exit on error... // disable exit on error...
handleErrorExit: false, handleErrorExit: false,
'@help': '-help', '@help': '-help',
'-verbose': function(){ '-verbose': function(){
console.log('>>> VERBOSE:', ...arguments) console.log('>>> VERBOSE:', ...arguments)
return 'verbose' }, return 'verbose' },
'-blank': {}, '-blank': {},
'-c': '@command', '-c': '@command',
'@cmd': '@command', '@cmd': '@command',
'@command': { '@command': {
priority: -50, priority: -50,
handler: function(){ handler: function(){
console.log('>>> COMMAND:', ...arguments) console.log('>>> COMMAND:', ...arguments)
return 'command' }, return 'command' },
}, },
'-r': '-required', '-r': '-required',
'-required': { '-required': {
doc: 'Required option', doc: 'Required option',
required: true, required: true,
}, },
'-prefix': { '-prefix': {
doc: 'prefix test', doc: 'prefix test',
handler: function(opts, key, value){ handler: function(opts, key, value){
console.log('PREFIX:', key[0]) }, }, console.log('PREFIX:', key[0]) }, },
'-value': { '-value': {
doc: 'Value option', doc: 'Value option',
arg: 'VALUE | valueValue', arg: 'VALUE | valueValue',
default: 333, default: 333,
}, },
'-c': '-collection', '-c': '-collection',
'-collection': { '-collection': {
doc: 'collect ELEM', doc: 'collect ELEM',
arg: 'ELEM | elems', arg: 'ELEM | elems',
collect: 'set', collect: 'set',
}, },
'-t': '-toggle', '-t': '-toggle',
'-toggle': { '-toggle': {
doc: 'toggle value', doc: 'toggle value',
arg: '| toggle_value', arg: '| toggle_value',
collect: 'toggle', collect: 'toggle',
}, },
'-s': '-string', '-s': '-string',
'-string': { '-string': {
doc: 'collect tab-separated strings', doc: 'collect tab-separated strings',
arg: 'STR | str', arg: 'STR | str',
collect: 'string|\t', collect: 'string|\t',
}, },
//'-a': '-ab', //'-a': '-ab',
'-sh': { '-sh': {
doc: 'short option', }, doc: 'short option', },
'-env': { '-env': {
doc: 'env value', doc: 'env value',
arg: 'VALUE | env_value', arg: 'VALUE | env_value',
env: 'VALUE', env: 'VALUE',
//default: 5, //default: 5,
handler: function(args, key, value){ console.log('GOT ENV:', value) }, handler: function(args, key, value){ console.log('GOT ENV:', value) },
}, },
'-type-error': { '-type-error': {
doc: 'throw a type error', doc: 'throw a type error',
type: 'error', type: 'error',
}, },
'-error': { '-error': {
doc: 'throw an error', doc: 'throw an error',
handler: function(){ handler: function(){
throw argv.ParserError('error: $ARG') }}, throw argv.ParserError('error: $ARG') }},
'-passive-error': { '-passive-error': {
doc: 'throw an error', doc: 'throw an error',
handler: function(){ handler: function(){
return argv.ParserError('error') }}, return argv.ParserError('error') }},
'-test': argv.Parser({ '-test': argv.Parser({
env: 'TEST', env: 'TEST',
arg: 'TEST', arg: 'TEST',
default: function(){ default: function(){
return this['-value'].default }, return this['-value'].default },
}).then(function(){ }).then(function(){
console.log('TEST', ...arguments) }), console.log('TEST', ...arguments) }),
'-i': '-int', '-i': '-int',
'-int': { '-int': {
arg: 'INT|int', arg: 'INT|int',
type: 'int', type: 'int',
valueRequired: true, valueRequired: true,
}, },
'@nested': argv.Parser({ '@nested': argv.Parser({
doc: 'nested parser.', doc: 'nested parser.',
'@nested': argv.Parser({ '@nested': argv.Parser({
doc: 'nested nested parser.', doc: 'nested nested parser.',
}).then(function(){ }).then(function(){
console.log('NESTED NESTED DONE', ...arguments)}), console.log('NESTED NESTED DONE', ...arguments)}),
}).then(function(){ }).then(function(){
console.log('NESTED DONE', ...arguments) }), console.log('NESTED DONE', ...arguments) }),
'-n': { '-n': {
doc: 'proxy to nested', doc: 'proxy to nested',
handler: function(){ handler: function(){
return this.handle('nested', ...arguments) }, }, return this.handle('nested', ...arguments) }, },
'-\\*': { '-\\*': {
handler: function(){ handler: function(){
console.log('-\\*:', ...arguments) } }, console.log('-\\*:', ...arguments) } },
//'@*': undefined, //'@*': undefined,
// these aliases will not get shown... // these aliases will not get shown...
// dead-end alias... // dead-end alias...
'-d': '-dead-end', '-d': '-dead-end',
// alias loops... // alias loops...
// XXX should we detect and complain about these??? // XXX should we detect and complain about these???
// ...maybe in a test function?? // ...maybe in a test function??
'-z': '-z', '-z': '-z',
'-x': '-y', '-x': '-y',
'-y': '-x', '-y': '-x',
'-k': '-l', '-k': '-l',
'-l': '-m', '-l': '-m',
'-m': '-k', '-m': '-k',
'@bare': setups.bare(assert), '@bare': setups.bare(assert),
'@opts': setups.opts(assert), '@opts': setups.opts(assert),
'@lang': setups.lang(assert), '@lang': setups.lang(assert),
'@chain': setups.chain(assert), '@chain': setups.chain(assert),
// collision test... // collision test...
// NOTE: values of these will shadow the API... // NOTE: values of these will shadow the API...
// XXX need to either warn the user of this or think of a // XXX need to either warn the user of this or think of a
// way to avoid this... // way to avoid this...
'@options': {}, '@options': {},
'-handler': {}, '-handler': {},
}) })
//.print(function(...args){ //.print(function(...args){
// console.log('----\n', ...args) // console.log('----\n', ...args)
// return argv.STOP }) // return argv.STOP })
.then(function(){ .then(function(){
console.log('DONE', ...arguments) }) console.log('DONE', ...arguments) })
.stop(function(){ .stop(function(){
console.log('STOP', ...arguments) }) console.log('STOP', ...arguments) })
.error(function(){ .error(function(){
console.log('ERROR', ...arguments) }) } console.log('ERROR', ...arguments) }) }
}) })
test.Modifiers({ test.Modifiers({
}) })
test.Tests({ test.Tests({
}) })
test.Cases({ test.Cases({
}) })
/* /*
console.log(' ->', p(['test', '--verbose', 'a', 'b', 'c'])) console.log(' ->', p(['test', '--verbose', 'a', 'b', 'c']))
console.log(' ->', p(['test', '-c', 'a', 'b', 'c'])) console.log(' ->', p(['test', '-c', 'a', 'b', 'c']))
console.log(' ->', p(['test', 'command', 'a', 'b', 'c'])) console.log(' ->', p(['test', 'command', 'a', 'b', 'c']))
console.log('---') console.log('---')
p(['test', 'nested', '-h']) p(['test', 'nested', '-h'])
p(['test', '-h']) p(['test', '-h'])
//*/ //*/
//--------------------------------------------------------------------- //---------------------------------------------------------------------
typeof(__filename) != 'undefined' typeof(__filename) != 'undefined'
&& __filename == (require.main || {}).filename && __filename == (require.main || {}).filename
&& console.log(setups.basic()()) && console.log(setups.basic()())
//&& test.run() //&& test.run()
/********************************************************************** /**********************************************************************
* vim:set ts=4 sw=4 : */ return module }) * vim:set ts=4 sw=4 : */ return module })