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

@relekang/args

v1.2.2

Published

Just another argument parser for multimodule cli applications

Downloads

5

Readme

@relekang/args

CircleCI

Just another opinionated lazy argument parser. This library is made to be opinionated about the structure of node cli application. It strives to give you a most of what you would need out of the box if you agree with its opinions. The core principal is to co-locate the comman configuration with the command code so it is stored in the same file.

Installation

yarn add @relekang/args

Usage

There is two ways of using args:

A directory with commands

const args = require('@relekang/args')

args({
  commandsPath: "./commands",
})(process.argv)

An object with commands

This gives you the control of the importing of the commands. It can be useful if you use something like rollup to create a single bundle of the cli.

const args = require('@relekang/args')

const info = require('./commands/info')
const update = require('./commands/update')

const commands = {info, update}

args({ commands })(process.argv)

Requiring setup on first run

args takes two options that help with initial setup and installation of your cli: needsSetup and setup.

Example

const args = require('@relekang/args')
const fs = require("fs")
const {promisfy} = require("util")

const {createConfig} = require("./config")
const readFile = promisify(fs.readFile)

args({
  commandsPath: "./commands",
  needsSetup: async () => {
      try {
        const config = JSON.parse(await readFile("~/config"))
        return true
      } catch (error) {
        return false
      }
  },
  setup: createConfig
})(process.argv)

Global options

  • name - required string - The name of the cli
  • defaultCommand - optional string - Set a command as default command. This is "help" as default.
  • commandPackagePrefixes - optional string[] - Prefixes for loading commands from separate packages. See package-import.ts in examples.

Creating a command

The command must either use named export of all the properties or export and object with all the properties. The available properties are:

  • name - required string - The name of the command like ./cli.js <name>
  • help - required string - A small help string shown on the command list help screen
  • manual - optional string - A multiline description of how to use the command with usage examples etc.
  • run - required (async) function - The code that will be executed with options as first argument, can be async.
  • positionalOptions - optional array of options - The positional options for the command. See options below for fields.
  • namedOptions - optional array of options - The positional options for the command. See options below for fields.

Options

  • name - required string - The name of the option
  • help - required string - A small help string shown on the command help screen
  • required - required boolean - Is the option required
  • transform - optional function - A function that converts the input. This runs before the validator. transform: Boolean and transform: Number is helpful.
  • validator - optional (async) function - A function that validates the input. This might return a promise for async operations.

Example

A tiny example below, see example folder for more examples.

export const name = 'update'
export const help = 'Update supercli'
export const positionalOptions = [
    {
        name: 'latest', 
        help: 'Will update to latest and not within range if present.', 
        required: false, 
        transform: Boolean
    }
]

export async function command(options) {
    const range = getVersionRange()
    await exec('npm -g install @org/supercli@'+ options.latest ? 'latest' : range)
}