# argparce

> Parse command line arguments in nodejs

Latest version **1.0.0** (published 2018-03-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install argparce
pnpm add argparce
yarn add argparce
bun add argparce
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-03-14 |
| First published | 2018-03-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | lufinkey |
| Keywords | args, params, parse, cli |

## Links

- npm: https://www.npmjs.com/package/argparce
- npm.io page: https://npm.io/package/argparce

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-03-14

## README

# argparce

An easy argument parser for nodejs

## Install

```bash
npm install --save argparce
```

## Usage

```javascript
const ArgParser = require('argparce');
```

### API Reference

#### ArgParser.parse(args, options)

- `args` [\<Array>] an array of arguments

- `options` [\<Object>]
	- `args` [\<Array>] an array of argument flag definitions
		- [\<Object>]
			- `type` [\<string>] The data type of the argument.
				- Valid types are `'string'`, `'integer'`, `'uinteger'`, `'float'`, `'ufloat'`, `'url'`.
				- `'boolean'` arguments do not require a following argument and can be called as `--arg` (which gives the value `true`) or `--arg=false` (which gives the value `false`)
				- all other argument types must be called either as `--arg=value` or `--arg value`
			- `name` [\<string>] The full name of the argument.
				- Example: `'connect-timeout'` will create an argument named `--connect-timeout`
			- `short` [\<string>] The short name of the argument.
				- Example: `'v'` will create a short argument named `-v`
			- `default` \<Any> The default value of the argument
	- `maxStrays` [\<integer>] the maximum number of stray (not attached to flags) arguments allowed, or `-1` to allow an infinite amount. **Default:** `0`
	- `unmappedArgsDefault` [\<string>] the default behavior for handling unrecognized argument flags. **Default:** `null`
		- `null` unrecognized flags will cause an error
		- `'stray'` unrecognized flags will be added as stray arguments.
		- `'boolean'` unrecognized flags will be read as boolean arguments and added to `args` in the result
		- `'string'` unrecognized flags will be read as string arguments (in the form `--arg=<string>`)
	- `stopAtError` [\<boolean>] stop parsing when an error is encountered.
	- `stopIfTooManyStrays` [\<boolean>] stop parsing when a stray is encountered that can't be added due to `maxStrays`, and doesn't add an error to `errors` in the result.
	- `errorExitCode` [\<integer>] An exit code to exit with if errors occur during parsing, or `null` to not exit. **Default:** `null`
- Returns [\<Object>]
	- `args` [\<Object>] An object mapping of argument names to their values
	- `errors` [\<Array>] An array of string messages for errors that occurred during parsing.
	- `endIndex` [\<integer>] The `args` index where parsing stopped, or `args.length` if parsing completed without stopping
	- `strays` [\<Array>] An array of stray arguments. Stray arguments are command line parameters that didn't match any arguments defined in `options.args`. Array is empty by default unless `options` has some non-default values.
	- `stopped` [\<boolean>] Indicates if parsing was stopped before finishing

Parses the command line arguments with the given options 

- **Example:**

	**Javascript Code:**

	```javascript
	// test.js
	var result = ArgParser.parse(process.argv.slice(2), {
		args: [
			{
				type: 'boolean',
				name: 'verbose',
				short: 'v'
			},
			{
				type: 'uinteger',
				name: 'request-timeout',
				default: 10000
			},
			{
				type: 'string',
				name: 'name'
			}
		],
		maxStrays: 2,
		stopAtError: true
	});
	console.log(result);
	```

	**Command Line:**
	```bash
	node test.js --request-timeout=23470 "bing" --verbose "bang" --name "hello world" "bong"
	```

	**Output:**
	```json
	{
		"args": {
			"request-timeout": 23470,
			"verbose": true,
			"name": "hello world"
		},
		"strays": ["bing", "bang"],
		"errors": ["invalid argument bong"],
		"stopped": true,
		"endIndex": 6
	}
	```

#### ArgParser.validate(type, value)

- `type` The argument type to valid
	- Valid types are `'string'`, `'integer'`, `'uinteger'`, `'float'`, `'ufloat'`, `'url'`.
- `value` The value to validate against the type
- Returns: The validated value, or `null` if validation failed

Validate a value against a type.




[\<boolean>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type
[\<number>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
[\<integer>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
[\<string>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type
[\<Object>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
[\<Array>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
[\<Function>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
[\<Promise>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
[\<Error>]: https://nodejs.org/api/errors.html#errors_class_error

---
_Source: https://npm.io/package/argparce · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
