# clean

> clean parses and sanitize argv for node, supporting fully extendable types, shorthands, validatiors and setters.

Latest version **4.0.2** (published 2015-11-19) · MIT license · 0 weekly downloads

## Install

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

## 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 | 4.0.2 |
| Published | 2015-11-19 |
| First published | 2013-10-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Kael |
| Maintainers | kael |
| Keywords | argv, parser, argument-vector, cleaner, simple |

## Links

- npm: https://www.npmjs.com/package/clean
- Repository: https://github.com/kaelzhang/node-clean
- Homepage: https://github.com/kaelzhang/node-clean#readme
- Issues: https://github.com/kaelzhang/node-clean/issues
- npm.io page: https://npm.io/package/clean

## Dependencies (4)

- [mix2](https://npm.io/package/mix2.md) ^1.0.0
- [async](https://npm.io/package/async.md) ^0.9.0
- [skema](https://npm.io/package/skema.md) ^1.0.0
- [minimist](https://npm.io/package/minimist.md) ^1.1.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 4.0.2 (latest) — 2015-11-19
- 4.0.1 — 2014-08-29
- 4.0.0 — 2014-08-13
- 3.0.3 — 2014-07-07
- 3.0.2 — 2014-07-05
- 3.0.1 — 2014-07-05
- 3.0.0 — 2014-07-04
- 2.1.6 — 2013-10-29
- 2.1.5 — 2013-10-17
- 2.1.4 — 2013-10-17
- 2.1.3 — 2013-10-14
- 2.1.2 — 2013-10-14
- 2.1.1 — 2013-10-10
- 1.1.4 — 2013-10-09
- 0.0.0 — 2013-10-09

## README

# clean [![NPM version](https://badge.fury.io/js/clean.png)](http://badge.fury.io/js/clean) [![Build Status](https://travis-ci.org/kaelzhang/node-clean.png?branch=master)](https://travis-ci.org/kaelzhang/node-clean) [![Dependency Status](https://gemnasium.com/kaelzhang/node-clean.png)](https://gemnasium.com/kaelzhang/node-clean)

Clean is small but powerful node.js module that parses and sanitize argv for node, supporting:

- fully extendable types
- shorthands
- validatiors
- setters

# Installation and Usage

```sh
npm install clean --save
```

```js
var clean = require('clean');
```

# Usage: clean(options)

## Argv Shorthands

We can define shorthands with the option `options.shorthands`.

```js
var shorthands = {
	c: 'cwd',
	n: 'no-recursive'
};

clean({
	shorthands: shorthands
}).argv(['node', 'xxx', '-c', 'abc', '-n']);
// The result is:
// {
//		cwd: 'abc',
//		recursive: false
// }
```

## Types

```js
clean({
	schema: {
		cwd: {
			type: require('path')
		},
		
		retry: {
			type: Boolean
		}
	}
}).parse(
	['node', 'xxx', '--cwd', 'abc', '--retry', 'false'], 
	function(err, results){
		console.log(results.cwd); // the `path.resolved()`d 'abc'
		console.log(results.retry === false); // is a boolean, not a string
	}
);
```

## Validators and Setters

Validators and setters of `clean` is implemented by `[checker](https://github.com/kaelzhang/node-checker)`, check the apis of `checker` for details.

You could check out the demo located at "example/clean.js". That is a very complicated situation of usage.

```sh
node example/clean.js --username guest
```


# Programatical Details

## constructor: clean(options)

- options `Object=`
  - schema `Object` schema to define the argv
  - offset `Number=` The offset from which the parser should start to parse. Optional. Default to `2`.
  - shorthands `Object=` The schema used to clean the given object or the parsred argv
  - parallel `Boolean=false` whether should check the argv in parallel, default to `false`

## options.schema

```js
name: {
  // If `required == true` and `--name` is not specified in argv, there will be an error
  required: true,
  validate: function(value){
    return /[a-z]/i.test(value);
  },
  set: function(value){
    return value.replace(/^[a-z]/, function(m){
      return m.toUpperCase();
    });
  }
}
```

- required `Boolean` 
- default `*` if `required` is `true`, this property will be ignored.
- validate `function(v, is_default)|Array` function for validation, or array of functions
- set `function(v, is_default)|Array` setter function, or array of functions.

There are three methods available for `this` object of validator and setter.

- `this.async()` returns function `done`, and turns the current validator or setter into an asynchronous method. We can also use `done` to define better error messages.
- `this.get(key)` could fetch the value of other properties.
- `this.set(key, value)` could set values of other properties.

## .argv(argv)

- argv `Array` `process.argv` or something like that.

Parses the argument vector, without cleaning the data.

Returns `Object` The parsed object with shorthand rules applied.

## .clean(data, callback)

- data `Object` The given data.
- callback `function(err, results)`

Cleans the given data according to the `schema`.

## .parse(argv, callback)

Parses argument vector (argv) or something like `process.argv`, and cleans the parsed data according to the `schema`.

This method is equivalent to `c.clean(c.argv(argv), callback)`.

# Advanced Section

## .registerType(type, typeDef)

Registers a custom type.

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