main
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -13,2 +13,7 @@ | ||
this.usage = function(message) { | ||
optimist.usage(message); | ||
return this; | ||
}; | ||
// Load in options for optimist | ||
@@ -28,3 +33,3 @@ this.flags = function(flags) { | ||
if (!ranDirectly) { return; } | ||
mainFn(optimist.argv, exitFn); | ||
mainFn(optimist.argv, exitFn, optimist.help()); | ||
}; | ||
@@ -31,0 +36,0 @@ }; |
{ | ||
"name": "main", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
node-main | ||
========= | ||
A *very* basic module that only runs code if the script is ran directly (e.g. calling `node script`). It will not call the code if the script has been required (e.g. `require('script')`). | ||
Runs a block of code if a script is called directly as in calling `node script`. It will *not* call the block of code if the script has been required in another module as in `require('script')`. | ||
node-main utilizes [optimist](https://github.com/substack/node-optimist) for argument parsing. | ||
node-main utilizes [optimist](https://github.com/substack/node-optimist) for argument parsing and provides other tools that are useful for when working with command line scripts. | ||
## Installation & Usage | ||
npm install main | ||
npm install --save main | ||
### require('main') | ||
@@ -16,2 +16,10 @@ | ||
### .usage(message) | ||
An optional message to append to the top of flags that can describe how the script should be invoked, e.g. | ||
Usage: ./script [flags] <posArg1> <posArg2> | ||
Flags do not have to be specified in this string. Usage for flags are automatically generated based on the options provided (see below) and will appear after this usage message. | ||
### .flags(options) | ||
@@ -35,3 +43,3 @@ | ||
```javascript | ||
fn(argv, exit) | ||
fn(argv, exit, help) | ||
``` | ||
@@ -41,2 +49,3 @@ | ||
- `exit` is a helper function that can be used to exit the script. It follows the form `exit(exitCode, optionalMessage)`. If no exit code if provided, it will exit with 0 (success). | ||
- `help` is the usage information if the need arises to explicitly display it. | ||
@@ -46,19 +55,22 @@ | ||
Refer to the following script as `sentence.js` | ||
```javascript | ||
#!/usr/bin/env node | ||
exports.isTrue = function(value) { // just to get a point across... | ||
return value === true; | ||
exports.sentence = function(name, word1, word2) { | ||
return name + ',' + word1 + ' ' + word2 + '.'; | ||
}; | ||
require('main') | ||
.usage('Usage:\n node test.js [flags] <word1> <word2>') | ||
.flags({ | ||
v: { alias: 'valid', 'boolean': true, 'default': false } | ||
n: { alias: 'name', demand: true } | ||
}) | ||
.run(function(argv, exit) { | ||
if (exports.isTrue(argv.valid)) { | ||
exit(); | ||
} else { | ||
exit(1, 'optional error message'); | ||
} | ||
.run(function(argv, exit, help) { | ||
// exit if there aren't two words (positional arguments) | ||
if (argv._.length !== 2) { exit(1, help); } | ||
var word1 = argv._[0], | ||
word2 = argv._[1]; | ||
exports.sentence(argv.name, word1, word2); | ||
}); | ||
@@ -70,9 +82,10 @@ ``` | ||
```bash | ||
> node scriptAbove.js --valid | ||
> $? | ||
0 | ||
> node scriptAbove.js | ||
optional error message | ||
> $? | ||
1 | ||
> node sentence.js --name Nolan sit down | ||
Nolan, sit down. | ||
> $? | ||
0 (success) | ||
> node sentence.js | ||
# (prints out help & usage information. name / words are not defined) | ||
> $? | ||
1 (failure) | ||
``` | ||
@@ -84,4 +97,7 @@ | ||
var scriptAbove = require('./scriptAbove'); | ||
console.log(scriptAbove.valid('hello')); // 'false' | ||
console.log(scriptAbove.valid(true)); // 'true' | ||
console.log(scriptAbove.sentence('Nolan', 'sit', 'down')); | ||
``` | ||
## Note | ||
When installing, make sure to use the `--save` option and/or specify the version in your `package.json` dependencies. This package is undergoing some heavy changes at the moment and new versions may be radically different from previous releases. This type of business will stop once it reaches 0.1.0. |
5787
34
98