Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rename function for validation and processing value: validation() -> …

…val().
  • Loading branch information...
commit bb38ef51188e8a6bc5c56cd20f7f96ab5731243b 1 parent 48d2955
@veged authored
View
16 README.md
@@ -29,7 +29,7 @@ require('coa').Cmd() // main (top level) command declaration
.opt()
.name('input').title('input file, required')
.short('i').long('input')
- .validate(function(v) { // validator function, also for translate simple values
+ .val(function(v) { // validator function, also for translate simple values
return require('fs').createReadStream(v) })
.required() // make option required
.end() // end option chain and return to command
@@ -158,10 +158,11 @@ Otherwise, the value will be used by the latter passed.<br>
Makes an option required.<br>
**@returns** *COA.Opt* `this` instance (for chainability)
-#### Opt.validate
-Set a validation function for option.<br>
+#### Opt.val
+Set a validation (or value) function for argument.<br>
Value from command line passes through before becoming available from API.<br>
-**@param** *Function* `_validate` validating function,
+Using for validation and convertion simple types to any values.<br>
+**@param** *Function* `_val` validating function,
invoked in the context of option instance
and has one parameter with value from command line<br>
**@returns** *COA.Opt* `this` instance (for chainability)
@@ -216,10 +217,11 @@ Otherwise, the value will be used by the latter passed.<br>
Makes an argument required.<br>
**@returns** *COA.Arg* `this` instance (for chainability)
-#### Arg.validate
-Set a validation function for argument.<br>
+#### Arg.val
+Set a validation (or value) function for argument.<br>
Value from command line passes through before becoming available from API.<br>
-**@param** *Function* `_validate` validating function,
+Using for validation and convertion simple types to any values.<br>
+**@param** *Function* `_val` validating function,
invoked in the context of argument instance
and has one parameter with value from command line<br>
**@returns** *COA.Arg* `this` instance (for chainability)
View
7 lib/arg.js
@@ -40,14 +40,15 @@ exports.Arg = Arg = (function() {
*/
Arg.prototype.required = Opt.prototype.required;
/**
- Set a validation function for argument.
+ Set a validation (or value) function for argument.
Value from command line passes through before becoming available from API.
- @param {Function} _validate validating function,
+ Using for validation and convertion simple types to any values.
+ @param {Function} _val validating function,
invoked in the context of argument instance
and has one parameter with value from command line
@returns {COA.Arg} this instance (for chainability)
*/
- Arg.prototype.validate = Opt.prototype.validate;
+ Arg.prototype.val = Opt.prototype.val;
/**
Set a default value for argument.
Default value passed through validation function as ordinary value.
View
15 lib/opt.js
@@ -75,15 +75,16 @@ exports.Opt = Opt = (function() {
return this;
};
/**
- Set a validation function for option.
+ Set a validation (or value) function for option.
Value from command line passes through before becoming available from API.
- @param {Function} _validate validating function,
+ Using for validation and convertion simple types to any values.
+ @param {Function} _val validating function,
invoked in the context of option instance
and has one parameter with value from command line
@returns {COA.Opt} this instance (for chainability)
*/
- Opt.prototype.validate = function(_validate) {
- this._validate = _validate;
+ Opt.prototype.val = function(_val) {
+ this._val = _val;
return this;
};
/**
@@ -102,7 +103,7 @@ exports.Opt = Opt = (function() {
@returns {COA.Opt} this instance (for chainability)
*/
Opt.prototype.output = function() {
- return this.def(process.stdout).validate(function(v) {
+ return this.def(process.stdout).val(function(v) {
if (typeof v === 'string') {
if (v === '-') {
return process.stdout;
@@ -139,8 +140,8 @@ exports.Opt = Opt = (function() {
};
Opt.prototype._saveVal = function(opts, val) {
var _name;
- if (this._validate) {
- val = this._validate(val);
+ if (this._val) {
+ val = this._val(val);
}
if (this._arr) {
(opts[_name = this._name] || (opts[_name] = [])).push(val);
View
7 src/arg.coffee
@@ -44,14 +44,15 @@ exports.Arg = class Arg
required: Opt::required
###*
- Set a validation function for argument.
+ Set a validation (or value) function for argument.
Value from command line passes through before becoming available from API.
- @param {Function} _validate validating function,
+ Using for validation and convertion simple types to any values.
+ @param {Function} _val validating function,
invoked in the context of argument instance
and has one parameter with value from command line
@returns {COA.Arg} this instance (for chainability)
###
- validate: Opt::validate
+ val: Opt::val
###*
Set a default value for argument.
View
11 src/opt.coffee
@@ -70,14 +70,15 @@ exports.Opt = class Opt
@
###*
- Set a validation function for option.
+ Set a validation (or value) function for option.
Value from command line passes through before becoming available from API.
- @param {Function} _validate validating function,
+ Using for validation and convertion simple types to any values.
+ @param {Function} _val validating function,
invoked in the context of option instance
and has one parameter with value from command line
@returns {COA.Opt} this instance (for chainability)
###
- validate: (@_validate) -> @
+ val: (@_val) -> @
###*
Set a default value for option.
@@ -95,7 +96,7 @@ exports.Opt = class Opt
output: ->
@
.def(process.stdout)
- .validate (v) ->
+ .val (v) ->
if typeof v is 'string'
if v is '-'
process.stdout
@@ -122,7 +123,7 @@ exports.Opt = class Opt
@
_saveVal: (opts, val) ->
- if @_validate then val = @_validate val
+ if @_val then val = @_val val
if @_arr
(opts[@_name] or= []).push val
else
View
13 tests/val.js
@@ -0,0 +1,13 @@
+require('../lib/coa').Cmd()
+ .name('val')
+ .title('Val test')
+ .helpful()
+ .opt()
+ .name('bla').title('Bla')
+ .short('b').long('bla')
+ .val(function(v) { return { value: v } })
+ .act(function(opts) {
+ console.log(opts.bla);
+ })
+ .end()
+ .parse(['--bla=blabla']);
Please sign in to comment.
Something went wrong with that request. Please try again.