Socket
Socket
Sign inDemoInstall

argparse

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

argparse

Very powerful CLI arguments parser. Native port of argparse - python's options parsing library


Version published
Weekly downloads
88M
decreased by-0.52%
Maintainers
1
Weekly downloads
 
Created

What is argparse?

The argparse npm package is a node.js module for parsing command-line options and arguments. It is inspired by the Python argparse module and allows developers to create user-friendly command-line interfaces for their applications. It provides a way to specify what arguments the program requires, and parses those arguments from the process.argv array.

What are argparse's main functionalities?

Argument Parsing

This feature allows the program to parse command-line arguments. The code sample demonstrates how to create a new ArgumentParser, add arguments with options like verbosity, and then parse the arguments provided by the user.

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

parser.add_argument('-v', '--verbose', {
  help: 'increase output verbosity',
  action: 'store_true'
});
parser.add_argument('echo', {
  help: 'echo the string you use here'
});

const args = parser.parse_args();
console.log(args);

Sub-commands

This feature allows the program to define sub-commands, which are commands that the program can perform. The code sample shows how to create sub-commands for starting and stopping a service, with additional options for each sub-command.

const { ArgumentParser } = require('argparse');
const parser = new ArgumentParser({
  description: 'Sub-command example'
});

const subparsers = parser.add_subparsers({
  title: 'subcommands',
  dest: 'subcommand_name'
});

const start = subparsers.add_parser('start', { aliases: ['run'] });
start.add_argument('-s', '--service', { help: 'Start the service' });

const stop = subparsers.add_parser('stop');
stop.add_argument('-f', '--force', { help: 'Force stop', action: 'store_true' });

const args = parser.parse_args();
console.log(args);

Argument Types and Choices

This feature allows the program to specify the type of an argument and restrict it to a set of choices. The code sample demonstrates how to define an integer argument and a string argument that must be one of the specified choices.

const { ArgumentParser } = require('argparse');
const parser = new ArgumentParser({
  description: 'Argument types example'
});

parser.add_argument('square', {
  help: 'display a square of a given number',
  type: 'int'
});
parser.add_argument('--cuisine', {
  help: 'choose the type of cuisine',
  choices: ['Italian', 'Mexican', 'Japanese'],
  required: true
});

const args = parser.parse_args();
console.log(args);

Other packages similar to argparse

Keywords

FAQs

Package last updated on 01 Dec 2014

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc