argparse vs yargs-parser
Command Line Argument Parsing Libraries Comparison
3 Years
argparseyargs-parserSimilar Packages:
What's Command Line Argument Parsing Libraries?

Command line argument parsing libraries are essential tools for Node.js applications that require the processing of user input from the command line. These libraries simplify the task of handling command line arguments, providing a structured way to define expected inputs, validate them, and generate help documentation. They enhance user experience by allowing developers to create intuitive command line interfaces (CLIs) that can accept various options and flags, making applications more flexible and user-friendly.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
argparse115,009,280
503-85 years agoPython-2.0
yargs-parser114,361,241
51485.6 kB492 months agoISC
Feature Comparison: argparse vs yargs-parser

Ease of Use

  • argparse:

    argparse provides a simple and intuitive API for defining command line arguments. It allows developers to easily specify argument types, default values, and help descriptions, making it straightforward for users to understand how to use the application.

  • yargs-parser:

    yargs-parser offers a more complex API that can handle intricate command line structures. While it may require a bit more setup, it provides extensive options for customizing argument parsing, making it powerful for advanced use cases.

Feature Set

  • argparse:

    argparse focuses on core argument parsing features, providing essential functionalities like positional arguments, optional flags, and automatic help message generation. It is lightweight and does not include unnecessary features, making it efficient for simpler applications.

  • yargs-parser:

    yargs-parser includes a broader feature set, supporting advanced functionalities such as command chaining, middleware support, and dynamic argument parsing. This makes it suitable for applications with multiple commands and complex argument structures.

Documentation and Community Support

  • argparse:

    argparse has clear and concise documentation, making it easy for new users to get started. The community is active, and the library is widely used, ensuring that developers can find support and examples easily.

  • yargs-parser:

    yargs-parser also has extensive documentation and a large community. Its integration with the yargs ecosystem means that users can benefit from a wealth of resources, tutorials, and community support for more complex CLI applications.

Customization and Extensibility

  • argparse:

    argparse allows for basic customization of argument parsing but is limited in terms of extensibility compared to yargs-parser. It is designed for straightforward use cases where advanced customization is not necessary.

  • yargs-parser:

    yargs-parser is highly customizable and extensible, allowing developers to create complex command line interfaces with nested commands and custom middleware. This flexibility makes it ideal for larger applications that require a tailored CLI experience.

Performance

  • argparse:

    argparse is lightweight and performs well for basic argument parsing tasks. Its simplicity contributes to its efficiency, making it suitable for applications where performance is a key concern.

  • yargs-parser:

    yargs-parser, while feature-rich, may introduce some overhead due to its extensive capabilities. However, it is optimized for performance and can handle complex parsing tasks efficiently, making it suitable for larger applications.

How to Choose: argparse vs yargs-parser
  • argparse:

    Choose argparse if you need a straightforward and powerful solution for parsing command line arguments with a focus on simplicity and ease of use. It is particularly well-suited for applications that require a clear and concise definition of command line options and automatic generation of help messages.

  • yargs-parser:

    Choose yargs-parser if you require a more feature-rich and flexible solution that supports advanced parsing capabilities, such as nested commands and middleware. It is ideal for complex CLI applications where you need to handle a variety of commands and options, and it integrates seamlessly with the larger yargs ecosystem.

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.