Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jaredkrinke/flags_usage

Adds "--help" to Deno's std/flags module
https://github.com/jaredkrinke/flags_usage

deno flags help usage

Last synced: about 2 months ago
JSON representation

Adds "--help" to Deno's std/flags module

Awesome Lists containing this project

README

        

# flags_usage
Adds "--help" to Deno's std/flags module, for printing out command line usage/option information.

# Example
main.ts:
```typescript
import { processFlags } from "https://deno.land/x/flags_usage/mod.ts";

const options = {
preamble: "Usage: my-tool ", // New: Optional preamble for describing non-flag/positional arguments
description: { output: "Output directory" }, // New: Description of flag
argument: { output: "dir" }, // New: "" in the output below
string: [ "output" ],
default: { output: "out" },
};

// `processFlags` parses normally, except it will print usage and exit on `--help`
const { output } = processFlags(Deno.args, options);
```

Output:
```
$ deno run main.ts --help
Usage: my-tool

Options:
--output Output directory (default: "out")
-h, -?, --help Display usage information
```

Note that `parseFlags`, `logUsage`, etc. (see API) can be used if automatically exiting on `--help` is not desirable.

# API
## Options
All `options` arguments follow std/flags's interface, with the following new, *optional* properties:

* `preamble`: An optional string to print out before flag information on `--help`
* `description`: An object mapping flags to descriptions that will be displayed on `--help`
* `argument`: An object mapping flags to names for their arguments to be displayed on `--help` (e.g. mapping `out` to `path` will display `--out ...` instead of the generic `--out ` for string arguments)

## `processFlags(Deno.args, options)`
Parse options as usual, but add `--help` and automatically print usage and exit if `--help`/`-h`/`-?` are specified.

## `parseFlags(Deno.args, options)`
Parse options as usual, with `--help`/`-h`/`-?` added as a Boolean flag (note: with this function, the caller must decide what to do if the `help` flag is set to true--use `processFlags` to automatically print usage and exit).

## `formatUsage(options)`
Format usage information as a string.

## `logUsage(options)`
Format usage information and print to the console.