now the metadata is acquired from package.json by default...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-08-02 06:44:47 +03:00
parent 13c8c3da6f
commit 1ad023e440
2 changed files with 34 additions and 10 deletions

42
argv.js
View File

@ -130,6 +130,18 @@ function(name, pre, post){
: this) } } : this) } }
var getFromPackage =
module.extra.getFromPackage =
function(attr){
return function(path){
try {
return require(path
|| this.packageJson
|| './package.json')[attr]
} catch(err){
return undefined } } }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Basic argv parser... // Basic argv parser...
@ -301,6 +313,8 @@ object.Constructor('Parser', {
requiredOptionPriority: 80, requiredOptionPriority: 80,
packageJson: undefined,
// instance stuff... // instance stuff...
// XXX do we need all three??? // XXX do we need all three???
@ -508,13 +522,13 @@ object.Constructor('Parser', {
helpValueSeparator: '=', helpValueSeparator: '=',
// doc sections... // doc sections...
author: undefined, author: getFromPackage('author'),
license: undefined, license: getFromPackage('license'),
usage: '$SCRIPTNAME [OPTIONS]', usage: '$SCRIPTNAME [OPTIONS]',
doc: undefined, doc: undefined,
examples: undefined, examples: undefined,
//footer: 'Written by $AUTHOR ($VERSION / $LICENSE).', //footer: undefined,
footer: undefined, footer: 'Written by: $AUTHOR\nVersion: $VERSION / License: $LICENSE',
// NOTE: this supports but does not requires the 'colors' module... // NOTE: this supports but does not requires the 'colors' module...
// XXX should wrap long lines... // XXX should wrap long lines...
@ -528,10 +542,16 @@ object.Constructor('Parser', {
: [a, '\t'.repeat(opts_width)+ prefix + b]) : [a, '\t'.repeat(opts_width)+ prefix + b])
: [a] }, : [a] },
expandTextVars: function(text){ expandTextVars: function(text){
var that = this
var get = function(attr, dfl){
return (typeof(that[attr]) == 'function' ?
that[attr]()
: that[attr])
|| dfl }
return text return text
.replace(/\$AUTHOR/g, this.author || 'Author') .replace(/\$AUTHOR/g, get('author', 'Author'))
.replace(/\$LICENSE/g, this.license || '') .replace(/\$LICENSE/g, get('license', ''))
.replace(/\$VERSION/g, this.version || '0.0.0') .replace(/\$VERSION/g, get('version', '0.0.0'))
.replace(/\$SCRIPTNAME/g, this.scriptName) }, .replace(/\$SCRIPTNAME/g, this.scriptName) },
// NOTE: this will set .quiet to false... // NOTE: this will set .quiet to false...
@ -685,7 +705,8 @@ object.Constructor('Parser', {
// Version... // Version...
// //
// NOTE: this will set .quiet to false... // NOTE: this will set .quiet to false...
version: undefined, //version: undefined,
version: getFromPackage('version'),
'-v': '-version', '-v': '-version',
'-version': { '-version': {
@ -693,7 +714,10 @@ object.Constructor('Parser', {
priority: 99, priority: 99,
handler: function(){ handler: function(){
this.quiet = false this.quiet = false
this.print(this.version || '0.0.0') this.print((typeof(this.version) == 'function' ?
this.version()
: this.version)
|| '0.0.0')
return module.STOP }, }, return module.STOP }, },

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-argv", "name": "ig-argv",
"version": "2.9.4", "version": "2.10.0",
"description": "simple argv parser", "description": "simple argv parser",
"main": "argv.js", "main": "argv.js",
"scripts": { "scripts": {