Lets say you have all of your game functions in one giant js file, functions for calculating attack, def, etc. A month down the road you realize that you want to change just about anything, you'll have to either rewrite a bunch or something far worse. Non-modularity is the hallmark of hard to maintain code or the 10lbs of 'stuff' in a 5lb bag.
Wouldn't it be much easier if we could just keep all of our functions separate? Pass arguments to them in a generic and modular way? Update a 20 line file instead of a 1,000+ line file? We can do all of that with options parsers.
Taken from: http://www.catonmat.net/blog/nodejs-modules-optimist/
I'm guessing it's in order of magnitudes more complicated, but looks to be necessary.
This time I'll introduce you to node-optimist - the lightweight options parser library. This library is also written by James Halliday (SubStack), my co-founder of Browserling and Testling.Wonder how lightweight an options parser can be? Check this out:var argv = require('optimist').argv;And you're done! All options have been parsed for you and have been put inargv
.Here are various use cases. First off, it supports long arguments:#!/usr/bin/env node var argv = require('optimist').argv; if (argv.rif - 5 * argv.xup > 7.138) { console.log('Buy more riffiwobbles'); } else { console.log('Sell the xupptumblers'); }Now you can run this script with--rif
and--xup
arguments like this:$ ./xup.js --rif=55 --xup=9.52 Buy more riffiwobbles $ ./xup.js --rif 12 --xup 8.1 Sell the xupptumblersI know you want to buy more riffiwobbles and sell your xupptumblers.
No comments:
Post a Comment