Socket
Socket
Sign inDemoInstall

clipanion

Package Overview
Dependencies
1
Maintainers
1
Versions
83
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

clipanion

Type-safe CLI library / framework with no runtime dependencies


Version published
Maintainers
1
Weekly downloads
1,427,504
decreased by-10.04%

Weekly downloads

Package description

What is clipanion?

The clipanion npm package is a powerful library for building command-line interfaces (CLIs) in Node.js. It provides a rich set of features for defining commands, parsing arguments, validating input, and generating help messages.

What are clipanion's main functionalities?

Command Definition

Clipanion allows you to define commands as classes, which can then be registered with a CLI instance. Each command can have its own execute method that contains the logic to be run when the command is invoked.

class MyCommand extends Command {
  execute() {
    console.log('Command executed!');
  }
}

const cli = new Cli();
cli.register(MyCommand);
cli.runExit(process.argv.slice(2));

Argument Parsing

Clipanion provides a declarative way to define command arguments using class properties. These arguments are automatically parsed and made available within the command's execute method.

class MyCommand extends Command {
  myArgument = Option.String();

  execute() {
    console.log(`Argument value: ${this.myArgument}`);
  }
}

const cli = new Cli();
cli.register(MyCommand);
cli.runExit(process.argv.slice(2));

Input Validation

Clipanion includes built-in validation for command arguments. You can specify whether an argument is required, and Clipanion will automatically enforce this when the command is run.

class MyCommand extends Command {
  myArgument = Option.String({ required: true });

  execute() {
    console.log(`Argument value: ${this.myArgument}`);
  }
}

const cli = new Cli();
cli.register(MyCommand);
cli.runExit(process.argv.slice(2));

Help Generation

Clipanion can automatically generate help messages for your commands. You can provide descriptions, detailed information, and usage examples that will be displayed when the help command is invoked.

class MyCommand extends Command {
  static usage = Command.Usage({
    description: 'My command description',
    details: 'Detailed information about my command',
    examples: [['Example usage', 'my-cli my-command']]
  });

  execute() {
    console.log('Command executed!');
  }
}

const cli = new Cli();
cli.register(MyCommand);
cli.runExit(process.argv.slice(2));

Other packages similar to clipanion

Readme

Source

Clipanion

Type-safe CLI library with no runtime dependencies

npm version Licence Yarn

Installation

yarn add clipanion

Why

  • Clipanion supports advanced typing mechanisms
  • Clipanion supports nested commands (yarn workspaces list)
  • Clipanion supports transparent option proxying without -- (for example yarn dlx eslint --fix)
  • Clipanion supports all option types you could think of (including negations, batches, ...)
  • Clipanion offers a Typanion integration for increased validation capabilities
  • Clipanion generates an optimized state machine out of your commands
  • Clipanion generates good-looking help pages out of the box
  • Clipanion offers common optional command entries out-of-the-box (e.g. version command, help command)

Clipanion is used in Yarn with great success.

Documentation

Check the website for our documentation: mael.dev/clipanion.

Migration

You can use clipanion-v3-codemod to migrate a Clipanion v2 codebase to v3.

Overview

Commands are declared by extending from the Command abstract base class, and more specifically by implementing its execute method which will then be called by Clipanion. Whatever exit code it returns will then be set as the exit code for the process:

class SuccessCommand extends Command {
    async execute() {
        return 0;
    }
}

Commands can also be exposed via one or many arbitrary paths using the paths static property:

class FooCommand extends Command {
    static paths = [[`foo`]];
    async execute() {
        this.context.stdout.write(`Foo\n`);
    }
}

class BarCommand extends Command {
    static paths = [[`bar`]];
    async execute() {
        this.context.stdout.write(`Bar\n`);
    }
}

Options are defined as regular class properties, annotated by the helpers provided in the Option namespace. If you use TypeScript, all property types will then be properly inferred with no extra work required:

class HelloCommand extends Command {
    // Positional option
    name = Option.String();

    async execute() {
        this.context.stdout.write(`Hello ${this.name}!\n`);
    }
}

Option arguments can be validated and coerced using the Typanion library:

class AddCommand extends Command {
    a = Option.String({required: true, validator: t.isNumber()});
    b = Option.String({required: true, validator: t.isNumber()});

    async execute() {
        this.context.stdout.write(`${this.a + this.b}\n`);
    }
}

License (MIT)

Copyright © 2019 Mael Nison

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Last updated on 21 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc