Skip to content

Commit

Permalink
Allow readonly string array choices for CliStringChoicesArgGroupOptions
Browse files Browse the repository at this point in the history
Fixes #193
  • Loading branch information
carnesen committed Oct 30, 2021
1 parent db47cdb commit 819d0ac
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
7 changes: 7 additions & 0 deletions main/changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# **@carnesen/cli** changelog

## Upcoming

### Fixes

- CliStringChoicesArgGroup factory should allow readonly string array choices [#193](https://github.com/carnesen/cli/issues/193)

## 0.5.1 - 2020-02-06

### Fixes

- Unexpected handling of exception with numeric "code" property (https://github.com/carnesen/cli/issues/166)

## 0.5.0 - 2020-09-12
Expand Down
2 changes: 1 addition & 1 deletion main/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion main/scripts/build-docs.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env bash

# This script generates the documentation package contents from the source code
# This script generates the documentation package contents from the source code.
# It uses TypeDoc in "library mode", which was released in an 0.20 beta but
# didn't make it into the actual release since --mode was removed. For more
# information see https://github.com/TypeStrong/typedoc/pull/1184 and
# https://github.com/TypeStrong/typedoc/releases/tag/v0.20.0

set -eo pipefail
SCRIPT_PATH="${BASH_SOURCE[0]}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const placeholder = '<special>';
const required = false;

const argGroup = CliStringChoiceArgGroup({
choices: ['foo', 'bar'],
choices: ['foo', 'bar'] as const,
description,
hidden,
placeholder,
Expand Down
29 changes: 15 additions & 14 deletions main/src/arg-group-factories/cli-string-choice-arg-group.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { ICliArgGroup } from '../cli-arg-group';
import { CliUsageError } from '../cli-usage-error';

export type CliStringChoices = string[] | readonly string[];

/**
* Options for [[`CliStringChoiceArgGroup`]]
* @typeParam TChoices Type of the "choices" option
*/
export interface ICliStringChoiceArgGroupOptions<TChoices extends string[]> {
export interface ICliStringChoiceArgGroupOptions<
TChoices extends CliStringChoices
> {
/**
* Choices for this argument. For strict typing do e.g.
*
* choices: ['foo' as const, 'bar' as const]
* choices: ['foo', 'bar'] as const
*
* */
choices: TChoices;
Expand All @@ -29,26 +33,23 @@ export interface ICliStringChoiceArgGroupOptions<TChoices extends string[]> {
}

/**
* A factory for required [[`ICliArgGroup`]]s whose value is one of the choices
* provided
* @typeParam TChoices Type of the provided choices
* A factory for [[`ICliArgGroup`]]s whose value is one of the choices provided
* @typeParam TChoices Type of the provided choices.
* */
function CliStringChoiceArgGroup<TChoices extends string[]>(

// required = true overload
function CliStringChoiceArgGroup<TChoices extends CliStringChoices>(
options: ICliStringChoiceArgGroupOptions<TChoices> & { required: true },
): ICliArgGroup<TChoices[number], true>;

/**
* A factory for optional [[`ICliArgGroup`]]s whose value is one of the choices
* provided
* @typeParam TChoices Type of the provided choices
* */
function CliStringChoiceArgGroup<TChoices extends string[]>(
// required = false overload
function CliStringChoiceArgGroup<TChoices extends CliStringChoices>(
options: ICliStringChoiceArgGroupOptions<TChoices>,
): ICliArgGroup<TChoices[number] | undefined, false>;

// Implementation
function CliStringChoiceArgGroup(
options: ICliStringChoiceArgGroupOptions<string[]>,
options: ICliStringChoiceArgGroupOptions<CliStringChoices>,
): ICliArgGroup<string | undefined> {
const valuesString = options.choices.join(', ');
const {
Expand Down Expand Up @@ -85,7 +86,7 @@ function CliStringChoiceArgGroup(
if (args.length > 0) {
return [];
}
return options.choices;
return options.choices as string[];
},
};
return argGroup;
Expand Down

0 comments on commit 819d0ac

Please sign in to comment.