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

yargs-promise

v1.1.0

Published

Use the headless yargs parser with promises

Downloads

2,893

Readme

yargs-promise

Use the headless yargs parser with promises!

Install

npm

npm install --save yargs-promise

yarn

yarn add --save yargs-promise

Usage

Instead of using a callback with yargs.parse, use a promise chain: parser.parse(text).then().catch().

Examples:

const yargs = require('yargs');
const YargsPromise = require('yargs-promise');

// create the customized yargs parser
const parser = new YargsPromise(yargs);

// setup command & command handler
parser
  .command('hello <name>', 'hello world parser' , ()=>{}, (argv) => {

    // resolve a promise or other value
    argv.resolve(doSomething);

    // reject stuff
    argv.reject(yourErrorData);

    // or do nothing and reject/resolve will be handled internally
    console.log('testing argv');
  })
  .help();

// parse text input and use the returned promise
parser.parse('hello world')
  .then(({data, argv}) => {
    // data is what your code resolved or what an internal command resolved
  })
  .catch(({error, argv}) => {
    // `error` is what your code rejected or an internal error from yargs
  });

Customizing context example

const yargs = require('yargs');
const YargsPromise = require('yargs-promise');

const parser = new YargsPromise(
  yargs,
  // customize context
  {
    customContextMethod: () => {},
    foo: 'bar'
  }
);

parser
  .command('hello <name>', 'hello world parser' , ()=>{}, (argv) => {
    // argv now contains
    argv.customContextMethod();
    console.log(argv.foo);
  })
  .help();

Need access to yargs object? Work with the direct yargs object prior to passing it into the yargs-promise constructor. For convenience, it is also available at parser.yargs.

How it works

This library does three things:

  • wraps the yargs.parse in a new Promise
    • no more callbacks
  • attaches that Promises resolve & reject methods on the context passed to yargs.parse
    • this enables you to call argv.resolve or argv.reject in command handler function
  • handles default behavior
    • from Error validation
    • output from internal commands like .help()
    • unhandled output from custom handler

Checkout the source code or tests for more information.

Why

Building chatbots requires parsing and handling text input. This wraps up the most common needs I've come across for handling errors, simple commands, and commands with handlers.