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

termparse

v2.0.3

Published

CLI maker and Command line arg parser

Downloads

9

Readme

𝗧𝗲𝗿𝗺𝗽𝗮𝗿𝘀𝗲 _

A minimal node js CLI maker.

Note: Version 2 has breaking changes so make sure to read below and make changes to your application accordingly

v2.0.3

  • modified the look of usage menu, made it cleaner
  • now args are accessible within the run property of addCommand using this.args. Check below for example.

v2.0.2 (breaking changes)

  • Removed previous methods and added chaining of methods. setFlags can be chained with addCommand
  • setFlags takes in multiple flag property. check below for example

Technology used:

No external dependencies used apart from chalk.js for coloured outputs.

Features:

  • Parses command line arguments like
    • -flag=value
    • -flag value
  • Allows user to set commands
  • Can have same named flags for different commands
  • Generates usage details of the CLI application

Getting started

  • use NPM bash npm i termparse and you are ready to go

Demo

const Termparse = require("termparse");

//create a instance
const tp=new Termparse();

//1st command
tp.addCommand({
    name:"cmd1",
    usage:"this is command 1",
    run:function(){
    	//adding functionality to cmd1
	//this.getFlag(flagName) returns flag object
        console.log(`accessing flags using getFlag`,this.getFlag("flag1"));
        //way to access arguments
        console.log(this.args)
    }
}).setFlags({ //chaining setFlags with addCommand
    name:"flag1",
    usage:"this is flag 1 for command 1",
    type:"string",
    value:"hahaha"
},{
    name:"flag2",
    usage:"this is flag 2 for command 1"
    //no type and value passed implies default: type:"boolean" and value:false
});

// 2nd command
tp.addCommand({
    name:"cmd2",
    usage:"this is command 2",
    run:function(){
        //another way to access flag object this.flags.<flag_name>
        console.log(`another way to access flag`,this.flags.gas1);
	//another way to access args (non flag type)
	console.log(tp.args);
    }
}).setFlags({
    name:"gas1",
    usage:"this is flag 1 (gas 1) for command 2"
    type:"number",
    value:2000
});

// accessing help/usage menu
tp.showHelp()

//get args from comamnd line
var args=process.argv.slice(2);
//pass arguments to termparse
tp.parse(args);

Usage:

addCommand(options)

Adds commands to the CLI application. Takes command property object as input like so

{
  "name": "name of command here",
  "usage": "usage details of the command",
  "run": "adds functionality to commad"
}
  • usage takes the details of what the command does which is recommended to generate a auto-usage guide.
  • run takes function and the function is called when the command is used in terminal.

NOTE: do not pass fat arrow function or ()=>{} to run. Rather use function(){}.

setFlags(options...)

Adds flags/options to a specific command of your CLI application. This function is used by chaining it to the addCommand({...}) function.

Takes in multiple flag property objects as shown in the very first example.

{
  "name": "name of flag",
  "type": "type of flag",
  "value": "value of flag",
  "usage": "usage details"
}
  • type can be either boolean/string/number. If no type is passed then default is boolean.

  • value if flag type is boolean then it takes true/false, default being false in boolean. If flag type is string then it takes string as value, default being empty string.

getFlag(flag_name)

Gets flag/option object of a specific command of your CLI application. Can be used within the run property of the addCommand({...})

  • flag takes name of the flag

returns flag property object

//content of flag property object
{
  "type": "type of flag",
  "value": "value of flag",
  "usage": "usage of the flag",
  "present": "whether flag is passed as arg or not"
}

using getFlag() lets user use the flags value to do various functions.