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

sparsely

v2.0.0

Published

A command line argument parser for JavaScript

Downloads

2

Readme

Sparsely

A command line argument parser for JavaScript and TypeScript.

Installation

npm install sparsely

Overview

Sparsely exports one class (Sparsely) and two interfaces for TypeScript usage - ConfigOption and ParsedOption. The Sparsely class instantiates an argument parser, and options that your application accepts can be registered with Sparsely objects by invoking the addOption method with an argument that follows the ConfigOption interface declaration:

interface ConfigOption {
  name: string, // The verbose form of the option ('--help')
  shorthand: string, // The shorthand form of the option ('-h')
  acceptsArgs: boolean, // Does the option accept arguments?
  keyValue: boolean, // Can the option appear in key/value pair form?
  groupable: boolean, // Is the option groupable with other options?
  minArgs: number, // The minimum number of arguments the option accepts
  maxArgs: number // The maximum number of arguments the option accepts
}

Command line arguments are parsed by Sparsely objects' exec method. This method, analyses a single string array arugment and populates three properties with results:

  • errors - a string array containing error messages (if invalid options were passsed to your application, etc.).
  • parsedArgs - a string array containing any program arguments.
  • parsedOptions - an array of objects following the ParsedOption interface declaration:
interface ParsedOption {
  name: string, // The verbose form of the option
  args: string[] // The arguments that were parsed for the option
}

Helper Methods

The following methods can be used to assist in processing the results produced by exec:

  • isParsedOption
    • Takes the name of an option as its argument
    • Returns true if the option has been parsed
  • getParsedOption
    • Takes the name of an option as its argument
    • Returns the parsed option if found
  • isParsedArg
    • Takes the (string) argument
    • Returns true if the argument has been parsed

Including Sparsely in your code

// TypeScript
import { Sparsely, ConfigOption, ParsedOption } from 'sparsely';
// JavaScript
const { Sparsely } = require('sparsely');

Usage

It convenient to create a separate file containing all valid options for your application (as an array of ConfigOption objects):

// valid-options.ts

export default [
  {
    name: "option-A",
    shorthand: "A",
    acceptsArgs: false,
    keyValue: false,
    groupable: false,
    minArgs: 0,
    maxArgs: 0
  },
  {
    name: "option-B",
    shorthand: "B",
    acceptsArgs: true,
    keyValue: false,
    groupable: false,
    minArgs: 1,
    maxArgs: 1
  },
  {
    name: "option-C",
    shorthand: "C",
    acceptsArgs: true,
    keyValue: true,
    groupable: false,
    minArgs: 1,
    maxArgs: 1
  },
  {
    name: "option-D",
    shorthand: "D",
    acceptsArgs: false,
    keyValue: false,
    groupable: true,
    minArgs: 0,
    maxArgs: 0
  },
  {
    name: "option-E",
    shorthand: "E",
    acceptsArgs: false,
    keyValue: false,
    groupable: true,
    minArgs: 0,
    maxArgs: 0
  },
  {
    name: "option-F",
    shorthand: "F",
    acceptsArgs: false,
    keyValue: false,
    groupable: false,
    minArgs: 0,
    maxArgs: 0
  },
  {
    name: "option-G",
    shorthand: "G",
    acceptsArgs: true,
    keyValue: false,
    groupable: false,
    minArgs: 1,
    maxArgs: 3
  }
]

You can then import these objects into the file where you create your parser object and add them through iteration:

// parser.ts

import { Sparsely, ConfigOption } from 'sparsely';
import validOptions from './valid-options';

const parser = new Sparsely();

validOptions.forEach((option: ConfigOption) => {
  parser.addOption(option);
});

export default parser;

Import the parser object into your app's entry point (or wherever you want...):

// main.ts

import parser from './parser';

const argv = [ 
  '-ED', 
  '--option-B', 
  'option-B-arg',
  '--option-C=arg',
  'program-arg-1',
  'program-arg-2',
  '-G',
  'option-G-arg-1',
  '-A',
  '-F'
];

parser.exec(argv);

console.log(parser.errors);
console.log(parser.parsedOptions);
console.log(parser.parsedArgs);

This example produces the following output:

[]
[
  { name: 'option-E', args: [] },
  { name: 'option-D', args: [] },
  { name: 'option-B', args: [ 'option-B-arg' ] },
  { name: 'option-C', args: [ 'arg' ] },
  { name: 'option-G', args: [ 'option-G-arg-1' ] },
  { name: 'option-A', args: [] },
  { name: 'option-F', args: [] }
]
[ 'program-arg-1', 'program-arg-2' ]