This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

getflags
TypeScript icon, indicating that this package has built-in type declarations

1.4.1 • Public • Published

getflags

npm Coverage Status

Parses arguments into long and short flags, with or without value.

Install

npm install --save getflags

Usage

The module exports a function that takes as arguments the argv array (splice from 2 to only include the program arguments), and a configuration of recognized flags. When a false flag is detected (not recognized or given a value when not expected and vice versa) an exception is thrown. Example:

const process = require('process');
const getflags = require('getflags');
 
console.log(
    getflags(process.argv.splice(2), [
        {
            short: 'c',             /* Short flag */
            long: 'collector',      /* Long equivalent flag */
            withValue: true,        /* Flag requires a value */
            collectInArray: true,   /* When same flag is given multiple times, 
                                       each value is pushed to an array, otherwise
                                       the last given value will set the flag value */
            required: true          /* Flag is required */
        },
        {
            short: 'd',
            withValue: true,
            default: 'when not specified' /* Default value given flag when not specified */
        },
        {
            long: 'some',
            withValue: true
        },
        {
            long: 'better',
            withValue: true
        },
        {
            long: 'function',
            withValue: false
        },
        {
            short: 'a',
            long: 'and',
            withValue: false,
            required: true
        },
        {
            short: 'g',
            withValue: true
        },
    ])
);

Saving that into example.js and running with:

node example.js --some 1 \
    --collector first \
    --better=some-text \
    --a -c='second collector' \
    --function -g 'and some with space'

returns:

{ 
    some: '1',
    collector: ['first', 'second collector'],
    better: 'some-text',
    and: true,
    function: true,
    g: 'and some with space',
    d: 'when not specified'
}

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i getflags

Weekly Downloads

0

Version

1.4.1

License

MIT

Unpacked Size

8.47 kB

Total Files

5

Last publish

Collaborators

  • knordman