Skip to content

Commit

Permalink
fix: allow flags to parse arrays without variadic modifier
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Molisani <git@molisani.us>
  • Loading branch information
molisani authored and mkubilayk committed Oct 22, 2024
1 parent 09dfb8d commit ecc9099
Show file tree
Hide file tree
Showing 6 changed files with 952 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/core/src/parameter/flag/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,11 @@ interface RequiredVariadicParsedFlagParameter<T, CONTEXT extends CommandContext>

type TypedFlagParameter_Optional<T, CONTEXT extends CommandContext> = [T] extends [readonly (infer A)[]]
? [A] extends [string]
? OptionalVariadicParsedFlagParameter<A, CONTEXT> | OptionalVariadicEnumFlagParameter<A>
: OptionalVariadicParsedFlagParameter<A, CONTEXT>
?
| OptionalVariadicParsedFlagParameter<A, CONTEXT>
| OptionalParsedFlagParameter<T, CONTEXT>
| OptionalVariadicEnumFlagParameter<A>
: OptionalVariadicParsedFlagParameter<A, CONTEXT> | OptionalParsedFlagParameter<T, CONTEXT>
: [T] extends [boolean]
? OptionalBooleanFlagParameter | OptionalParsedFlagParameter<boolean, CONTEXT>
: [T] extends [number]
Expand All @@ -312,8 +315,11 @@ type TypedFlagParameter_Optional<T, CONTEXT extends CommandContext> = [T] extend

type TypedFlagParameter_Required<T, CONTEXT extends CommandContext> = [T] extends [readonly (infer A)[]]
? [A] extends [string]
? RequiredVariadicParsedFlagParameter<A, CONTEXT> | RequiredVariadicEnumFlagParameter<A>
: RequiredVariadicParsedFlagParameter<A, CONTEXT>
?
| RequiredVariadicParsedFlagParameter<A, CONTEXT>
| RequiredParsedFlagParameter<readonly A[], CONTEXT>
| RequiredVariadicEnumFlagParameter<A>
: RequiredVariadicParsedFlagParameter<A, CONTEXT> | RequiredParsedFlagParameter<readonly A[], CONTEXT>
: [T] extends [boolean]
? RequiredBooleanFlagParameter | RequiredParsedFlagParameter<boolean, CONTEXT>
: [T] extends [number]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
 --required-parsed-with-kebab-case-name required parsed flag with kebab-case name
-h --help Print help information and exit
 -- All subsequent inputs should be interpreted as arguments
:::: formatDocumentationForFlagParameters / parsed / optional array parsed flag
 [--optionalArrayParsed] optional array parsed flag
-h  --help Print help information and exit
  -- All subsequent inputs should be interpreted as arguments
:::: formatDocumentationForFlagParameters / parsed / optional parsed flag
 [--optionalParsed] optional parsed flag
-h  --help Print help information and exit
Expand All @@ -62,6 +66,10 @@
 [--optionalVariadicParsed]... optional variadic parsed flag
-h  --help Print help information and exit
  -- All subsequent inputs should be interpreted as arguments
:::: formatDocumentationForFlagParameters / parsed / required array parsed flag
 --requiredArrayParsed required array parsed flag
-h --help Print help information and exit
 -- All subsequent inputs should be interpreted as arguments
:::: formatDocumentationForFlagParameters / parsed / required parsed flag
 --requiredParsed required parsed flag
-h --help Print help information and exit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ cli [--optionalEnum a|b|c]
cli (--requiredEnum a|b|c)
:::: formatUsageLineForParameters / flags / enum / required enum flag with default
cli [--requiredEnum a|b|c]
:::: formatUsageLineForParameters / flags / parsed / optional array parsed flag
cli [--optionalArrayParsed value]
:::: formatUsageLineForParameters / flags / parsed / optional parsed flag
cli [--optionalParsed value]
:::: formatUsageLineForParameters / flags / parsed / optional parsed flag [hidden]
Expand All @@ -20,6 +22,8 @@ cli
cli [--optionalParsed parsed]
:::: formatUsageLineForParameters / flags / parsed / optional variadic parsed flag
cli [--optionalVariadicParsed value]...
:::: formatUsageLineForParameters / flags / parsed / required array parsed flag
cli (--requiredArrayParsed value)
:::: formatUsageLineForParameters / flags / parsed / required parsed flag
cli (--requiredParsed value)
:::: formatUsageLineForParameters / flags / parsed / required parsed flag with alias
Expand Down
55 changes: 55 additions & 0 deletions packages/core/tests/parameter/flag/formatting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,61 @@ describe("formatDocumentationForFlagParameters", function () {
compareToBaseline(this, StringArrayBaselineFormat, lines);
});

it("required array parsed flag", function () {
// GIVEN
type Positional = [];
type Flags = {
readonly requiredArrayParsed: string[];
};

const parameters: TypedCommandParameters<Flags, Positional, CommandContext> = {
flags: {
requiredArrayParsed: {
kind: "parsed",
parse: (values) => values.split(","),
brief: "required array parsed flag",
},
},
positional: { kind: "tuple", parameters: [] },
};

// WHEN
const lines = formatDocumentationForFlagParameters(parameters.flags, parameters.aliases ?? {}, defaultArgs);

// THEN
compareToBaseline(this, StringArrayBaselineFormat, lines);
});

it("optional array parsed flag", function () {
// GIVEN
type Positional = [];
type Flags = {
readonly optionalArrayParsed?: string[];
};

const parameters: TypedCommandParameters<Flags, Positional, CommandContext> = {
flags: {
optionalArrayParsed: {
kind: "parsed",
parse: (values) => values.split(","),
optional: true,
brief: "optional array parsed flag",
},
},
positional: { kind: "tuple", parameters: [] },
};

// WHEN
const lines = formatDocumentationForFlagParameters(
parameters.flags ?? {},
parameters.aliases ?? {},
defaultArgs,
);

// THEN
compareToBaseline(this, StringArrayBaselineFormat, lines);
});

it("multiple parsed flags", function () {
// GIVEN
type Positional = [];
Expand Down
51 changes: 51 additions & 0 deletions packages/core/tests/parameter/formatting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,57 @@ describe("formatUsageLineForParameters", function () {
// THEN
compareToBaseline(this, StringArrayBaselineFormat, [line]);
});

it("required array parsed flag", function () {
// GIVEN
type Positional = [];
type Flags = {
readonly requiredArrayParsed: string[];
};

const parameters: TypedCommandParameters<Flags, Positional, CommandContext> = {
flags: {
requiredArrayParsed: {
kind: "parsed",
parse: (values) => values.split(","),
brief: "required array parsed flag",
},
},
positional: { kind: "tuple", parameters: [] },
};

// WHEN
const line = formatUsageLineForParameters(parameters, defaultArgs);

// THEN
compareToBaseline(this, StringArrayBaselineFormat, [line]);
});

it("optional array parsed flag", function () {
// GIVEN
type Positional = [];
type Flags = {
readonly optionalArrayParsed?: string[];
};

const parameters: TypedCommandParameters<Flags, Positional, CommandContext> = {
flags: {
optionalArrayParsed: {
kind: "parsed",
parse: (values) => values.split(","),
optional: true,
brief: "optional array parsed flag",
},
},
positional: { kind: "tuple", parameters: [] },
};

// WHEN
const line = formatUsageLineForParameters(parameters, defaultArgs);

// THEN
compareToBaseline(this, StringArrayBaselineFormat, [line]);
});
});
});
});
Loading

0 comments on commit ecc9099

Please sign in to comment.