Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-argparse

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-argparse

Simple Argument parser for Command-line Applications

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-33.33%
Maintainers
2
Weekly downloads
 
Created
Source

simple-argparse

Simple Argument parser for Command-line Applications

node npm Travis Gemnasium Coveralls

installation

⇒ npm install simple-argparse

basic usage

sample.js:

require("simple-argparse")
  .description("Application Description")
  .version("0.3.0")
  .option("start", "starts application", startFunc)
  .epilog("See License at http://opensource.org/licenses/MIT")
  .parse();

function startFunc(host, port) {
  app.listen(port, host);
}

sample output:

⇒ node Sample.js
 Application Description

    help     show this help information
    start    starts application
    version  show version information

 See License at http://opensource.org/licenses/MIT

API

The module exports a new Parser instance, that can be used immediately. If you wish to create more parsers, you instead use the Parser constructor exported at .Parser:

var Parser = require("simple-argparse").Parser;
var myParser = new Parser();

While instantiating a parser, an output function may be registered with the parser other than the default console.log:

var myOtherParser = new Parser(function(output) {
  socket.emit("commandComplete", output);
});

A Parser has these methods:

  1. Parser#description([name:String,] description:String)
  • name:(Optional) refers to the name of your Application

  • description: provides a description of your Application

  • Parser#version(version:String)

  • version: provides version information of your Application. Defaults to "0.0.0"

  • Parser#option(command:String, description:String [, optionFunction:Function])

  • command:

    • a string that will be typed by user to fire the command
    • any spaces will be replaced by hyphens
  • description: help information regarding this command

  • optionFunction:(Optional) See Parsing below for more information.

  • Parser#epilog(epilog:String)

  • epilog: a string that will appear at the bottom of the help information

  • Parser#parse([arguments:String])

  • arguments:(Optional)

    • a string representing commands as typed in command-line
    • if left out, process.argv will be used instead
  • Parser#showHelp()

    • shows the help information
    • is done by passing all the necessary data as string to the registered output function
  • Parser#showVersion()

    • similar to Parser#showHelp() but only supplies version information, registered with .version().

Parsing

All arguments parsed by .parse() are processed using minimist, and made available to the option functions as their this argument.

An option function refers to the function passed to .option. Options that are NOT perceived as options by minimist are passed to the function as arguments.

Consider the following example:

parse.js:

require("simple-argparse")
  .version("0.0.0")
  .option("test", "run tests", function(suite) {
    if (this.verbose) { console.log("--verbose was used"); }
    if (suite) {
      console.log("will run tests only for: " + suite);
    } else {
      console.log("will run all tests!");
    }
    // ...
  })
  .parse();

Now running the above script from a terminal:

⇒ node parse.js test
will run all tests!

⇒ node parse.js test someSuite
will run tests only for: someSuite

⇒ node parse.js test someSuite --verbose
--verbose was used
will run tests only for: someSuite

See minimist for more information on the parsing.

The option function is optional. If it is left out, the option will be ignored. This may be useful for commands not yet implemented.

license

The MIT License (MIT)

Copyright (c) 2014-2015 Forfuture LLC we@forfuture.co.ke

Keywords

FAQs

Package last updated on 19 Jul 2015

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