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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@helloyork/commandparser

v0.1.0

Published

> `npm install @helloyork/commandparser`

Readme

CommandParser

Install

using npm

npm install @helloyork/commandparser

How to use

import package

const { Options, Types, default: CommandParser } = require("@helloyork/commandparser");

create a command parser

let options = new Options({
    "add": {
        name: "add", // Human-readable names
        description: "add numbers together",
        head: "/",
        args: {
            "usingArray": { // arg type 1
            // only command that is type-matched will be used
                args: [
                    {
                        name: "arg1",
                        description: "arg1",
                        required: true,
                        type: Types.ARRAY(Types.NUMBER), // number[]
                    },
                ],
                catchAll: true, // if catchAll is enabled, the matched result will contain an array of additional args
                allowExcess: true // match failed is there are some additional args
            },
            // this command will be matched if "/test [1,2,3]"

            "usingNumbers": {
                args: [
                    Types.NUMBER, // equivalent to {type: Types.NUMBER}
                    Types.NUMBER,
                ],
                catchAll: true,
                allowExcess: true
            }
        },
    },
    "test2": {
        // ... another command
    }
});

const parser = new CommandParser({
    commands: options,
    parserConfig: {
        strict: false // default false, if set to true, then failed parse when getting an invalid data type
    }
});

after that, use .parse(YOUR_TEXT) to start

let result = parser.parse("/add [1, 3]");
// {
//     "name": "add",
//     "result": {
//         "usingArray": {
//             "name": "usingArray",
//             "accepted": true,
//             "result": {
//                 "argOptions": ...,
//                 "parsed": {
//                     "commandName": "add",
//                     "commandArgs": [
//                         {
//                             "name": "ARRAY",
//                             "childrenTypes": [
//                                 NumberArgType
//                             ],
//                             "value": [
//                                 NumberArgType,
//                                 NumberArgType
//                             ]
//                         }
//                     ]
//                 }
//             }
//         },
//         "usingNumbers": ...
//     }
// }
console.log("command name: " + result.name);
console.log("usingArray accepted: " + result.result?.["usingArray"]?.accepted);
console.log("added result: " + result.result?.["usingArray"]?.result?.parsed?.commandArgs[0]
    .getValue() // you need to get value from ArrayArgType, is returns ArgType[]
    .reduce((a, b) => a + b.getValue(), 0)) // get values from each arg

// command name: add
// usingArray accepted: true
// added result: 4




let result2 = parser.parse("/add 1 3");
// {
//     "name": "add",
//     "result": {
//         "usingArray": ...,
//         "usingNumbers": {
//             "name": "usingNumbers",
//             "accepted": true,
//             "result": {
//                 "argOptions": ...,
//                 "parsed": {
//                     "commandName": "add",
//                     "commandArgs": [
//                         {
//                             "name": "NUMBER",
//                             "value": 1
//                         },
//                         {
//                             "name": "NUMBER",
//                             "value": 3
//                         }
//                     ]
//                 }
//             }
//         }
//     }
// }
console.log(JSON.stringify(result2, null, 4));
console.log("command name: " + result2.name);
console.log("usingNumbers accepted: " + result2.result?.["usingNumbers"]?.accepted);
console.log("added result: " + result2.result?.["usingNumbers"]?.result?.parsed?.commandArgs
    .map((a) => a.getValue()) // get values from each arg
    .reduce((a, b) => a + b, 0) // add them together
);

// command name: add
// usingNumbers accepted: true
// added result: 4

or you can parse command for your bot project


const { Options, Types, default: CommandParser } = require("commandparser");

let options = new Options({
    "add": {
        name: "add",
        description: "add numbers together",
        head: "/",
        args: {
            "usingArray": {
                args: [Types.ARRAY(Types.NUMBER)],
                catchAll: false,
                allowExcess: false
            },

            "usingNumbers": {
                args: [Types.NUMBER, Types.NUMBER],
                catchAll: true,
                allowExcess: true
            }
        },
    },
});

const parser = new CommandParser({
    commands: options,
});

const handlers = {
    "add": {
        "usingArray": (parsed) => {
            let sum = parsed.commandArgs[0]?.getValue().reduce((a, b) => a + b.getValue(), 0);
            return sum;
        },
        "usingNumbers": (parsed) => {
            let sum = parsed.commandArgs.reduce((a, b) => a + b.getValue(), 0);
            let catchedAllSum = parsed.catchedAll.reduce((a, b) => a + Number(b.value), 0);
            return sum + catchedAllSum;
        }
    }
};

function handle(input) {
    let result = parser.parse(input);
    let commandName = result.name;
    let firstAccepted = Object.keys(result.result).filter(k => result.result[k].accepted)[0];
    if (!firstAccepted) {
        console.log("No command found");
        return;
    }
    let commandHandler = handlers[commandName][firstAccepted];
    let commandArgs = result.result[firstAccepted].result.parsed;

    console.log(commandHandler(commandArgs));
}

handle("/add 1 3 5");
handle("/add [1, 3, 5]");
handle("/add 1 \"3\" 5");
handle("/add [1, \"3\", 5]");