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

cli-core

v1.0.2

Published

A tool for building really simple cli tools

Downloads

10

Readme

cli-core

A tool for building very simple cli's

This is the internal core of ambient-cli that I have pulled out into its own library. The reason for writing my own cli instead of using one of the many out there like vorpal is that they were either too heavy or too simple for what I wanted - this one is a little in between.

Example

demo.js

import { define, command, option, flags, init } from 'cli-core'

define(
    command(
        "say-hello",
        "This command just says hi",
        () => {
            if (option('french')) return "Bonjour!"
        
            return "Hi!"
        }
    )
)

flags([
    '--french', 'Say hello in french'
])

init()
$ node demo.js
$
Simple-cli help text

Available commands:

  - say-hello  This command just says hi

Available flags:

 --french  Say hello in french
$ node demo.js say-hello
$ Hi!
$ node demo.js say-hello --french
$ Bonjour!

API Reference

define(...commands)

Define a collection of commands that cli-core has access to. Accepts a sequence of command

command(name, man, action, ...commands)

Create a command to be available through the terminal

name [string]

The name of the command.

  • To make a wildcard command, prefix your command name with : eg :name. A wildcard command should be defined last in sequence.
  • To create command aliases, use a | between commands eg: list|ls
  • If the command name ends with a ], then it will absorb all subsequent command arguments - and pass them as a parameter to action. for example - if a command absorb] was defined and was used like so: cli absorb a b c d - then the arguments a b c d will be passed into the absorb commands action.
man [string]

The commands manual - a description of the command. This is used in cli-core's help menu

action [function]

A function to be run for it's specific command. This functions output can be injected into nested commands.

  • returning [string] will pass the string to console if there are no other commands in the sequence.
  • returning [object] will do the following with its properties:
    • object.payload - data to be injected into the following command in sequence
    • object.action [function | string] - an action to be run if this command is the last in its sequence. If action is a function, it will be run, else it will be passed to console

Accepts payload [object]

  • payload.name - The inputted value of the command run. Useful for wildcard commands.
  • payload.data - Any data that the previous command in the sequence might want to pass down
  • payload.args - An array of command line arguments following the last command that did not match any defined commands.
...commands

A sequence of nested commands to be run next

option(name)

Accepts a string name and returns that option/flags value or null if it was not in sequence.

prompt(label, cb)

Start an interactive prompt. It accepts label and cb. The majority of the code for this feature was take from prompt-sync with the history module included.

  • label The prompt text. If none is provided, will default to ~/cwd + current git branch and commit status (x or nothing)
  • cb Will be called on exit and will provide an error as the first argument if any, and the input form the user as the second argument

if exit is written, the prompt will exit without firing the cb

setHelpText(text)

Set the description text that appears when running any help commands.

help()

Displays the help listing for available commands and flags

getUnusedArgs()

Get all arguments that are not defined as commands and have not been used in the current sequence via option(). Returns [array]

init()

Begin constructing the command sequence based on command line arguments. This should be run after defining all commands you want your cli to use