argparse vs yargs
Command-Line Argument Parsers Comparison
1 Year
argparseyargsSimilar Packages:
What's Command-Line Argument Parsers?

Command-line argument parsers are essential tools in Node.js development that help developers handle input parameters passed to scripts via the command line. They simplify the process of parsing command-line arguments, providing a structured way to define options, flags, and commands. These libraries enhance usability by allowing developers to create intuitive command-line interfaces (CLIs) for their applications, making it easier for users to interact with scripts and tools. Both Argparse and Yargs offer unique features and design philosophies that cater to different developer needs and preferences.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
argparse119,668,281500-75 years agoPython-2.0
yargs116,904,74411,262292 kB2922 years agoMIT
Feature Comparison: argparse vs yargs

Ease of Use

  • argparse:

    Argparse is designed to be simple and intuitive, making it easy for developers to define command-line arguments and options. Its API is straightforward, allowing you to quickly set up argument parsing with minimal boilerplate code.

  • yargs:

    Yargs offers a more extensive API with a focus on usability. It supports chaining methods for defining commands and options, making it easy to create complex command-line interfaces. Its built-in help and versioning features enhance user experience.

Feature Set

  • argparse:

    Argparse provides essential features for argument parsing, including positional arguments, optional flags, and type validation. However, it lacks advanced features like command handling and middleware support, making it less suitable for complex applications.

  • yargs:

    Yargs boasts a rich feature set, including support for commands, middleware, and dynamic argument parsing. It allows you to create subcommands and manage complex argument structures, making it a powerful choice for building sophisticated CLIs.

Documentation and Community Support

  • argparse:

    Argparse has decent documentation, but its community is smaller compared to Yargs. This may lead to fewer resources and examples available for troubleshooting or learning.

  • yargs:

    Yargs has extensive documentation and a larger community, providing a wealth of resources, tutorials, and examples. This makes it easier for developers to find help and best practices when using the library.

Performance

  • argparse:

    Argparse is lightweight and performs well for simple argument parsing tasks. Its minimalistic design means it has lower overhead, making it suitable for small scripts and utilities.

  • yargs:

    Yargs is also performant but may introduce slight overhead due to its richer feature set. However, this overhead is generally negligible for most applications, especially when the benefits of its advanced features are considered.

Customization and Extensibility

  • argparse:

    Argparse offers basic customization options for argument parsing but lacks the extensibility features found in more advanced libraries. It is suitable for straightforward use cases without the need for extensive customization.

  • yargs:

    Yargs excels in customization and extensibility, allowing developers to create custom commands, middleware, and validation logic. This flexibility makes it a better choice for applications that require tailored command-line interfaces.

How to Choose: argparse vs yargs
  • argparse:

    Choose Argparse if you need a straightforward and minimalistic approach to argument parsing, especially if you are familiar with Python's argparse library. It is suitable for simple scripts where you want to define options and arguments without additional complexity.

  • yargs:

    Choose Yargs if you require a more feature-rich solution with advanced capabilities such as command chaining, middleware support, and built-in help generation. Yargs is ideal for larger applications where a more extensive command-line interface is needed.

README for argparse

argparse

Build Status NPM version

CLI arguments parser for node.js, with sub-commands support. Port of python's argparse (version 3.9.0).

Difference with original.

  • JS has no keyword arguments support.
    • Pass options instead: new ArgumentParser({ description: 'example', add_help: true }).
  • JS has no python's types int, float, ...
    • Use string-typed names: .add_argument('-b', { type: 'int', help: 'help' }).
  • %r format specifier uses require('util').inspect().

More details in doc.

Example

test.js file:

#!/usr/bin/env node
'use strict';

const { ArgumentParser } = require('argparse');
const { version } = require('./package.json');

const parser = new ArgumentParser({
  description: 'Argparse example'
});

parser.add_argument('-v', '--version', { action: 'version', version });
parser.add_argument('-f', '--foo', { help: 'foo bar' });
parser.add_argument('-b', '--bar', { help: 'bar foo' });
parser.add_argument('--baz', { help: 'baz bar' });

console.dir(parser.parse_args());

Display help:

$ ./test.js -h
usage: test.js [-h] [-v] [-f FOO] [-b BAR] [--baz BAZ]

Argparse example

optional arguments:
  -h, --help         show this help message and exit
  -v, --version      show program's version number and exit
  -f FOO, --foo FOO  foo bar
  -b BAR, --bar BAR  bar foo
  --baz BAZ          baz bar

Parse arguments:

$ ./test.js -f=3 --bar=4 --baz 5
{ foo: '3', bar: '4', baz: '5' }

API docs

Since this is a port with minimal divergence, there's no separate documentation. Use original one instead, with notes about difference.

  1. Original doc.
  2. Original tutorial.
  3. Difference with python.

argparse for enterprise

Available as part of the Tidelift Subscription

The maintainers of argparse and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.