npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

command-line-arg-map

v1.0.3

Published

Library that maps command line arguments to a easily readable map.

Downloads

6

Readme

command-line-arg-map

Library that maps command line arguments to a easily readable map. An array or string can be used as parameters for the constructor and toCommand function.

The command-line argument map contains a command and an args property:

{
    "command": [],
    "args": {}
}
  • command: All arguments given before the first flag.
  • args: All flags with their given values, this can eigher be null, a single value or an array. When an argument's value is undefined it means that it has not been set and if it is null, it is set without a value. In every other case it returns the given value.

Map example

{
    "command": [ //All items before the arguments.
        "program",
        "command"
    ],
    "args": {
        "-s": null, //This flag is set without a value.
        "--source": null, //This flag is set without a value.
        "-f": undefined, //This flag is not set or given a value.
        "--flag": undefined, //This flag is not set or given a value.
        "-v": "1.0.0", //This flag is given a single value.
        "--lang": "eng", //This flag is given a single value.
        "-i": [ //This flag is set with multiple items.
            "./input/dir/",
            "./input/dir1/"
        ],
        "--output": [ //This flag is set with multiple items.
            "./output/dir/",
            "./output/dir1/"
        ]
    }
}

Install

npm install --save command-line-arg-map

Import package

Normal javascript

let CommandMap = require('command-line-arg-map');

ES6

import CommandMap from 'command-line-arg-map';

How to use

String as value


let value = "node index.js -s -f -i ./test/dir/file.ext ./test/dir/file1.ext ./test/dir/file2.ext -o ./test/dir/out.ext -v 1.0.0 --flag --lang=eng --flags 1 2 3";

let command = new CommandMap(value);

console.log(command);

Array as value


let value = ["node", "index.js", "-s", "-f", "-i", "./test/dir/file.ext", "./test/dir/file1.ext", "./test/dir/file2.ext", "-o", "./test/dir/out.ext", "-v", "1.0.0", "--flag", "--lang=eng", "--flags", "1", "2", "3"];

let command = new CommandMap(value);

console.log(command);

Nodejs command line as value Script:

let command = new CommandMap(process.argv);

console.log(command);

Command:

node index.js -s -f -i ./test/dir/file.ext ./test/dir/file1.ext ./test/dir/file2.ext -o ./test/dir/out.ext -v 1.0.0 --flag --lang=eng --flags 1 2 3

Output

{
    "commands": [
        "node",
        "index.js"
    ],
    "args": {
        "-s": null,
        "-f": null,
        "-i": [
            "./test/dir/file.ext",
            "./test/dir/file1.ext",
            "./test/dir/file2.ext"
        ],
        "-o": "./test/dir/out.ext",
        "-v": "1.0.0",
        "--flag": null,
        "--lang": "eng",
        "--flags": [ 
            "1",
            "2",
            "3"
        ]
    }
}

Accessing the command

Script:

let value = "node index.js -s -f -i ./test/dir/file.ext ./test/dir/file1.ext ./test/dir/file2.ext -o ./test/dir/out.ext -v 1.0.0 --flag --lang=eng --flags 1 2 3";

let command = new CommandMap(value);

console.log(command.getCommands());

Output:

["node", "index.js"]

Getting arguments

There are a couple of ways to retreive arguments when a command map is created.

Argument functions When only one argument is given to the getArg function, it will return the value of that argument.

let value = "node index.js -s ./source/dir/";

let command = new CommandMap(value);

console.log(command.getArg("-s"));

Output:

"./source/dir/"


When multiple flags can be used for the same argument, the getArg function can be used with the posible flags as parameters. This functions will return the value of the first flag that is not null or undefined.

let value = "node index.js -s ./source/dir/";

let command = new CommandMap(value);

console.log(command.getArg("-s", "--source"));

Output:

"./source/dir/"

When no arguments are given to the getArgs function it will return all arguments found in the command.

let value = "node index.js -s -f -i ./test/dir/file.ext ./test/dir/file1.ext ./test/dir/file2.ext -o ./test/dir/out.ext -v 1.0.0 --flag --lang=eng --flags 1 2 3";

let command = new CommandMap(value);

console.log(command.getArgs());

Output:

{
    "-s": null,
    "-f": null,
    "-i": [
        "./test/dir/file.ext",
        "./test/dir/file1.ext",
        "./test/dir/file2.ext"
    ],
    "-o": "./test/dir/out.ext",
    "-v": "1.0.0",
    "--flag": null,
    "--lang": "eng",
    "--flags": [ 
        "1",
        "2",
        "3"
    ]
}

Multiple arguments can be requested using the getArgs function. This function will return a map containing all the requested arguments.

let value = "node index.js -s -f -i ./test/dir/file.ext ./test/dir/file1.ext ./test/dir/file2.ext -o ./test/dir/out.ext -v 1.0.0 --flag --lang=eng --flags 1 2 3";

let command = new CommandMap(value);

console.log(command.getArgs("-s", "-p", "-i", "--lang"));

Output:

{
    "-s": null,
    "-p": undefined,
    "-i": [
        "./test/dir/file.ext",
        "./test/dir/file1.ext",
        "./test/dir/file2.ext"
    ],
    "--lang": "eng"
}