# argufy

> Parses Command Line Arguments To Node.JS CLI Programs, Keeps Them In Arguments.xml File To Paste Into README Documentation And Generate Google Closure Compatible Exports.

Latest version **1.7.5** (published 2019-12-07) · MIT license · 0 weekly downloads

## Install

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

Provides the command `argufy`.

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.7.5 |
| Published | 2019-12-07 |
| First published | 2018-06-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 66.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Anton |
| Maintainers | zvr |
| Keywords | argufy |

## Links

- npm: https://www.npmjs.com/package/argufy
- Repository: https://github.com/artdecocode/argufy
- Homepage: https://www.artd.eco/
- Issues: https://github.com/artdecocode/argufy/issues
- npm.io page: https://npm.io/package/argufy

## Recent versions

- 1.7.5 (latest) — 2019-12-07
- 1.7.4 — 2019-12-07
- 1.7.3 — 2019-12-07
- 1.7.2 — 2019-12-07
- 1.7.1 — 2019-07-16
- 1.7.0 — 2019-04-25
- 1.6.3 — 2019-04-24
- 1.6.2 — 2019-04-24
- 1.6.1 — 2019-04-24
- 1.6.0 — 2019-04-24
- 1.5.0 — 2019-04-17
- 1.4.0 — 2019-04-12
- 1.3.3 — 2019-04-05
- 1.3.2 — 2019-01-14
- 1.3.1 — 2019-01-14
- … 9 more at https://npm.io/package/argufy/versions

## README

# argufy

