Skip to content

Commit

Permalink
arglen()
Browse files Browse the repository at this point in the history
  • Loading branch information
shinout committed Mar 7, 2012
1 parent c4d7f77 commit b120c96
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 22 deletions.
78 changes: 56 additions & 22 deletions ArgParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ function ArgParser() {
this._files = [];
this._dirs = [];
this._nums = [];
this._err = null;
this._min = null;
this._max = null;
}

ArgParser.create = function() {
Expand Down Expand Up @@ -124,6 +127,21 @@ ArgParser.prototype.addOptions = function() {
// syntax sugar
ArgParser.prototype.nonvals = ArgParser.prototype.addOptions;

// register function called after error (in parsing)
ArgParser.prototype.err = function(err) {
if (typeof err == "function") this._err = err;
return this;
};


// register argument nums
ArgParser.prototype.arglen = function(min, max) {
if (typeof min == "number") this._min = min;
if (typeof max == "number") this._max = max;
return this;
};


// parse argv
ArgParser.prototype.parse = function(arr) {
/* clear past data */
Expand Down Expand Up @@ -202,29 +220,45 @@ ArgParser.prototype.parse = function(arr) {

var path = require('path'), fs = require('fs');

// check file existence
this._files.forEach(function(v) {
if (typeof v == "string" && this.getOptions(v) === this.emptyValue) return;
var f = (typeof v == "number") ? this.getArgs(v) : this.getOptions(v);
if (f == '-') return;
try{if(!fs.statSync(f).isFile()){throw 1}}catch(e){throw new Error(f + ": no such file or directory (in args " + v + ')');}
}, this);

// check dir existence
this._dirs.forEach(function(v) {
if (typeof v == "string" && this.getOptions(v) === this.emptyValue) return;
var d = (typeof v == "number") ? this.getArgs(v) : this.getOptions(v);
try{if(!fs.statSync(d).isDirectory()){throw 1}}catch(e){throw new Error(d + ": no such file or directory (in args " + v + ')');}
}, this);
try {

// check file existence
this._files.forEach(function(v) {
if (typeof v == "string" && this.getOptions(v) === this.emptyValue) return;
var f = (typeof v == "number") ? this.getArgs(v) : this.getOptions(v);
if (f == '-') return;
try{if(!fs.statSync(f).isFile()){throw 1}}catch(e){throw new Error(f + ": no such file or directory (in args " + v + ')');}
}, this);

// check dir existence
this._dirs.forEach(function(v) {
if (typeof v == "string" && this.getOptions(v) === this.emptyValue) return;
var d = (typeof v == "number") ? this.getArgs(v) : this.getOptions(v);
try{if(!fs.statSync(d).isDirectory()){throw 1}}catch(e){throw new Error(d + ": no such file or directory (in args " + v + ')');}
}, this);

// Numberize
this._nums.forEach(function(v) {
if (typeof v == "string" && this.getOptions(v) === this.emptyValue) return;
var num = Number( (typeof v == "number") ? this.getArgs(v) : this.getOptions(v) );
if (isNaN(num)) throw new Error('the arg ' + v +' must be a number.');
if (typeof v == "number") this._args[v] = num;
else this._options[v] = num;
}, this);

// check argument length
if (this._min != null && this._min > this._args.length)
throw new Error('required at least ' + this._min + ' argument(s)');

if (this._max != null && this._max < this._args.length)
throw new Error('required at most ' + this._max + ' argument(s)');
}
catch (e) {
if (!this._err) throw e;
var ret = this._err(e);
return (ret == undefined) ? false : ret;
}

// Numberize
this._nums.forEach(function(v) {
if (typeof v == "string" && this.getOptions(v) === this.emptyValue) return;
var num = Number( (typeof v == "number") ? this.getArgs(v) : this.getOptions(v) );
if (isNaN(num)) throw new Error('the arg ' + v +' must be a number.');
if (typeof v == "number") this._args[v] = num;
else this._options[v] = num;
}, this);
return this;
};

Expand Down
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ in script.js
- ap.dirs()
- ap.nums()
- ap.defaults()
- ap.err()
- ap.arglen()
- ap.parse(arr)
- ap.opt(op1, op2, ...)
- ap.arg()
Expand Down Expand Up @@ -188,6 +190,41 @@ whose default value is number.
By default, **ap.nums()** are called to these options automatically.


### ap.err(fn) ###
Registers a function called when an error is thrown in parsing.

The argument passed to **fn** is the thrown error.

Return value of the function is the return value of **ap.parse()**.

By default, it returns **false**.

var ap = require('argparser')
.files(0)
.err(function(e) {
console.error(e.message)
console.error("[usage]\n\tnode", __filename, "<file>")
})
.parse();

if (!ap) process.exit();


### ap.arglen(min, max) ###
Registers a limit of argument length.

ArgParser throws an error if **min** is larger than arguments.length.

ArgParser throws an error if **max** is smaller than arguments.length.

We can set just **min** like the following sample.

var ap = require('argparser')
.arglen(3) // three arguments are required
.parse();



### ap.parse(arr) ###
Parses arguments.

Expand Down

0 comments on commit b120c96

Please sign in to comment.