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

clinei

v1.0.2

Published

clinei is a command line interface handler to make it easy to build cli programs

Downloads

23

Readme

clinei

command-line interface handler

clinei is handler to facilitate the building of cli programs with stability and also you can specify the type of entry of each command and customization that helps you write a clean program clinei is not a cli package, it is a package that helps you build a cli package

Features

  • Command
  • Option
  • Argument
  • Help
  • Version
  • Customization
  • Type
  • Alias
  • Default
  • Required
  • Description
  • Example

Support

  • ECMAScript Modules (ESM)
  • CommonJS (CJS)
  • ❌️ Deno

Map

Examples

index.js

#!/usr/bin/env node
import { Build } from "clinei";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
new Build({
  path: __dirname + "/commands",
  prefix: " real-cli",
});

if You use CommonJS

- import
+ require

+ const { Build } = require("clinei");
- import { Build } from "clinei";
- import path from "path";
- import { fileURLToPath } from "url";
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
+ new Build({
+   path: __dirname + "/commands",
+   prefix: " real-cli",
+ });

and
+ module.exports = Command(...);
- export default Command(...);

commands/print.js

import { Command } from "clinei";
export default Command(
  {
    cmd: "print", //or ["print","--print","-p","myprint"] //with aliases for this command
    desc: "Log in and print data in console",
    options: [
      {
        name: ["--username", "-u"], //with aliases
        desc: "your real name",
        required: true,
      },
      {
        name: "--age", //no aliases
        desc: "your real age",
        type: "number",
        msg: "See the documentation for more information on github",
        default: 99,
      },
      {
        name: "--store", //no aliases
        desc: "store your data or no (optional)",
      },
    ],
    usage: "print pro -u Arth --age=101 --store --skills 1 2 3 4 5 6",
  },
  ({ getOptions, exist, getArgs, parseArgs }) => {
    const username = getOptions("--username"); // or -u
    const age = getOptions("--age");
    const store = exist("--store");
    const Skills = parseArgs("--skills"); //or any other key like --
    if (getArgs("pro")) {
      console.log(
        `[${getArgs()[0]}] Hi ,${username} Your Data Enjoy!
      
  [username] ${username}
  [age] ${age}
  [store] ${store ? "yes store my data" : "No!"}
  [skills] ${Skills.join(", ")}
`
      );
    } else
      console.log(
        `[noob] Hi ,${username} Your Data Enjoy!
    
[username] ${username}
[age] ${age}
[store] ${store ? "yes store my data" : "No!"}
[skills] ${Skills.join(", ")}
`
      );
  }
);

commands/help.js

import { Command } from "clinei";
export default Command(
  {
    cmd: ["-h", "--help", "help"], // <-- This is the command name like <Prefix> help <command>
    desc: "View Commands", // <-- This is the command desc
    usage: "help <command>",
  },
  ({ printHelp, getArgs, getStructure }, focus) => {
    console.log(printHelp);
    if (
      (getArgs()[0] || focus) &&
      !getStructure.commands.find(({ cmd }) =>
        cmd.find((c) => c === focus || c === getArgs()[0])
      )
    ) {
      console.log(`Warn: ${`["${getArgs()[0] || focus}"]`} not found`);
    }
  }
);

Run

node index.js print -u Arth --age 120 --store

OutPut

[noob] Hi ,Arth Your Data Enjoy!

[username] Arth
[age] 120
[store] yes store my data
[skills]

Installation

NPM

npm i clinei

yarn

yarn add clinei

Build

Build is a class that is responsible for building the cli program, it is necessary to pass the path of the commands folder and the prefix of the program

We use real-cli as an example

import { Build } from "clinei";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
new Build({
  path: __dirname + "/<commands folder>",
  prefix: "real-cli", //<prefix>
});
const { Build } = require("clinei");
new Build({
  path: __dirname + "/<commands folder>",
  prefix: "real-cli", //<prefix>
});

Command

/<commands folder>/**/*.js Command() a function that registers the command in the program

import { Command } from "clinei";
export default Command(
  {
    cmd: [], // or string
    desc: "",
    usage: "",
    options: [],
  },
  ({ <Methods> }) => {
    // code
    }
);
const { Command } = require("clinei");
module.exports = Command(
  {
    cmd: [], // or string
    desc: "",
    usage: "",
    options: [],
  },
  ({ <Methods> }) => {
    // code
    }
);

CommandConfig

{
  cmd: [],
  desc: "",
  usage: "",
  options: [...],
}

cmd

cmd is the command that will be executed in the program, it can be a string or an array of strings

no alias

$ real-cli print
{
  cmd: "print",
}

with alias

-p or --print or print

$ real-cli -p
{
  cmd: ["-p", "--print", "print"],
}

desc

desc is the description of the command, it is used in the help command

{
  desc: "Log in and print data in console",
}

options

the options of the command, required if the command has options

see full documentation of CommandConfigOption

usage

usage is the usage of the command, it is used in the help command

{
  usage: "print pro -u Arth --age=101 --store --skills 1 2 3 4 5 6",
}

Finally

 {
    cmd: "print", //or ["print","--print","-p","myprint"] //with aliases for this command
    desc: "Log in and print data in console",
    options: [
      {
        name: ["--username", "-u"], //with aliases
        desc: "your real name",
        required: true,
      },
      {
        name: "--age", //no aliases
        desc: "your real age",
        type: "number",
        msg: "See the documentation for more information on github",
        default: 99,
      },
      {
        name: "--store", //no aliases
        desc: "store your data or no (optional)",
      },
    ],
    usage: "print pro -u Arth --age=101 --store --skills 1 2 3 4 5 6",
  }

CommandConfigOption

ConfigOption is the configuration of the options of the command

default value