[![npm version](https://badge.fury.io/js/argufy.svg)](https://www.npmjs.com/package/argufy)

```sh
yarn add argufy
```

_Argufy_ Parses Command Line Arguments to _Node.JS_ CLI Programs. It also allows to manage arguments by keeping their definitions in the XML file, so that they can then be embedded into documentation quickly without manual copy-paste. Finally, it produces JavaScript code to extract arguments that is compatible with _Google Closure Compiler_.

<p align="center"><a href="#table-of-contents">
  <img src="/.documentary/section-breaks/0.svg?sanitize=true">
</a></p>

## Table Of Contents

- [Table Of Contents](#table-of-contents)
- [CLI](#cli)
  * [Arguments.xml File](#argumentsxml-file)
  * [Closure-Compatible Arguments](#closure-compatible-arguments)
  * [Usually Integration](#usually-integration)
  * [Argufy Arguments](#argufy-arguments)
  * [Multiple Input Files](#multiple-input-files)
  * [Reusing Shorts](#reusing-shorts)
- [API](#api)
  * [`argufy(config: !Config, argv=: !Array<string>): !Object`](#argufyconfig-configargv-arraystring-object)
    * [`Config`](#type-config)
    * [`Flag`](#type-flag)
  * [`reduceUsage(config: !Config): !Object`](#reduceusageconfig-config-object)
- [Copyright](#copyright)

<p align="center"><a href="#table-of-contents">
  <img src="/.documentary/section-breaks/1.svg?sanitize=true">
</a></p>

## CLI

The best way to use _Argufy_ is to create an `arguments.xml` file and place all definitions in it. For example, _Argufy_ defines the following CLI arguments:

<table>
<tr><th><a name="argumentsxml-file">Arguments.xml File</a> (<a href="types/arguments.xml">view source</a>)</th></tr>
<tr><td>

```xml
<arguments>
  <arg command multiple name="inputs" default="types/arguments.xml">
    The locations of `arguments.xml` files.
  </arg>
  <arg name="output" short="o">
    The destination where to save output.
    If not passed, prints to stdout.
  </arg>
  <arg boolean name="help" short="h">
    Print the help information and exit.
  </arg>
  <arg boolean name="version" short="v">
    Show the version's number and exit.
  </arg>
</arguments>
```
</td></tr>
</table>

When run from the CLI, _Argufy_ will then generate the `get-args.js` file that will parse `process.argv` using the API, and export them as ES6 named exports:

<table>
<tr><th>
  <a name="closure-compatible-arguments">Closure-Compatible Arguments</a>
  </th></tr>
<tr><td>

```js
import argufy from 'argufy'

export const argsConfig = {
  'inputs': {
    description: 'The locations of `arguments.xml` files.',
    command: true,
    multiple: true,
    default: 'types/arguments.xml',
  },
  'output': {
    description: 'The destination where to save output.\nIf not passed, prints to stdout.',
    short: 'o',
  },
  'help': {
    description: 'Print the help information and exit.',
    boolean: true,
    short: 'h',
  },
  'version': {
    description: 'Show the version\'s number and exit.',
    boolean: true,
    short: 'v',
  },
}

const args = argufy(argsConfig)

/**
 * The locations of `arguments.xml` files. Default `types/arguments.xml`.
 */
export const _inputs = /** @type {!Array<string>} */ (args['inputs'] || ['types/arguments.xml'])

/**
 * The destination where to save output.
    If not passed, prints to stdout.
 */
export const _output = /** @type {string} */ (args['output'])

/**
 * Print the help information and exit.
 */
export const _help = /** @type {boolean} */ (args['help'])

/**
 * Show the version's number and exit.
 */
export const _version = /** @type {boolean} */ (args['version'])

/**
 * The additional arguments passed to the program.
 */
export const _argv = /** @type {!Array<string>} */ (args._argv)
```
</td></tr>
</table>

It also exports the configuration object which can be then used to pass to [_Usually_](https://artdecocode.com/usually/) &mdash; the help generator for CLI programs:

<table>
<tr><th><a name="usually-integration">Usually Integration</a> (<a href="example/usage.js">view source</a>)</th></tr>
<tr><td>

```js
import usually from 'usually'
import { reduceUsage } from 'argufy'
import { _help, _output, _input, _version,
  argsConfig } from '../src/get-args'

if (_help) {
  const usage = reduceUsage(argsConfig)
  console.log(usually({
    usage,
    description: `Generates the JavaScript file that exports all arguments
based on the configuration found in the arguments.xml file.`,
    line: 'argufy input [-o output] [-hv]',
    example: 'argufy types/arguments.xml -o src/bin/get-args.js',
  }))
  process.exit()
}

// program
```
</tr></td>
<tr><td>
When run as <code>node example -h</code>, the following output will be shown to the user to indicate how to use the program:
</tr></td>
<tr><td>

```
Generates the JavaScript file that exports all arguments
based on the configuration found in the arguments.xml file.

  argufy input [-o output] [-hv]

	inputs       	The locations of `arguments.xml` files.
	             	Default: types/arguments.xml.
	--output, -o 	The destination where to save output.
	             	If not passed, prints to stdout.
	--help, -h   	Print the help information and exit.
	--version, -v	Show the version's number and exit.

  Example:

    argufy types/arguments.xml -o src/bin/get-args.js
```
</td></tr>
</table>

The CLI is essentially an abstraction over the API, in which the _Argufy_ config must be written and maintained manually. The main advantage of it is that the types can then be embedded into documentation when it is compiled with the [_Documentary_](https://artdecocode.com/documentary/) package. An example of the table and arguments for _Argufy_ usage are shown below.

<p align="center"><a href="#table-of-contents">
  <img src="/.documentary/section-breaks/2.svg?sanitize=true" width="20">
</a></p>

### Argufy Arguments

<table>
 <thead>
  <tr>
   <th>Argument</th> 
   <th>Short</th>
   <th>Description</th>
  </tr>
 </thead>
  <tr>
   <td>inputs</td>
   <td></td>
   <td>The locations of <code>arguments.xml</code> files. Default <code>types/arguments.xml</code>.</td>
  </tr>
  <tr>
   <td>--output</td>
   <td>-o</td>
   <td>The destination where to save output.
    If not passed, prints to stdout.</td>
  </tr>
  <tr>
   <td>--help</td>
   <td>-h</td>
   <td>Print the help information and exit.</td>
  </tr>
  <tr>
   <td>--version</td>
   <td>-v</td>
   <td>Show the version's number and exit.</td>
  </tr>
</table>

<p align="center"><a href="#table-of-contents">
  <img src="/.documentary/section-breaks/3.svg?sanitize=true" width="20">
</a></p>

### Multiple Input Files

Sometimes it might be desired to keep the arguments in multiple files, e.g., to split the program's arguments by different operations that it can perform. _Argufy_ is capable of reading multiple files and generating a single `get-args` file where each property arguments configuration is reduced in turns, so that the `_argv` is exhausted at the end. To enable this functionality, the `name` attribute should be set on the root `arguments` element of all xml files after the first one. The exported configs will have the name in form of `argsConfig{name}`.

_For example, it is possible to join these 2 types together:_

<table>
<tr><th><a href="example/multiple/1.xml">1.xml</a>, <em>anonymous</em></th><th><a href="example/multiple/2.xml">2.xml</a>, <em>Bundle</em></th></tr>
<tr><td>

```xml
<arguments>
  <arg command name="source">
    The path to the source code.
  </arg>
</arguments>
```
</td><td>

```xml
<arguments name="Bundle">
  <arg boolean name="bundle">
    Whether to bundle the source.
  </arg>
</arguments>
```
</td></tr>
</table>

```
$ argufy example/multiple/1.xml example/multiple/2.xml
```

```js
import argufy from 'argufy'

export const argsConfig = {
  'source': {
    description: 'The path to the source code.',
    command: true,
  },
}

export const argsConfigBundle = {
  'bundle': {
    description: 'Whether to bundle the source.',
    boolean: true,
  },
}

const args = argufy({ ...argsConfig, ...argsConfigBundle })

/**
 * The path to the source code.
 */
export const _source = /** @type {string} */ (args['source'])

/**
 * Whether to bundle the source.
 */
export const _bundle = /** @type {boolean} */ (args['bundle'])

/**
 * The additional arguments passed to the program.
 */
export const _argv = /** @type {!Array<string>} */ (args._argv)
```

### Reusing Shorts

It's possible to set the same `short` for multiple commands. In this case, both commands will be assigned the relevant value.

<table>
<tr><th><a href="example/reuse/1.xml">1.xml</a>, <em>original</em></th><th><a href="example/reuse/2.xml">2.xml</a>, <em>additional</em></th></tr>
<tr><td>

```xml
<arguments>
  <arg name="source" short="s">
    The path to the source code.
  </arg>
</arguments>
```
</td><td>

```xml
<arguments name="Additional">
  <arg name="snapshot" short="s">
    The name of the snapshot.
  </arg>
</arguments>
```
</td></tr>
</table>



```
$ program -s test
```

```js
import { _snapshot, _source } from './get-args'

console.log(_snapshot, _source)
```

```js
test test
```

<p align="center"><a href="#table-of-contents">
  <img src="/.documentary/section-breaks/4.svg?sanitize=true">
</a></p>

## API

The package is available by importing its default and named functions:

```js
import argufy, { reduceUsage } from 'argufy'
```

The types and [externs](externs.js) for _Google Closure Compiler_ via [**_Depack_**](https://github.com/dpck/depack) are defined in the `_argufy` namespace.

<p align="center"><a href="#table-of-contents">
  <img src="/.documentary/section-breaks/5.svg?sanitize=true" width="20">
</a></p>

### <code><ins>argufy</ins>(</code><sub><br/>&nbsp;&nbsp;`config: !Config,`<br/>&nbsp;&nbsp;`argv=: !Array<string>,`<br/></sub><code>): <i>!Object</i></code>
The flags from the arguments will be extracted according to the configuration object and the arguments array. If arguments array is not passed, `process.argv` is used to find arguments.

 - <kbd><strong>config*</strong></kbd> <em><code><a href="#type-config" title="The configuration for parsing, where each key is a flag name and values are either strings, or objects with possible properties:">!Config</a></code></em>: The config.
 - <kbd>argv</kbd> <em><code>!Array&lt;string&gt;</code></em> (optional): The `argv` to parse, by default will be looked up in the process.

The package assumes that the arguments begin from the 3rd position, i.e., standard _Node.JS_ use such as `node example.js --title "Hello World"`, or `example --title "Hello World"` if the program has a shebang and/or is run via the `bin` _package.json_ property.

```js
import argufy from 'argufy'

const config = {
  title: { short: 't', command: true },
  list: { short: 'l', boolean: true },
  app: 'a',
  delay: 'y',
  file: 'f',
  wait: { short: 'w', number: true },
  'no-empty': 'e',
  resize: 'z',
  colors: 'c',
  dir: 'D',
}

const res = argufy(config, process.argv)
console.log(JSON.stringify(res, null, 2))
```

```sh
node example.js --title "Hello World" -w 10 -l --app Argufy
# or
node example.js HelloWorld -w 10 -l --app Argufy
```

```json
{
  "title": "HelloWorld",
  "list": true,
  "app": "Argufy",
  "wait": 10,
  "_argv": []
}
```

The configuration for each flag can either be a shorthand string, or an object of the _Flag_ type. The types are shown below. The special `_argv` property will be assigned to contain all unmatched arguments. For example, it can be used to pass any additional parameters through to other program.

<code>Object&lt;string, (string \| <a href="#type-flag" title="The flag passed to the program.">!Flag</a>)&gt;</code> __<a name="type-config">`Config`</a>__: The configuration for parsing, where each key is a flag name and values are either strings, or objects with possible properties:

__<a name="type-flag">`Flag`</a>__: The flag passed to the program.

|    Name     |       Type       |                                                                           Description                                                                            | Default |
| ----------- | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| short       | <em>string</em>  | Shorthand for this argument, usually one letter.                                                                                                                 | -       |
| boolean     | <em>boolean</em> | Whether the flag is a boolean and does not require a value.                                                                                                      | `false` |
| number      | <em>boolean</em> | Specifies whether the flag should be parsed as a number.                                                                                                         | `false` |
| command     | <em>boolean</em> | If set to true, the value is read from the first argument passed to the CLI command (e.g., `$ cli command`).                                                     | `false` |
| multiple    | <em>boolean</em> | When using the `command` property, will parse the commands as an array.                                                                                          | `false` |
| default     | <em>string</em>  | The default value for the argument. Does not actually set the value, only used in reducing the usage info (`argufy` bin on the other hand will set the default). | -       |
| description | <em>string</em>  | The description to be used by `usually`.                                                                                                                         | -       |

<p align="center"><a href="#table-of-contents">
  <img src="/.documentary/section-breaks/6.svg?sanitize=true" width="20">
</a></p>

### <code><ins>reduceUsage</ins>(</code><sub><br/>&nbsp;&nbsp;`config: !Config,`<br/></sub><code>): <i>!Object</i></code>
Given the _Argufy_ config, creates an object that can be passed to [_Usually_](https://artdecocode.com/usually/). Can be used to reduce the config auto-generated and exported from the JavaScript file with the CLI.

 - <kbd><strong>config*</strong></kbd> <em><code><a href="#type-config" title="The configuration for parsing, where each key is a flag name and values are either strings, or objects with possible properties:">!Config</a></code></em>: The config.

```js
import { reduceUsage } from 'argufy'
import { argsConfig } from '../src/get-args'

console.log(reduceUsage(argsConfig))
```
```js
{ inputs: 'The locations of `arguments.xml` files.\nDefault: types/arguments.xml.',
  '--output, -o': 'The destination where to save output.\nIf not passed, prints to stdout.',
  '--help, -h': 'Print the help information and exit.',
  '--version, -v': 'Show the version\'s number and exit.' }
```

<p align="center"><a href="#table-of-contents">
  <img src="/.documentary/section-breaks/7.svg?sanitize=true">
</a></p>

## Copyright

<table>
  <tr>
    <th>
      <a href="https://artd.eco">
        <img width="100" src="https://raw.githubusercontent.com/wrote/wrote/master/images/artdeco.png"
          alt="Art Deco">
      </a>
    </th>
    <th>© <a href="https://artd.eco">Art Deco</a>   2019</th>
    <th>
      <a href="https://www.technation.sucks" title="Tech Nation Visa">
        <img width="100" src="https://raw.githubusercontent.com/idiocc/cookies/master/wiki/arch4.jpg"
          alt="Tech Nation Visa">
      </a>
    </th>
    <th><a href="https://www.technation.sucks">Tech Nation Visa Sucks</a></th>
  </tr>
</table>

<p align="center"><a href="#table-of-contents">
  <img src="/.documentary/section-breaks/-1.svg?sanitize=true">
</a></p>

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