Skip to content

Commit

Permalink
More documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
febeling committed Dec 8, 2023
1 parent 7d213d7 commit e57590c
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,60 @@ Generate icons and web manifest for web applications
-h, --help print help
```

Synopt doesn't make assumptions about programs with subcommands (e.g. `git branch`), which often come with separate sets of options. But multiple sets of options are supported by `createCommand(name)` instead of importing the default `synopt`.

```js
import { createCommand } from "synopt";

const init = createCommand("init")
.option("--quiet", { boolean: true })
.option("-h", "--help");

const store = createCommand("store")
.option("-v", "--verbose", { boolean: true })
.option("--name");
```

## Command API

`option([short], long, [description], [options])`

Declares an option. Only the `long` form (`--task NAME`) is required, which consists of two dashes (`--`) and the options name, optionally followed by the argument name to be shown in the usage banner (`NAME`). If the argument name is left off, the option name will be assumed (e.g. `--task TASK`).

The optional `short` form starts with a single dash (`-`), followed by a single letter. This form can appear first or second position of parameters.

The optional description explains the meaning of the option, including if it's mandatory, or has specific legal values or other contstraints.

The options to the declaration are passed as an object. `boolean` indicates an option which doesn't require a value (e.g. cases like typical `--quiet` or `--dry-run`).

`option` can be chained, because it returns the command itself.
``

`summary(text)`

This sets a summary text to be show on the usage banner. Returns the command itself for chaining.

`description(text)`

This sets a description to be show on the usage banner, below the summary. Returns the command itself for chaining.

`name(text)`

This sets a name to be show on the usage banner. Returns the command itself for chaining.

`parse(argv)`

Parse the arguments from the command line against the declared options and never throws an exception. Returns an result object of `{ ok, options, error }`. Check `ok` for success of the parsing of the `options`, and display `error` to inform the user of problems.

The option object has this structure: `string` to `string | boolean | string[]`. The value is `string`, unless it's declared a `boolean` option, or a repeat option (`string[]`).

`usage()`

Return the usage banner as a string.

`createCommand([name])`

Create a command object, with an optional name. In many cases the name should be the executable, but sometimes an executable has subcommands with separate option interfaces each. Theses are best represented by a separated command.

If you use the convenience default `import synopt from 'synopt;`, this is a command without a name set yet.

0 comments on commit e57590c

Please sign in to comment.