@drizzle-team/brocli
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -432,3 +432,3 @@ // src/brocli-error.ts | ||
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"); | ||
@@ -435,0 +435,0 @@ if (helpIndex !== -1 && (helpIndex > 0 ? args[helpIndex - 1]?.startsWith("-") ? false : true : true)) { |
@@ -5,3 +5,3 @@ { | ||
"author": "Drizzle Team", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Typed CLI command runner", | ||
@@ -8,0 +8,0 @@ "license": "Apache-2.0", |
@@ -9,6 +9,6 @@ # BroCLI | ||
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' | ||
@@ -18,2 +18,4 @@ const commandOptions = { | ||
opt2: boolean('flag').alias('f'), | ||
opt3: number().int(), | ||
opt4: positional().enum('one', 'two') | ||
// And so on... | ||
@@ -29,3 +31,3 @@ } | ||
- `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 | ||
@@ -37,2 +39,8 @@ If not specified, defaults to key of this option | ||
- `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')` | ||
@@ -46,2 +54,5 @@ - `boolean(name?: string)` - defines option as a boolean-type option which requires data to be passed as `--option` | ||
- `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: | ||
@@ -63,3 +74,16 @@ | ||
- `.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 | ||
@@ -85,2 +109,18 @@ | ||
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 | ||
@@ -112,2 +152,3 @@ | ||
handler: commandHandler, | ||
help: () => 'This command works like this: ...' | ||
})); | ||
@@ -132,2 +173,4 @@ ``` | ||
- `help` - function or string, which will be executed or printed when help is called for this command | ||
### Running commands | ||
@@ -165,3 +208,7 @@ | ||
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 ...')); | ||
} | ||
}) | ||
@@ -171,3 +218,3 @@ ``` | ||
: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. | ||
@@ -178,2 +225,2 @@ ## CLI | ||
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. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
139497
214