🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

cli-argparse

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cli-argparse

Lightweight argument parser

0.1.3
Source
npm
Version published
Weekly downloads
173
2.37%
Maintainers
1
Weekly downloads
 
Created
Source

Parse

Lightweight yet feature rich argument parser.

This module does not define any options or any program requirements it simply parses arguments into an object structure that is easier for other modules to work with.

Features

  • Supports multiple option values as arrays
  • Supports long flag negations, eg: --no-color
  • Supports --option=value and --option value
  • Expands short flags such as -xvf
  • Treat - as special stdin flag
  • Stop argument parsing on --
  • Comprehensive test suite

Install

npm install cli-argparse

Test

npm test

Example

var parse = require('cli-argparse');
var args = [
  'server',
  'start',
  '-xvd',
  '--port=80',
  '--file=file.txt',
  '--file',
  'file.json',
  '--no-color'
];
var result = parse(args);
{
  "flags": {
    "x": true,
    "v": true,
    "d": true
  },
  "options": {
    "port": "80",
    "file": [
      "file.txt",
      "file.json"
    ]
  },
  "raw": [
    "server",
    "start",
    "-xvd",
    "--port=80",
    "--file=file.txt",
    "--file",
    "file.json"
  ],
  "stdin": false,
  "unparsed": [
    "server",
    "start"
  ]
}

API

var parse = require('cli-argparse');
var result = parse();
console.dir(result);

parse(args, [options])

  • args: Specific arguments to parse, default is process.argv.slice(2).
  • options: Parsing configuration options.

Returns a result object.

Options

  • alias: Map of argument names to property names.
  • flags: Array of argument names to be treated as flags.
  • options: Array of argument names to be treated as options.
Aliases

Aliases are mapped on the raw argument name, to map -v | --verbose to a verbose property use {'-v --verbose': 'verbose'}.

Flags

Use the flags array when you need to force a long argument to be treated as a flag, for example ['--syntax-highlight'].

Options

Use the options array when you need to treat a short argument as accepting a value, for example ['-f'].

Result

The result object contains the fields:

  • flags: Object containing arguments treated as flags.
  • options: Object containing arguments treated as options with values.
  • raw: Array of the raw arguments parsed.
  • stdin: Boolean indicating whether - is present in the argument list.
  • unparsed: Array of values that were not parsed.

License

Everything is MIT. Read the license if you feel inclined.

Keywords

cli

FAQs

Package last updated on 30 Jan 2014

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