Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Created July 14, 2014 02:00
Show Gist options
  • Select an option

  • Save TGOlson/9ad5518c6796b57afdfb to your computer and use it in GitHub Desktop.

Select an option

Save TGOlson/9ad5518c6796b57afdfb to your computer and use it in GitHub Desktop.
var _ = require('lodash'),
argv = require('yargs').argv,
input = _.first(argv._);
// everything else will default to 1
var defaults = {
ideaCount: 4,
questionCount: 2
};
// allows scenario loader to be run via command line
if(input) {
parseCommands(input);
}
function parseCommands(commands) {
var config = {};
commands = commands.split('_and_');
_.forEach(commands, function(command) {
command = command.split('_');
var count = _.first(command),
entityType = _.last(command);
countType = mapToCountType(entityType);
config[countType] = toNum(count);
});
config = _.defaults(config, defaults);
console.log(config);
return config;
}
module.exports = parseCommands;
// renames ideas to ideaCount, etc.
function mapToCountType(entityType) {
if(entityType[entityType.length - 1] === 's') {
entityType = entityType.slice(0, entityType.length - 1);
}
return entityType + 'Count';
}
function toNum(string) {
var nums = {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
ten: 10,
fifteen: 15,
twenty: 20,
thirty: 30,
fourty: 40,
fifty: 50
};
return nums[string];
}
var optionParser = require('./option-parser');
optionParser('five_ideas_and_one_question');
// => { ideaCount: 5, questionCount: 1 }
// option parser can also be run through the command line
// node option-parser two_questions_and_three_ideas
// => { questionCount: 2, ideaCount: 3 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment