Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Feb 1, 2017
1 parent 4e40041 commit d38dfbd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,35 @@ We have an issue here: command-line-args will assume we are setting two options
$ grep-tool --search=-f
```

### Partial parsing

By default, if the user sets an option without a valid [definition](#exp_module_definition--OptionDefinition) an `UNKNOWN_OPTION` exception is thrown. However, in some cases you may only be interested in a subset of the options wishing to pass the remainder to another library. See [here](https://github.com/75lb/command-line-args/blob/master/example/mocha.js) for a example showing where this might be necessary.

To enable partial parsing, set `partial: true` in the method options:

```js
const optionDefinitions = [
{ name: 'value', type: Number }
]
const options = commandLineArgs(optionDefinitions, { partial: true })
```

Now, should any unknown args be passed at the command line:

```
$ example --milk --value 2 --bread cheese
```

They will be returned in the `_unknown` property of the `commandLineArgs` output with no exceptions thrown:

```js
{
value: 2,
_unknown: [ '--milk', '--bread', 'cheese']
}
```


## Install

```sh
Expand All @@ -98,7 +127,7 @@ $ npm install command-line-args --save
### commandLineArgs(optionDefinitions, [options]) ⇒ <code>object</code> ⏏
Returns an object containing all options set on the command line. By default it parses the global [`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array.

By default, an exception is thrown if the user sets an unknown option (one without a valid [definition](#exp_module_definition--OptionDefinition)). To enable __partial parsing__, invoke `commandLineArgs` with the `partial` option - all unknown arguments will be returned in the additional `_unknown` property of the output.
By default, an exception is thrown if the user sets an unknown option (one without a valid [definition](#exp_module_definition--OptionDefinition)). To enable __partial parsing__, invoke `commandLineArgs` with the `partial` option - all unknown arguments will be returned in the `_unknown` property.

**Kind**: Exported function
**Throws**:
Expand Down
4 changes: 2 additions & 2 deletions example/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const assert = require('assert')
const commandLineArgs = require('../')

/*
enable partial parsing to prevent exceptions being thrown
if the user sets undefined, mocha-specific options (e.g. --no-colors)
enable partial parsing to prevent `UNKNOWN_OPTION` exceptions being thrown
if the user sets mocha-specific options (e.g. --no-colors)
*/
const options = commandLineArgs({ name: 'value', type: Number }, { partial: true })

Expand Down
29 changes: 29 additions & 0 deletions jsdoc2md/README.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,35 @@ We have an issue here: command-line-args will assume we are setting two options
$ grep-tool --search=-f
```

### Partial parsing

By default, if the user sets an option without a valid [definition](#exp_module_definition--OptionDefinition) an `UNKNOWN_OPTION` exception is thrown. However, in some cases you may only be interested in a subset of the options wishing to pass the remainder to another library. See [here](https://github.com/75lb/command-line-args/blob/master/example/mocha.js) for a example showing where this might be necessary.

To enable partial parsing, set `partial: true` in the method options:

```js
const optionDefinitions = [
{ name: 'value', type: Number }
]
const options = commandLineArgs(optionDefinitions, { partial: true })
```

Now, should any unknown args be passed at the command line:

```
$ example --milk --value 2 --bread cheese
```

They will be returned in the `_unknown` property of the `commandLineArgs` output with no exceptions thrown:

```js
{
value: 2,
_unknown: [ '--milk', '--bread', 'cheese']
}
```


## Install

```sh
Expand Down
2 changes: 1 addition & 1 deletion lib/command-line-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = commandLineArgs
/**
* Returns an object containing all options set on the command line. By default it parses the global [`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array.
*
* By default, an exception is thrown if the user sets an unknown option (one without a valid [definition](#exp_module_definition--OptionDefinition)). To enable __partial parsing__, invoke `commandLineArgs` with the `partial` option - all unknown arguments will be returned in the additional `_unknown` property of the output.
* By default, an exception is thrown if the user sets an unknown option (one without a valid [definition](#exp_module_definition--OptionDefinition)). To enable __partial parsing__, invoke `commandLineArgs` with the `partial` option - all unknown arguments will be returned in the `_unknown` property.
*
*
* @param {module:definition[]} - An array of [OptionDefinition](#exp_module_definition--OptionDefinition) objects
Expand Down

0 comments on commit d38dfbd

Please sign in to comment.