Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

@davidosborn/getopt

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@davidosborn/getopt

A full-featured parser for command-line arguments

latest
Source
npmnpm
Version
0.0.17
Version published
Maintainers
1
Created
Source

Getopt

A parser of command-line arguments for modern JavaScript.

Features

  • Supports any number of short and long names for an option.
  • Can generate usage documentation automatically.

Library usage

You can import getopt as a module to parse the arguments of a JavaScript program. Look at the following example for how to use this module.

'use strict'

import process from 'process'
import getopt, {usage} from '@davidosborn/getopt'

main(process.argv.slice(2))

function main(args) {
	// Parse the arguments.
	let opts = getopt(args, {
		options: [
			{
				short: 'h',
				long: 'help',
				description: 'Display this usage information and exit.',
				callback: usage
			},
			{
				short: 'o',
				long: 'output',
				argument: 'file',
				description: 'Write to the specified file.'
			},
			{
				short: 'q',
				long: 'quiet',
				description: 'Do not write to the console.'
			},
			{
				short: 'v',
				long: 'verbose',
				description: 'Write extra information to the console.'
			}
		],
		usage: {
			footer: 'Header content',
			header: 'Footer content',
			program: 'example',
			spec: '[option]... <input-file>...'
		},
		callback: function(opts, args, settings) {
			// Show the usage when there is no input.
			if (opts.parameters.length < 1 || !opts.parameters[0].value)
				usage(settings)
		}
	})

	// Use the parsed arguments.
	let sources = opts.parameters.map(function(p) {return p.value})
	let destination = opts.options.output?.value

	if (opt.options.verbose)
		console.log('Verbose output!');
}

Command-line usage

You can run getopt from the command line to parse the arguments of a shell script. The first argument must be a path to a JSON file that contains the settings. The remaining arguments will be parsed according to the settings. This particular use case is more of a toy for now.

See also

  • The C++ version of this library

Keywords

argument

FAQs

Package last updated on 26 Dec 2022

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