GithubHelp home page GithubHelp logo

node-args's Introduction

@valkyriestudios/node-args

npm npm

A lightweight node script that reads and parses the arguments passed to a cli command, both converting them to their respective primitives and organizing them for easy access.

npm install @valkyriestudios/node-args

Example Output

The following is a series of example outputs for specific scenarios, showing the conversion of raw arguments to their primitives.

node myscript.js --fruits=bananas,apples,oranges --price=.1,.2,.3

{
    __bin: '/usr/bin/node',
    __script: '.../myscript.js',
    flags: {
        fruits: ['bananas', 'apples', 'oranges'],
        prices: [0.1, 0.2, 0.3],
    },
    args: [],
}
node myscript.js --no-filter --search

{
    __bin: '/usr/bin/node',
    __script: '.../myscript.js',
    flags: {
        filter: false,
        search: true,
    },
    args: [],
}
node myscript.js --price=8e-2

{
    __bin: '/usr/bin/node',
    __script: '.../myscript.js',
    flags: {
        price: 0.08,
    },
    args: [],
}
node myscript.js --foo=5.0124 -bar=false --foobar="abcdefg" --test=a,1,b,2 "Hello World" this,is,5

{
    __bin: '/usr/bin/node',
    __script: '.../myscript.js',
    flags: {
        foo: 5.0124,
        bar: false,
        foobar: "abcdefg",
        test: ["a", 1, "b", 2],
    },
    args: [
        "Hello World",
        ["this", "is", 5],
    ],
}

How does this work?

node-args uses process.argv, a node-native array of command line arguments and parses that array into an easy-to-use javascript object.

Getting Started

Simply import the library and execute it as a function. It will automatically return the parsed process arguments.

import NodeArgs from '@valkyriestudios/node-args';

const arguments = NodeArgs();

Configuration

Some aspects of the parsing can be controlled through a simple configuration object that you pass as a parameter to the node-args function.

An example of using this would be :

import NodeArgs from '@valkyriestudios/node-args';

const arguments = NodeArgs({
    delimiter: ';',
    c_numeric: false,
});

Options

  • c_array
    (default: true) Configures whether or not a string should be converted to an array if possible

  • c_bool
    (default: true) Configures whether or not a string should be converted to a boolean if possible

  • c_numeric
    (default: true) Configures whether or not a string should be converted to a number if possible

  • delimiter
    (default: ',') Sets the delimiter to be used to split a string into an array

  • bool_true_val
    (default: 'true') Configures what string represents a boolean true. e.g : '--harmony=true' will be parsed to { harmony: true }, whereas when bool_true_val is configured as '1', '--harmony=true' would be parsed to { harmony: 'true' }

  • bool_false_val
    (default: 'false') Configures what string represents a boolean false. e.g : '--harmony=false' will be parsed to { harmony: false }, whereas when bool_false_val is configured as '0', '--harmony=false' would be parsed to { harmony: 'false' }

Author

Contributors

node-args's People

Stargazers

Peter Vermeulen avatar

Watchers

Peter Vermeulen avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.