yargs vs cmd-ts vs caporal vs commander
CLIライブラリ
yargscmd-tscaporalcommander類似パッケージ:

CLIライブラリ

CLIライブラリは、コマンドラインインターフェース(CLI)アプリケーションを構築するためのツールであり、ユーザーがコマンドを入力してアプリケーションと対話できるようにします。これらのライブラリは、コマンドの定義、引数の解析、ヘルプメッセージの生成などの機能を提供し、開発者が効率的にCLIアプリケーションを作成できるようにします。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
yargs171,089,37411,453231 kB3109ヶ月前MIT
cmd-ts198,204349407 kB5224日前MIT
caporal46,7143,462-286年前MIT
commander027,984209 kB101ヶ月前MIT

機能比較: yargs vs cmd-ts vs caporal vs commander

APIのシンプルさ

  • yargs:

    Yargsは、コマンドライン引数の解析に特化したAPIを持ち、オプションの定義が簡単で、フレキシブルな設計です。

  • cmd-ts:

    cmd-tsは、TypeScriptの型定義を活用したAPIを提供し、型安全性を確保しつつ、シンプルなインターフェースを持っています。

  • caporal:

    Caporalは、シンプルで直感的なAPIを提供し、コマンドやオプションの定義が容易です。特に、初心者にとって理解しやすい設計になっています。

  • commander:

    Commanderは、コマンドやオプションの定義が明確で、使いやすいAPIを提供しています。広範なドキュメントがあり、学習コストが低いです。

型安全性

  • yargs:

    YargsはJavaScriptで書かれており、型安全性は提供されていませんが、TypeScriptでの使用もサポートしています。

  • cmd-ts:

    cmd-tsはTypeScript専用で、型安全性を重視しており、開発中のエラーを早期に発見できます。

  • caporal:

    CaporalはJavaScriptで書かれており、型安全性は提供されていませんが、簡単に使えるため、迅速なプロトタイピングに適しています。

  • commander:

    CommanderはJavaScriptで書かれており、型安全性はありませんが、TypeScriptでの使用も可能です。

機能の豊富さ

  • yargs:

    Yargsは、引数の解析、コマンドの定義、ヘルプメッセージの生成など、強力な機能を提供し、複雑なCLIアプリケーションの構築に適しています。

  • cmd-ts:

    cmd-tsは、TypeScriptの特性を活かした機能を提供し、型安全なコマンドラインインターフェースを構築するためのツールを提供しています。

  • caporal:

    Caporalは、コマンドの定義、オプションの解析、ヘルプメッセージの生成などの基本的な機能を提供していますが、他のライブラリに比べて機能は限定的です。

  • commander:

    Commanderは、コマンドのネスト、引数の解析、ヘルプ生成など、豊富な機能を備えており、複雑なCLIアプリケーションに適しています。

コミュニティとサポート

  • yargs:

    Yargsも広く使用されており、活発なコミュニティがあります。多くのリソースが利用可能で、サポートが得やすいです。

  • cmd-ts:

    cmd-tsは、TypeScriptユーザー向けに特化したライブラリであり、特定のニッチなコミュニティがあります。

  • caporal:

    Caporalは比較的新しいライブラリであり、コミュニティのサポートは限られていますが、シンプルさが魅力です。

  • commander:

    Commanderは広く使用されており、活発なコミュニティが存在します。ドキュメントも充実しており、サポートが得やすいです。

学習曲線

  • yargs:

    Yargsは、強力な機能を持ちながらも、使いやすいAPIを提供しているため、学習曲線は緩やかです。

  • cmd-ts:

    cmd-tsはTypeScriptを前提としているため、TypeScriptに不慣れな開発者には学習曲線がやや急になる可能性があります。

  • caporal:

    CaporalはシンプルなAPIを持ち、学習曲線は緩やかです。初心者でもすぐに使い始めることができます。

  • commander:

    Commanderは、豊富なドキュメントとシンプルなAPIを持っているため、学習曲線は比較的緩やかです。

選び方: yargs vs cmd-ts vs caporal vs commander

  • yargs:

    Yargsは、強力な引数解析機能を持ち、複雑なコマンドラインオプションを簡単に設定できます。柔軟性が求められる場合や、詳細な引数解析が必要な場合に選択してください。

  • cmd-ts:

    cmd-tsは、TypeScriptでの使用を前提としたCLIライブラリであり、型安全性を重視するプロジェクトに最適です。TypeScriptの機能をフル活用したい場合に選択してください。

  • caporal:

    Caporalは、シンプルで直感的なAPIを提供し、迅速な開発を可能にします。特に、複雑なコマンドやオプションが必要な場合に適しています。

  • commander:

    Commanderは、広く使われているCLIライブラリで、豊富な機能とドキュメントが揃っています。安定したプロジェクトやコミュニティのサポートが必要な場合に適しています。

yargs のREADME

Yargs

Yargs be a node.js library fer hearties tryin' ter parse optstrings


ci NPM version js-standard-style Coverage Conventional Commits

Description

Yargs helps you build interactive command line tools, by parsing arguments and generating an elegant user interface.

It gives you:

  • commands and (grouped) options (my-program.js serve --port=5000).
  • a dynamically generated help menu based on your arguments:
mocha [spec..]

Run tests with Mocha

Commands
  mocha inspect [spec..]  Run tests with Mocha                         [default]
  mocha init <path>       create a client-side Mocha setup at <path>

Rules & Behavior
  --allow-uncaught           Allow uncaught errors to propagate        [boolean]
  --async-only, -A           Require all tests to use a callback (async) or
                             return a Promise                          [boolean]
  • generate completion scripts for Bash and Zsh for your command
  • and tons more.

Installation

Stable version:

npm i yargs

Bleeding edge version with the most recent features:

npm i yargs@next

Usage

Simple Example

#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv)).parse()

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}
$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!

$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!

Note: hideBin is a shorthand for process.argv.slice(2). It has the benefit that it takes into account variations in some environments, e.g., Electron.

Complex Example

#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

yargs(hideBin(process.argv))
  .command('serve [port]', 'start the server', (yargs) => {
    return yargs
      .positional('port', {
        describe: 'port to bind on',
        default: 5000
      })
  }, (argv) => {
    if (argv.verbose) console.info(`start server on :${argv.port}`)
    serve(argv.port)
  })
  .option('verbose', {
    alias: 'v',
    type: 'boolean',
    description: 'Run with verbose logging'
  })
  .parse()

Run the example above with --help to see the help for the application.

Supported Platforms

TypeScript

yargs has type definitions at @types/yargs.

npm i @types/yargs --save-dev

See usage examples in docs.

Deno

As of v16, yargs supports Deno:

import yargs from 'https://deno.land/x/yargs@v17.7.2-deno/deno.ts'
import { Arguments } from 'https://deno.land/x/yargs@v17.7.2-deno/deno-types.ts'

yargs(Deno.args)
  .command('download <files...>', 'download a list of files', (yargs: any) => {
    return yargs.positional('files', {
      describe: 'a list of files to do something with'
    })
  }, (argv: Arguments) => {
    console.info(argv)
  })
  .strictCommands()
  .demandCommand(1)
  .parse()

Note: If you use version tags in url then you also have to add -deno flag on the end, like @17.7.2-deno

Usage in Browser

See examples of using yargs in the browser in docs.

Documentation

Table of Contents

Supported Node.js Versions

Libraries in this ecosystem make a best effort to track Node.js' release schedule. Here's a post on why we think this is important.