Home

Awesome

Table of Contents

Parse

<img src="https://travis-ci.org/cli-kit/cli-argparse.svg?v=2" alt="Build Status"> <img src="http://img.shields.io/npm/v/cli-argparse.svg?v=2" alt="npm version"> <img src="https://coveralls.io/repos/cli-kit/cli-argparse/badge.svg?branch=master&service=github&v=2" alt="Coverage Status">.

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

Install

npm install cli-argparse

Example

var parse = require('./index');
var args = [
  'server',
  'start',
  '-xvd',
  '--port=80',
  '--config',
  '-',
  '--config=config.json',
  '--log',
  'server.log',
  '--no-color'
];
var result = parse(args);
console.log(JSON.stringify(result, undefined, 2));
{
  "flags": {
    "x": true,
    "v": true,
    "d": true,
    "color": false
  },
  "options": {
    "port": "80",
    "config": [
      "-",
      "config.json"
    ],
    "log": "server.log"
  },
  "raw": [
    "server",
    "start",
    "-xvd",
    "--port=80",
    "--config",
    "-",
    "--config=config.json",
    "--log",
    "server.log",
    "--no-color"
  ],
  "stdin": true,
  "unparsed": [
    "server",
    "start"
  ],
  "strict": false,
  "vars": {}
}

API

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

parse([args], [options])

Returns a result object.

Options

Result

The result object contains the fields:

Aliases

Aliases allow arguments to map to meaningful property names that will be set on the result object options and flags.

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

Note that you should not use the negated long form (--no-highlight) when specifying these hints, always use the positive form.

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

Strict

A boolean that indicates that only known arguments (those declared in the options and flags properties) are accepted, all other arguments will be placed in the unparsed array.

Flat

Creating a flat result can be useful if you are certain that there are no naming collisions, typically this can be achieved by providing hints using flags and options.

When this option is specified the result object will not have a flags property, instead all flags and options will be in the options property of the result.

Vars

Sometimes it is useful to collect arguments following a convention, for example -D like java or maybe all arguments prefixed with @. When the vars option is set all arguments that match the convention are collected in to the vars result object, see the vars test spec for examples.

Developer

Test

To run the test suite:

npm test

Cover

To generate code coverage run:

npm run cover

Lint

Run the source tree through jshint and jscs:

npm run lint

Clean

Remove generated files:

npm run clean

Readme

To build the readme file from the partial definitions (requires mdp):

npm run readme

License

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

Generated by mdp(1).