Socket
Socket
Sign inDemoInstall

main

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

main - npm Package Compare versions

Comparing version 0.0.16 to 0.0.17

4

examples/basic.js

@@ -8,3 +8,5 @@ #!/usr/bin/env node

require('../index')(module)
.usage('Usage:\n node test.js [flags] <word1> <word2>')
.usage(
'Usage:',
'node test.js [flags] <word1> <word2>')
.flags({

@@ -11,0 +13,0 @@ n: { alias: 'name', demand: true }

{
"name": "main",
"version": "0.0.16",
"version": "0.0.17",
"main": "index.js",

@@ -5,0 +5,0 @@ "repository": {

node-main
=========
Provides useful tools for writing command line scripts. It also ensures that a block of code is only invoked when 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')`).
Provides useful tools for writing command line scripts. It also ensures that a block of code is only invoked when 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')`). View the example directory for sample use cases.
Bare bones example:
```javascript
#!/usr/bin/env node
// won't be called if this module is required from another module
require('main')(module).run(function($) {
$.cout('foo').exit(); // print with console.log, exit 0
});
```
## Installation & Usage

@@ -34,12 +23,23 @@

#### `.usage(message)`
### `.usage(message [, <line2>, <line>, ...])`
An optional message to append to the top of flags that can describe how the script should be invoked, e.g.
An optional message to append to the top of flags that describes 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 and will appear after this usage message (see below).
Flags do not have to be specified in this string. Usage for flags are automatically generated based on the flag configuration provided (see below) and will appear after this usage message.
#### `.flags(options)`
If you need to provide a more detailed usage message that will span over multiple lines, you can do so by providing more arguments. Every argument will be separated by a newline, e.g.
```javascript
require('main')(module)
.usage(
'Usage: ./script [flags] <posArg1> <posArg2>',
' - Some more usage information',
' - Another line of usage information')
.run(/* ... */)
```
### `.flags(options)`
`options` follows the [optimist format for options](https://github.com/substack/node-optimist#optionskey-opt), but groups them together, e.g.:

@@ -53,8 +53,8 @@

// ...
})
}).run(/* ... */)
```
#### `.run(fn)`
### `.run(fn)`
`fn` is the callback that will be invoked when the script is ran directly from a terminal. It can take the following parameters:
`fn` is the callback that will be invoked when the script is ran directly from a terminal. `fn` can take the following parameters:

@@ -268,3 +268,6 @@ ```javascript

require('main')(module)
.usage('Usage:\n node test.js [flags] <word1> <word2>')
.usage(
'Create a sentence from flags and positional arguments!',
'',
'Usage: node test.js [flags] <word1> <word2>')
.flags({

@@ -271,0 +274,0 @@ n: { alias: 'name', demand: true }

@@ -10,3 +10,4 @@ var Tools = require('./Tools');

// sets the usage
this.usage = function(newUsage) {
this.usage = function() {
var newUsage = Array.prototype.slice.call(arguments).join('\n');
if (ranDirectly) { usage = newUsage; } return this;

@@ -13,0 +14,0 @@ };

@@ -21,5 +21,7 @@ var extend = require('util')._extend;

self.flags = {};
for (var argvKey in self.optimist.argv) {
if (typeof flags[argvKey] !== 'undefined') {
self.flags[argvKey] = self.optimist.argv[argvKey];
if (flags) {
for (var argvKey in self.optimist.argv) {
if (typeof flags[argvKey] !== 'undefined') {
self.flags[argvKey] = self.optimist.argv[argvKey];
}
}

@@ -26,0 +28,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc