Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ run([echo], {
- [`desc`](#options-desc)
- [`default`](#options-default)
- [`hidden`](#options-hidden)
- [`optionTypeText`](#options-optionTypeText)


[**`run`**](#run)
Expand Down Expand Up @@ -243,6 +244,11 @@ Extensions:
- `value` - maximal allowed value
:warning: - does not limit defaults

- `.optionTypeText(optionTypeText: string)` - define custom help type labels to customise option help text displayed when using the `help` command
- `optionTypeText` - option type text to be displayed
The default option type text is replaced e.g. declaring `.optionTypeText('opt-val')` results in `--opt number` becoming`--opt opt-val`.
This is useful with `.enum()` options that have a large number of enum values.

### Creating handlers

Normally, you can write handlers right in the `command()` function, however there might be cases where you'd want to define your handlers separately.
Expand Down
66 changes: 35 additions & 31 deletions src/event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,48 +117,52 @@ export type BroCliEventType = BroCliEvent['type'];
const getOptionTypeText = (option: BuilderConfig) => {
let result = '';

switch (option.type) {
case 'boolean':
result = '';
break;
case 'number': {
if ((option.minVal ?? option.maxVal) !== undefined) {
let text = '';
if (option.optionTypeText) {
result = option.optionTypeText;
} else {
switch (option.type) {
case 'boolean':
result = '';
break;
case 'number': {
if ((option.minVal ?? option.maxVal) !== undefined) {
let text = '';

if (option.isInt) text = text + `integer `;

if (option.isInt) text = text + `integer `;
if (option.minVal !== undefined) text = text + `[${option.minVal};`;
else text = text + `(∞;`;

if (option.minVal !== undefined) text = text + `[${option.minVal};`;
else text = text + `(∞;`;
if (option.maxVal !== undefined) text = text + `${option.maxVal}]`;
else text = text + `∞)`;

if (option.maxVal !== undefined) text = text + `${option.maxVal}]`;
else text = text + `∞)`;
result = text;
break;
}

if (option.isInt) {
result = 'integer';
break;
}

result = text;
result = 'number';
break;
}
case 'string': {
if (option.enumVals) {
result = '[ ' + option.enumVals.join(' | ') + ' ]';
break;
}

if (option.isInt) {
result = 'integer';
result = 'string';
break;
}

result = 'number';
break;
}
case 'string': {
if (option.enumVals) {
result = '[ ' + option.enumVals.join(' | ') + ' ]';
case 'positional': {
result = `${option.isRequired ? '<' : '['}${option.enumVals ? option.enumVals.join('|') : option.name}${
option.isRequired ? '>' : ']'
}`;
break;
}

result = 'string';
break;
}
case 'positional': {
result = `${option.isRequired ? '<' : '['}${option.enumVals ? option.enumVals.join('|') : option.name}${
option.isRequired ? '>' : ']'
}`;
break;
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/option-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type BuilderConfig<TType extends OptionType = OptionType> = {
minVal?: number;
maxVal?: number;
enumVals?: [string, ...string[]];
optionTypeText?: string;
};

export type ProcessedBuilderConfig = {
Expand All @@ -30,6 +31,7 @@ export type ProcessedBuilderConfig = {
minVal?: number;
maxVal?: number;
enumVals?: [string, ...string[]];
optionTypeText?: string;
};

export type BuilderConfigLimited = BuilderConfig & {
Expand Down Expand Up @@ -186,6 +188,19 @@ export class OptionBuilderBase<
return new OptionBuilderBase({ ...config, description }) as any;
}

public optionTypeText<TOptionTypeText extends string>(optionTypeText: TOptionTypeText): Omit<
OptionBuilderBase<
TBuilderConfig,
TOutput,
TOmit | 'optionTypeText'
>,
TOmit | 'optionTypeText'
> {
const config = this.config();

return new OptionBuilderBase({ ...config, optionTypeText }) as any;
}

public hidden(): Omit<
OptionBuilderBase<
TBuilderConfig,
Expand Down
2 changes: 1 addition & 1 deletion tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,7 @@ describe('Globals tests', (it) => {
describe('Type tests', (it) => {
const generateOps = {
dialect: string().alias('-d', '-dlc').desc('Database dialect [pg, mysql, sqlite]').enum('pg', 'mysql', 'sqlite')
.required(),
.optionTypeText('dialect').required(),
schema: string('schema').alias('s').desc('Path to a schema file or folder'),
out: string().alias('o').desc("Output folder, 'drizzle' by default"),
name: string().alias('n').desc('Migration file name'),
Expand Down