argument.js
* @module greppy/helper/argument
* @author Hermann Mayer <hermann.mayer92@gmail.com>
var ArgumentHelper = function()
* Build a new argument object to process.
* @param {Array} args - Arguments to analyse
ArgumentHelper.prototype.build = function(args)
* Define all commands to parse.
* @param {Object} commands - Commands configuration
Argument.prototype.setCommands = function(commands)
throw new Error('You need an atomic command');
this.commands.atomic = commands.atomic;
Object.keys(commands.combined).forEach(function(combinedCommand) {
commands.combined[combinedCommand].forEach(function(atomicCommand) {
if (-1 === self.commands.atomic.indexOf(atomicCommand)) {
'No atomic command "' + command + '" was registerd'
this.commands.combined = commands.combined;
* Set the validator for all arguments.
* @param {Function} validator - Validator function
Argument.prototype.setValidator = function(validator)
* Parse the arguments and match up the commands
* to get a array of commands to run.
Argument.prototype.parse = function()
var combinedCommands = Object.keys(this.commands.combined);
var commands = this.commands.atomic.concat(combinedCommands);
// Build the command configuration
this.args.forEach(function(arg) {
if (-1 !== commands.indexOf(arg)) {
if (-1 !== combinedCommands.indexOf(operation)) {
self.commands.combined[operation].forEach(function(operation) {
if (-1 !== combinedCommands.indexOf(operation)) {
self.commands.combined[operation].forEach(function(operation) {
conf[operation].opts.push(arg);
conf[operation].opts.push(arg);
// Remap the command configuration into a chain
Object.keys(conf).forEach(function(command) {
conf[command].opts = self.validator(conf[command].opts);