Skip to content

Commit

Permalink
Rename function for validation and processing value: validation() -> …
Browse files Browse the repository at this point in the history
…val().
  • Loading branch information
veged committed Aug 30, 2011
1 parent 48d2955 commit bb38ef5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions lib/arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 8 additions & 7 deletions lib/opt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
/**
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/arg.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions src/opt.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/val.js
Original file line number Diff line number Diff line change
@@ -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']);

0 comments on commit bb38ef5

Please sign in to comment.