-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(flags): Handle space separated and double quoted multi-valued fla…
…gs PR#2 (#237) * rewrite tokenizer and test * update dist folder * update dist folder * eslint fix * actions/upload-artifact to v4 --------- Co-authored-by: Gulom Alimov <[email protected]>
- Loading branch information
Showing
5 changed files
with
38 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
// tokenize functions splits a string of CLI flags by whitespace, additionally handling double-quoted | ||
// and space separated string values | ||
export function tokenize (input: string): string[] { | ||
// Regular expression to find quoted strings or sequences of non-whitespace characters | ||
const regex = /"([^"]*)"|\S+/g | ||
// This regular expression looks for: | ||
// 1. Sequences inside double quotes which may contain spaces (captured including the quotes) | ||
// 2. Or sequences of non-space characters | ||
const regex = /"[^"]*"|\S+/g | ||
|
||
const tokens: string[] = [] | ||
let match: RegExpExecArray | null | ||
const matches = input.match(regex) | ||
if (matches == null) { | ||
return [] | ||
} | ||
|
||
// Use the exec method to find all matches in the input string | ||
while ((match = regex.exec(input)) !== null) { | ||
// match[1] will hold the matched content inside quotes if it exists, | ||
// otherwise use match[0] which covers the non-quoted matches | ||
if (match[1] !== undefined) { | ||
tokens.push(match[1]) // Pushes the inside of the quotes to the array | ||
} else { | ||
tokens.push(match[0]) // Pushes the non-quoted match to the array | ||
return matches.map(arg => { | ||
// Preserve the double quotes around arguments | ||
if (arg.startsWith('"') && arg.endsWith('"')) { | ||
return arg | ||
} | ||
} | ||
|
||
return tokens | ||
return arg | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters