Skip to content

Commit

Permalink
Updated docs, fixed default help calling wrong help
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukairo-02 committed Jun 27, 2024
1 parent c9eeec5 commit de8a21d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
59 changes: 53 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ Run CLI commands with fully typed handlers

### Defining options

Start defining options using `string` and `boolean` functions:
Start defining options using `string`, `number`, `boolean` and `positional`functions:

```Typescript
import { string, boolean } from '@drizzle-team/brocli'
import { string, boolean, number, positional } from '@drizzle-team/brocli'

const commandOptions = {
opt1: string(),
opt2: boolean('flag').alias('f'),
opt3: number().int(),
opt4: positional().enum('one', 'two')
// And so on...
}
```
Expand All @@ -24,13 +26,19 @@ Keys of the object passed to the object storing options determine to which keys

Initial builder functions:

- `string(name?: string)` - defines option as a string-type option which requires data to be passed as `--option=value`
- `string(name?: string)` - defines option as a string-type option which requires data to be passed as `--option=value` or `--option value`
- `name` - name by which option is passed in cli args
If not specified, defaults to key of this option
:warning: - must not contain `=` character, not be in `--help`,`-h`,`--version`,`-v` and be unique per each command
:speech_balloon: - will be automatically prefixed with `-` if one character long, `--` if longer
If you wish to have only single hyphen as a prefix on multi character name - simply specify name with it: `string('-longname')`

- `number(name?: string)` - defines option as a number-type option which requires data to be passed as `--option=value` or `--option value`
- `name` - name by which option is passed in cli args
If not specified, defaults to key of this option
:warning: - must not contain `=` character, not be in `--help`,`-h`,`--version`,`-v` and be unique per each command
:speech_balloon: - will be automatically prefixed with `-` if one character long, `--` if longer
If you wish to have only single hyphen as a prefix on multi character name - simply specify name with it: `string('-longname')`

- `boolean(name?: string)` - defines option as a boolean-type option which requires data to be passed as `--option`
- `name` - name by which option is passed in cli args
Expand All @@ -39,6 +47,9 @@ Initial builder functions:
:speech_balloon: - will be automatically prefixed with `-` if one character long, `--` if longer
If you wish to have only single hyphen as a prefix on multi character name - simply specify name with it: `boolean('-longname')`

- `positional()` - defines option as a positional-type option which requires data to be passed after a command as `command value`
:warning: - does not consume options and data that starts with `-`

Extensions:

- `.alias(...aliases: string[])` - defines aliases for option
Expand All @@ -55,6 +66,19 @@ Extensions:

- `.hidden()` - sets option as hidden - option will be omitted from being displayed in `help` command

- `.enum(values: [string, ...string[]])` - limits values of string to one of specified here
- `values` - allowed enum values
:warning: - does not test default value, will add it to the output type instead

- `.int()` - ensures that number is an integer

- `.min(value: number)` - specified minimal allowed value for numbers
- `value` - minimal allowed value
:warning: - does not limit defaults

- `.max(value: number)` - specified maximal allowed value for numbers
- `value` - maximal allowed value
:warning: - does not limit defaults

### Creating handlers

Expand All @@ -76,6 +100,22 @@ export const commandHandler = (options: TypeOf<typeof commandOptions>) => {
}
```

Or by using `handler(options, myHandler () => {...})`

```Typescript
import { string, boolean, handler } from '@drizzle-team/brocli'

const commandOptions = {
opt1: string(),
opt2: boolean('flag').alias('f'),
// And so on...
}

export const commandHandler = handler(commandOptions, (options) => {
// Your logic goes here...
});
```

### Defining commands

To define commands, use `command()` function:
Expand All @@ -102,6 +142,7 @@ commands.push(command({
hidden: false,
options: commandOptions,
handler: commandHandler,
help: () => 'This command works like this: ...'
}));
```

Expand All @@ -121,6 +162,8 @@ Parameters:

- `handler` - function, which will be executed in case of successful option parse

- `help` - function or string, which will be executed or printed when help is called for this command

### Running commands

After defining commands, you're going to need to execute `runCli()` function to start command execution
Expand Down Expand Up @@ -153,15 +196,19 @@ commands.push(command({

runCli(commands, {
name: 'my-program',
version: '1.0.0'
version: '1.0.0',
help: () => {
console.log('Command list:');
commands.forEach(c => console.log('This command does ... and has options ...'));
}
})
```

:speech_balloon: - in case cli arguments are not stored in `process.argv` in your environment, you can pass custom argument source to a second argument of `runCli()`, however note that first two elements of such source will be ignored as they are expected to store executable and executed file paths instead of args.
:speech_balloon: - custom help and version output handlers can be passed to a second argument to replace default brocli outputs for those operations with your own.
:speech_balloon: - custom help and version output handlers or strings can be passed to a second argument to replace default brocli outputs for those operations with your own.

## CLI

In `BroCLI`, command doesn't have to be the first argument, instead it may be contained in any order.
To make this possible, hovewer, option that's stated right before command should have an explicit value, even if it is a flag: `--verbose true <command-name>`
Options are parsed in strict mode, meaning that having any unrecognized options will result in anerror.
Options are parsed in strict mode, meaning that having any unrecognized options will result in an error.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@drizzle-team/brocli",
"type": "module",
"author": "Drizzle Team",
"version": "0.2.1",
"version": "0.2.2",
"description": "Typed CLI command runner",
"license": "Apache-2.0",
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion src/command-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ export const rawCli = (commands: Command[], config?: BroCliConfig) => {
const cmds = [...rawCmds, helpCommand(rawCmds, helpHandler)];

let args = argSource.slice(2, argSource.length);
if (!args.length) return help(cmds);
if (!args.length) return executeOrLog(helpHandler);

const helpIndex = args.findIndex((arg) => arg === '--help' || arg === '-h');
if (helpIndex !== -1 && (helpIndex > 0 ? args[helpIndex - 1]?.startsWith('-') ? false : true : true)) {
Expand Down

0 comments on commit de8a21d

Please sign in to comment.