Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Function argument validation/processing for CoffeeScript
tree: 877544f838

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
src
README.mdown
package.json
test.coffee

README.mdown

Argon

Argon handles common function argument-related tasks for you. It's written in CoffeeScript (also used for all of the examples below) and runs under Node.js (browser compatibility is planned for the future).

Installation

Get Node and npm. npm install argon. Then add

argon = require 'argon'

to the top of your source file.

Usage

The argon object has a single function, ize, which you use to argon.ize your arguments for two purposes: validation and processing.

Validation

Let's say that you define a concatenate function, and you want it to be able to accept an array of files/directories as its first argument. Then you would write

concatenate = (paths) ->
  argon.ize arguments,
    name: 'concatenate'
    requiredArgs: ['file/dir path(s)']

Then if you call concatenate(), you'll get the message

[ERROR] Missing required argument 'file/dir path(s)' in call to `concatenate`

and an exception will be thrown.

Let's say that concatenate takes an optional hash of additional arguments (say, minify and target, with default values false and '') after the required arguments. Then Argon can automatically merge the given options hash with the defaults and log a warning if an invalid option is provided:

concatenate = (paths, options) ->
  [paths, {options}] = argon.ize arguments,
    name: 'concatenate'
    requiredArgs: ['file/dir paths']
    defaultOptions:
      minify: false, target: ''

The return value of argon is an array of the required arguments followed by a hash containing the options (among other things, potentially).

Now if concatenate ['src'] is equivalent to concatenate ['src'], minify: false, target: ''; concatenate ['src'], target: 'out' is equivalent to concatenate ['src'], minify: false, target: 'out'; and concatenate ['src'], oops: 'my bad' will generate

[WARNING] Invalid option 'oops' in call to `concatenate`

One more thing we can do is take callbacks in the options hash; if a function is given as the last argument, then it's used for all callbacks not explicitly specified in the options hash. For instance, concatenate might take an onSuccess and an onFail callback, but accept a single function as both, e.g.

concatenate ['src'], -> console.log 'Concatenation either succeeded or failed'

To do this, pass an events option to concatenate; the last entry in its returned array will be a map from each event name to a callback function or null:

concatenate = (paths, options) -> [paths, {options, callbacks}] = argon.ize arguments, name: 'concatenate' requiredArgs: ['file/dir paths'] events: ['onSuccess', 'onFail'] defaultOptions: minify: false, target: ''

Note that callbacks are considered separate from options except insofar as they're both given to your function in the same hash. Argon separates the two.

Unlike other options, events don't need to have defaults—but they can.

Processing

We can do some common argument conversions as we're validating. Continuing our example above, let's say that if we get a non-array for paths, we want to make it one. That is, we want the equivalent of

paths = [paths] unless paths instanceof Array

Argon uses a simple convention for this: Just end the argument name with s), as in string(s) or switch(es). It'll modify arguments appropriately and return the (possibly modified) arguments as an array. Thus, we can write

concatenate = (paths) -> [paths] = argon.ize arguments, name: 'concatenate' requiredArgs: ['file/dir path(s)']

Now paths will always be an array.

Settings

If you want to adjust Argon's verbosity level or other output settings, use the styout instance named 'argon', e.g.

argonOut = (require 'styout').instance 'argon'
argonOut.verbosity = argonOut.ERROR_VERBOSITY

TODO

  • Make browser-compatible (currently, depends on styout for output)
  • Optional type-checking and other fine-grained validation for individual arguments (similar to onvalid)

Copyright and license

Copyright (c) 2011 Trevor Burnham and made available under the MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.