Skip to content

Commit

Permalink
fix: loosen character restrictons on flags
Browse files Browse the repository at this point in the history
Loosens the regex used for flag pattern matching to allow:

- Periods
- Underscores
- Digits

...which enables flags like `--foo.bar`, `--foo123`, and `--foo_bar`.

Signed-off-by: Marty Jones <mjones721@bloomberg.net>
  • Loading branch information
Marty Jones committed Dec 12, 2024
1 parent 7898563 commit 1baa426
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/parameter/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ function findFlagsByArgument<CONTEXT extends CommandContext>(
return [];
}

const FLAG_NAME_VALUE_PATTERN = /^--([a-z][a-z-]+)=(.+)$/i;
const FLAG_NAME_VALUE_PATTERN = /^--([a-z][a-z-.\d_]+)=(.+)$/i;
const ALIAS_VALUE_PATTERN = /^-([a-z])=(.+)$/i;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
:::: formatDocumentationForFlagParameters / no flags
-h --help Print help information and exit
 -- All subsequent inputs should be interpreted as arguments
:::: formatDocumentationForFlagParameters / parsed / flag with nonstandard character
 --a.b.c_10 required parsed flag
-h --help Print help information and exit
 -- All subsequent inputs should be interpreted as arguments
:::: formatDocumentationForFlagParameters / parsed / multipart flag
 --multi.part required parsed flag
-h --help Print help information and exit
 -- All subsequent inputs should be interpreted as arguments
:::: formatDocumentationForFlagParameters / parsed / multiple parsed flags
 --requiredParsed required parsed flag
 --requiredParsedWithLongerName required parsed flag with longer name
Expand Down
50 changes: 50 additions & 0 deletions packages/core/tests/parameter/flag/formatting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,5 +728,55 @@ describe("formatDocumentationForFlagParameters", function () {
// THEN
compareToBaseline(this, StringArrayBaselineFormat, lines);
});

it("multipart flag", function () {
// GIVEN
type Positional = [];
type Flags = {
readonly "multi.part": string;
};

const parameters: TypedCommandParameters<Flags, Positional, CommandContext> = {
flags: {
"multi.part": {
kind: "parsed",
parse: String,
brief: "required parsed flag",
},
},
positional: { kind: "tuple", parameters: [] },
};

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

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

it("flag with nonstandard character", function () {
// GIVEN
type Positional = [];
type Flags = {
readonly "a.b.c_10": string;
};

const parameters: TypedCommandParameters<Flags, Positional, CommandContext> = {
flags: {
"a.b.c_10": {
kind: "parsed",
parse: String,
brief: "required parsed flag",
},
},
positional: { kind: "tuple", parameters: [] },
};

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

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

0 comments on commit 1baa426

Please sign in to comment.