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

positional-args

v1.1.0

Published

Positional argument parser

Downloads

11

Readme

positional-args - Node.js positional argument parser

Unlike other argument parsers, this library is built specifically (and only) for positional arguments, allowing it to provide much more functionality and control over how positional arguments are interpreted. It also doesn't force you to restructure your code to use it. All the individual pieces are exposed and work standalone, so you only have to use as much of the library as you need.

This library was originally written to be a lightweight, unintrusive alternative to Commando for Discord bots. There are no Discord-specific things in this library, but it does still work well for this purpose.

Features:

  • No external dependencies.
  • Lightweight, unintrusive API that doesn't change the paradigm of your code.
  • Each class of the library is exposed, so you can use as much or as little as you want.
  • Parses from strings or arrays from any source.
  • Validate and transform arguments with user-specified functions.
  • Provides parsed arguments in a format similar to other argparse libraries, such as yargs.
  • Define commands with multiple argument lists and auto-generated help text.
  • Command registry can consume a command string and delegate it to the appropriate command.
  • Async / Promise friendly. All user-specified functions may be async.
  • Useful alternative to Commando.js for Discord bots.

Installation

Available on npm.

npm install positional-args

Usage and examples

See full API here!

See GitHub releases page for API changes.

This example creates a CommandRegistry with multiple commands and handler for unrecognized commands.

const registry = new CommandRegistry()
    .add(new Command('say')
        .description('Prints the args given. Also has multiple argument sets, for fun')
        .addArgSet([ new Argument('first') ])
        .addArgSet([
            new Argument('first'),
            new Argument('extras')
                .varargs(true)
                .optional(true),
        ])
        .handler(args => console.log('I got', args))
    )
    .add(new Command('die_always')
        .description('I always throw because I am not nice')
        .handler(() => { throw new Error('I strike again!') })
    )
    .helpHandler() // Use the built-in help handler
    .defaultHandler((parts) => console.log('Unrecognized command', parts));

registry.help('say'); /* Returns string:
"say <first>\n" +
"say <first> <extras_1> [extras_2] ... [extras_n]" */

registry.execute('say one'); /* Prints:
I got {
  _: ["one"],
  first: "one",
} */

registry.execute('say one two three four'); /* Prints:
I got {
  _: ["one", "two", "three", "four"],
  first: "one",
  extras: ["two", "three", "four"],
} */

try {
    registry.execute('die_always');
} catch (err) {
    // CommandError
    // .message: "Command failed"
    // .nested: Error("I strike again!")
    // .full_message: "Command failed: I strike again!"
}

registry.execute('some random thing'); /* Prints:
Unrecognized command ["some", "random", "thing"] */

There are more examples in the API docs.

A note on async

This library has a permissive enough API that one could force mixing sync and async elements together. While this is possible, it is explicitly discouraged and not officially supported. If you need both sync and async commands, consider either making all commands async or dividing them into two separate registries.

// DON'T DO THIS!
const registry = new CommandRegistry()
    .asynchronous(false)
    .add(new Command('example'))
    .add(new Command('other'));
registry.commands.get('example').is_async = true;

try {
    registry.execute('example').then(...).catch(...);
} catch (err) {
    ...
}

// Do this instead!
const registry = new CommandRegistry()
    .asynchronous(true)
    .add(new Command('example'))
    .add(new Command('other'));

registry.execute('example').then(...).catch(...);

License

Copyright 2021 Mimickal

This code is licensed under the LGPL-3.0 license.

Basically, you are free to use this library in your closed source projects, but any modifications to this library must be made open source.