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

node-main =========


Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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')).

node-main utilizes optimist for argument parsing.

Installation & Usage

npm install main

require('main')

Once required, you can chain the functions below

.flags(options)

options follows the optimist format for options, but groups them together, e.g.:

require('main').flags({
    f: { alias: 'flag' },
    t: { alias: 'secondFlag' },
    d: { demand: true },
    // ...
})

.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(argv, exit)
  • argv is the parsed optimist argv object
  • 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).

Example

#!/usr/bin/env node

exports.isTrue = function(value) { // just to get a point across...
    return value === true;
};

require('main')
.flags({
    v: { alias: 'valid', 'boolean': true, 'default': false }
})
.run(function(argv, exit) {
    if (exports.isTrue(argv.valid)) {
        exit();
    } else {
        exit(1, 'optional error message');
    }
});

Running from the terminal ($? indicates exit status):

    > node scriptAbove.js --valid
    > $?
    0
    > node scriptAbove.js
    optional error message
    > $?
    1

Using the module from another script will not execute the code in main:

var scriptAbove = require('./scriptAbove');
console.log(scriptAbove.valid('hello')); // 'false'
console.log(scriptAbove.valid(true)); // 'true'

Keywords

FAQs

Package last updated on 01 Oct 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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