deepify
Version:
DEEP Development Tools
210 lines (165 loc) • 3.73 kB
JavaScript
/**
* Created by AlexanderC on 8/7/15.
*/
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Arguments = undefined;
var _Argument = require('./Argument');
var _ArgumentObjectRequiredException = require('./Exception/ArgumentObjectRequiredException');
var _ArgumentsObjectRequiredException = require('./Exception/ArgumentsObjectRequiredException');
var _MissingArgumentException = require('./Exception/MissingArgumentException');
class Arguments {
constructor() {
this._vector = [];
this._unmanagedVector = [];
}
/**
* @returns {Arguments}
*/
validate() {
for (let i in this._vector) {
if (!this._vector.hasOwnProperty(i)) {
continue;
}
let item = this._vector[i];
if (item.required && !item.exists) {
throw new _MissingArgumentException.MissingArgumentException(item);
}
}
return this;
}
/**
* @param {Arguments} sibling
* @returns {Arguments}
*/
merge(sibling) {
if (!(sibling instanceof Arguments)) {
throw new _ArgumentsObjectRequiredException.ArgumentsObjectRequiredException();
}
this._vector = this._vector.concat(sibling.list());
return this;
}
/**
* @param {Array} args
* @returns {Arguments}
*/
populateUnmanaged(args) {
for (let i in args) {
if (!args.hasOwnProperty(i)) {
continue;
}
let item = args[i];
if (_Argument.Argument._matchNonOption(item)) {
this._unmanagedVector.push(item);
}
}
return this;
}
/**
* @param {Array} args
* @returns {Arguments}
*/
populate(args) {
for (let i in this._vector) {
if (!this._vector.hasOwnProperty(i)) {
continue;
}
let item = this._vector[i];
item.collect(args);
}
return this;
}
/**
* @param {String} name
* @returns {Arguments}
*/
remove(name) {
for (let i in this._vector) {
if (!this._vector.hasOwnProperty(i)) {
continue;
}
let item = this._vector[i];
if (item.name === name) {
this._vector.splice(i, 1);
break;
}
}
return this;
}
/**
* @param {String} name
* @returns {Argument}
*/
locate(name) {
for (let i in this._vector) {
if (!this._vector.hasOwnProperty(i)) {
continue;
}
let item = this._vector[i];
if (item.name === name) {
return item;
}
}
return null;
}
/**
* @returns {Arguments}
*/
create(...args) {
return this.add(new _Argument.Argument(...args));
}
/**
* @param {Argument} argument
* @returns {Arguments}
*/
add(argument) {
if (!(argument instanceof _Argument.Argument)) {
throw new _ArgumentObjectRequiredException.ArgumentObjectRequiredException();
}
this._vector.push(argument);
return this;
}
/**
* @returns {Boolean}
*/
get hasUnmanaged() {
return this._unmanagedVector.length > 0;
}
/**
* @param {Boolean} includeUnmanaged
* @returns {String[]}
*/
listValues(includeUnmanaged = true) {
let valuesVector = [];
for (let i in this._vector) {
if (!this._vector.hasOwnProperty(i)) {
continue;
}
let item = this._vector[i];
if (!item.exists) {
break;
}
valuesVector.push(item.value);
}
if (includeUnmanaged) {
valuesVector = valuesVector.concat(this._unmanagedVector);
}
return valuesVector;
}
/**
*
* @returns {String[]}
*/
listUnmanaged() {
return this._unmanagedVector;
}
/**
* @returns {Argument[]}
*/
list() {
return this._vector;
}
}
exports.Arguments = Arguments;