l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

append.js (1565B)


      1 /*:nodoc:*
      2  * class ActionAppend
      3  *
      4  * This action stores a list, and appends each argument value to the list.
      5  * This is useful to allow an option to be specified multiple times.
      6  * This class inherided from [[Action]]
      7  *
      8  **/
      9 
     10 'use strict';
     11 
     12 var util = require('util');
     13 
     14 var Action = require('../action');
     15 
     16 // Constants
     17 var c = require('../const');
     18 
     19 /*:nodoc:*
     20  * new ActionAppend(options)
     21  * - options (object): options hash see [[Action.new]]
     22  *
     23  * Note: options.nargs should be optional for constants
     24  * and more then zero for other
     25  **/
     26 var ActionAppend = module.exports = function ActionAppend(options) {
     27   options = options || {};
     28   if (this.nargs <= 0) {
     29     throw new Error('nargs for append actions must be > 0; if arg ' +
     30         'strings are not supplying the value to append, ' +
     31         'the append const action may be more appropriate');
     32   }
     33   if (!!this.constant && this.nargs !== c.OPTIONAL) {
     34     throw new Error('nargs must be OPTIONAL to supply const');
     35   }
     36   Action.call(this, options);
     37 };
     38 util.inherits(ActionAppend, Action);
     39 
     40 /*:nodoc:*
     41  * ActionAppend#call(parser, namespace, values, optionString) -> Void
     42  * - parser (ArgumentParser): current parser
     43  * - namespace (Namespace): namespace for output data
     44  * - values (Array): parsed values
     45  * - optionString (Array): input option string(not parsed)
     46  *
     47  * Call the action. Save result in namespace object
     48  **/
     49 ActionAppend.prototype.call = function (parser, namespace, values) {
     50   var items = (namespace[this.dest] || []).slice();
     51   items.push(values);
     52   namespace.set(this.dest, items);
     53 };