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

@auttam/easycli

v1.0.6

Published

A quick and easy way of creating cli for your npm package.

Downloads

27

Readme

Easy CLI

Installation

Install @auttam/easycli package

npm install @auttam/easycli

Example # 1

const { Program } = require('@auttam/easycli');

class HelloWorld extends Program {

    main(message, $options){
        // use default message if message not from the cli
        let greetMessage = message || "Hello World!";        

        // check for the underline options
        if($options.$has("u", "U", "underline")) {
            greetMessage = `\u001b[4m${greetMessage}\u001b[0m`;
        }

        // check of the color options
        if($options.$has("c", "C", "color")) {
            greetMessage = `\u001b[32m${greetMessage}\u001b[0m`;
        }

        console.log(greetMessage);
    }
}

// run the program
Program.run(new HelloWorld());

To test, save the example above as bin/hello-world.js file and run the following commands -

node ./bin/hello-world
# output: Hello World!

node ./bin/hello-world "Test Message"
# output: Test Message

node ./bin/hello-world -cu
# displays 'Hello World!' with underline and green color

node ./bin/hello-world -h 
# displays help

node ./bin/hello-world -v
# displays cli version which is by default '1.0.0'
  • All the non-option command-line arguments are passed as the parameters of the main() method
  • Add $params parameter to the main() method to access all the parameters supplied from the cli
  • Add $options parameter to the main() method to access all options supplied from the cli
    • Use $options.$has(...names) to check if any of the option from the list is set
    • Use $options.$get(name) to get the value supplied with the option e.g. node ./bin/hello-world --value_option=my_value
    • To access options by name e.g. $options.underline when any of -u, -U or --underline option is set, add program configuration. Find more information here.

Example # 2

const { Program } = require("@auttam/easycli");

// enable commands
Program.settings({
  enableCommands: true,
});

class SimpleCalculator extends Program {
  divideCommand(dividend, divisor) {
    if (isNaN(dividend) || isNaN(divisor)) {
      console.log("dividend and divisor must be numbers");
      return;
    }
    console.log(`${dividend}/${divisor} = ${dividend / divisor}`);
  }

  addCommand(...numbers) {
    // due to configuration added below,
    // the numbers parameter will never be empty
    if (numbers.some((item) => isNaN(item))) {
      console.log("All parameters must be numbers");
      return;
    }

    const total = numbers.reduce((sum, number) => sum + number);
    console.log(`${numbers.join("+")} = ${total}`);
  }
}

// add configuration and run program
Program.run(
  new SimpleCalculator({
    commands: [
      {
        name: "add",
        method:"addCommand",
        params: [
          {
            name: "numbers",
            required: true,
          }
        ],
      },
    ],
  })
);

To test, save the example above as bin/simple-calculator.js file and run following commands -

# Displays list of available commands
node ./bin/simple-calculator

# Runs add command
node ./bin/simple-calculator add 5 5 5
# output: 5+5+5 = 15

# Runs divide command
node ./bin/simple-calculator divide 5 2
# output: 5/2 = 2.5

Printing CLI help using node ./bin/simple-calculator -h

Simple Calculator v1.0.0

Usage: simple-calculator <command>

Available Commands:

   divide
   add

See command help for more options

Other usage:

   simple-calculator --help, -h             To view help
   simple-calculator <command> --help, -h   To view command help
   simple-calculator --version, -v          To view help
   simple-calculator help                   To view help

More help

Quick start guide and more help available here