{
  name: "",// or array
  desc: "",
  type: "string",
  msg: false,
  required: false,
  default: undefined
}

name

name of the option, it can be a string or an array of strings must start with - or --

no alias

{
  name: "--username",
}
$ real-cli --username Arth

with alias

-u or --username

{
  name: ["-u", "--username"],
}
$ real-cli -u Arth

desc

the description of the option, it is used in the help command

{
  desc: "your real name",
}

type

the type of the option, it is used to validate the option

{
  type: "string" | "number" | "boolean",
}

Type Test

{
  name: "--age", //no aliases
  desc: "your real age",
  type: "number",
  msg: "See the documentation for more information on github",
  default: 99
}

expected number

$ real-cli --age nine

OutPut in interface

Error: Invalid value for option --age expected number got nine
                                                         ^
Tip: use  real-cli help print to see command options

msg

will be displayed if the option is not valid

{
  msg: "See the documentation for more information on github",
}
$ real-cli --age nine

OutPut in interface

Error: Invalid value for option --age expected number got nine
                                                         ^
See the documentation for more information on github

Tip: use  real-cli help print to see command options

required

required is a boolean that indicates if the option is required

  {
    name: ["--username", "-u"], //with   aliases
    desc: "your real name",
    required: true,
  }
$ real-cli --age 99

OutPut in interface

Missing required option --username,-u

Tip: use  real-cli help print to see command options

default

default is the default value of the option

  {
    name: "--age", //no aliases
    desc: "your real age",
    type: "number",
    msg: "See the documentation for more information on github",
    default: 99
  }
$ real-cli
getOptions("--age"); // 99

Methods

Command(
  {
    cmd: [], // or string
    desc: "",
    usage: "",
    options: [],
  },
  ({ getOptions, getArgs, parseArgs, exist, getStructure, printHelp }) => {
    // code
  }
);

getOptions()

returns values of the options passed in the command

specify the name of the option to get the value

getOptions("--username");
// or one of the aliases
getOptions("-u");

all options

getOptions();

getArgs()

returns the arguments passed in the command

getArgs(); // return array

specify the key to get the arguments

getArgs("arg1"); // "arg1" || false
$ real-cli print arg1 arg2 arg3

OutPut getArgs();

["arg1", "arg2", "arg3"]; // if the key exists
[]; // if the key does not exist

getArgs("arg1");

"hi"; // if the key exists
false; // if the key does not exist

parseArgs()

returns the arguments associated with the key passed in the command

-- 1 2 3 4 5

specify the key to get the arguments -- or any other key or string

parseArgs("--");

OutPut Array

[1, 2, 3, 4, 5] // if the key exists
[] // if the key does not exist

exist()

returns a boolean indicating if the option exists

exist("--username");

OutPut Boolean

true; // if the option exists
false; // if the option does not exist

getStructure

returns the structure of the commands You can use it to build a custom help instead printHelp()

getStructure; // return object

OutPut Object

{
  commands: [
  {
  "cmd": [], // or string
  "desc": "",
  "usage": "",
  "options": [...]
  ,...
  }
  ];
  prefix: string;
  //this is the structure of the command that is being executed
  this: {
  "cmd": [], // or string
  "desc": "",
  "usage": "",
  "options": [...]
  }
}

printHelp

printHelp is a property that prints the help of the commands

printHelp;

HelpCommand

HelpCommand is a command that is used to print the help of the commands

Command(
  {
    cmd: ["-h", "--help", "help"],
    desc: "View Commands",
    usage: "help <command>",
  },
  ({ printHelp }) => {
    console.log(printHelp);
  }
);
$ real-cli help

OutPut

usage:  real-cli help <command>

 real-cli -h, --help, help [options] [aliases]
                                                  View Commands
 real-cli print [options] [aliases]
                                                  Log in and print data in console
 Options:
    --username, -u  REQUIRED,STRING
                                                  your real name
    --age  NUMBER
                                                  your real age
    --store  STRING
                                                  store your data or no (optional)

$ real-cli help print

OutPut

usage:  real-cli print pro -u Arth --age=101 --store --skills 1 2 3 4 5 6

 real-cli print [options] [aliases]
                                                  Log in and print data in console
Options:
    --username, -u  REQUIRED,STRING
                                                  your real name
    --age  NUMBER
                                                  your real age
    --store  STRING
                                                  store your data or no (optional)
Command(
  {
    cmd: ["-h", "--help", "help"], // <-- This is the command name like <Prefix> help <command>
    desc: "View Commands", // <-- This is the command desc
    usage: "help <command>",
  },
  ({ printHelp, getArgs, getStructure }, focus) => {
    console.log(printHelp);
    //focus ->> delete
    if (
      (getArgs()[0] || focus) &&
      !getStructure.commands.find(({ cmd }) =>
        cmd.find((c) => c === focus || c === getArgs()[0])
      )
    ) {
      console.log(`Warn: ${`["${getArgs()[0] || focus}"]`} not found`);
    }
  }
);
$ real-cli delete
           ^^^^^^ ->> focus
$ real-cli help delete
                ^^^^^^ ->> getArgs()[0]

OutPut

usage:  real-cli help <command>

 real-cli -h, --help, help [options] [aliases]
                                                  View Commands
 real-cli print [options] [aliases]
                                                  Log in and print data in console
 Options:
    --username, -u  REQUIRED,STRING
                                                  your real name
    --age  NUMBER
                                                  your real age
    --store  STRING
                                                  store your data or no (optional)

Warn: ["delete"] not found

Make Cli Global

Make a global program local use npm

add this to your package.json

{
  "bin": {
    "real-cli": "index.js"
  }
}

run

$ npm link

or publish and install it globally

Example with npm

npm install <package> -g

now you can use your program

$ real-cli help

Links

License

clinei is a CLI program builder