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

node-cmder

v3.0.6

Published

Command line tools for Node.js & Typescript

Downloads

11

Readme

中文文档

Command line tools for Node.js & Typescript

Easily generate your command with a string signature. example:

command {arg1} {arg2 : arg2} {--bool-flag} {--A|age=10} description

signature

  • command name is omissible when not in group commond
  • array-type argument or optional arg must be at last
  • option shortcut or name can not be duplicated

Usage:

import { CommandBuilder } from 'node-cmder'

const signature = '{name} {--bool-flag} {--A|age=10} commanddescription'

CommandBuilder.command(signature)
  .setAction(({ args, options }) => {
    console.log(args, options)
  })
  .execute()

then run it

$ node test.js --help

Option parsing

{--bool}                     //  boolean option
{--bool : this is boolean}   //  option with description
{--B|bool}                   //  option with shortcut
{--version=}                 //  option need value (required)
{--version=10}               //  option with default value
{--version="has blank"}      //  option with default value contains blank
{--version?=}                //  option need value (optional)
{--tags=*}                   //  array option
{--tags?=*}                  //  array option (optional)

Argument parsing

{arg}                        // arg
{arg : this is arg}          //  arg with description
{arg?}                       // optional arg
{arg*}                       // array arg
{version?=}                  //  option need value (optional)
{tags=*}                     //  array arg
{tags?=*}                    //  array arg (optional)
{arg=10}                     //  arg with default value
{arg="has blank"}            //  arg with default value contains blank

Option transform and callback

.mergeOption('age',{transform:parseInt}) transform age to int .mergeOption('age',{callback:(v)=>console.log(v)}) option callback

built-in options

  • ---help enable by default, generate help automately. call .removeHelpOption() to disable it or .customHelp() to customize.
  • ---V|version to print command version , it is disabled by default. call .setVersion(v:string) to enable it.

Execute command

.execute (argv = process.argv.slice(2)) execute the commond with console. Note: process will exit automately when action return return "a never resolve Promise" to prevent, like new Promise(()=>{/**/})

Command Builder

  • .command(signature: string, action?: Types.Action | undefined): Command; build a simple commond
  • .groupCommand(): GroupCommand; build a group commond with can add sub commonds

Command & GroupCommand

  • .run(argv:string[]) run the command. different from .execute:

    • .run does not handle any Error,suitable for being called by program
    • .execute handle and print erros to terminal and exit process after action return.
  • .printHelp() print the help with console.log

  • .getHelpText() get the help text

  • .addOption() add the extra option

  • .addArg() add the extra argument (only Command)

  • .mergeOption(name,opt) set option metas

  • more api are in Section Interfaces

build command step by step

.addArg or .addOption method does not need { }

CommandBuilder.command('test {name : arg}')
    .addArg('name2')
    .addOption('--A|age')
    .setVersion('2.0.0')
    .setAction(({ args, options }) => {
        console.log(args, options)
    })

customHelp

.customHelp accept 2 types argument

  • string print the string instead origin help
  • function print the ret of function and pass orgin help as first argument
CommandBuilder.command('test {name : arg}')
    .addArg('name2')
    .addOption('--A|age=')
    .setVersion('2.0.0')
    .customHelp((origin) => {
        return origin + `\nExample:\n node test.js joe name 2 -A=20`
    })
    .setAction(({ args, options }) => {
        console.log(args, options)
    })
    .execute()

group commond

.addCommand accept a signature and action or commond instance

CommandBuilder.groupCommand()
    .addCommand('test1 {name : arg}', ({ args, options }) => {
        console.log(args, options)
    })
    .addCommand((g) => {
        return CommandBuilder.command('test2 {name : arg}')
            .setAction(({ args, options }) => {
                console.log(args, options)
            })
    })
    .execute()