Skip to content

Commit

Permalink
fix(flags): Handle space separated and double quoted multi-valued fla…
Browse files Browse the repository at this point in the history
…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
Googlom and Gulom Alimov authored Jan 16, 2025
1 parent 374680f commit 742a63d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/verify-ts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
node-version: ${{ env.NODE_VERSION }}
- run: npm clean-install
- run: npm run lint:ci
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: always()
with:
name: lint report
Expand All @@ -38,12 +38,12 @@ jobs:
node-version: ${{ env.NODE_VERSION }}
- run: npm clean-install
- run: npm run test:ci
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: always()
with:
name: test report
path: reports/sonar-report.xml
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: always()
with:
name: coverage report
Expand Down
29 changes: 14 additions & 15 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions src/utils.ts
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
})
}
12 changes: 6 additions & 6 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ describe('utils.tokenize', () => {

it('should handle single quoted group', () => {
const result = tokenize('--flag1 "multi value"')
expect(result).toEqual(['--flag1', 'multi value'])
expect(result).toEqual(['--flag1', '"multi value"'])
})

it('should handle multiple quoted groups', () => {
const result = tokenize('--flag1 "multi value" --anotherFlag "another multi value" --flag333')
expect(result).toEqual(['--flag1', 'multi value', '--anotherFlag', 'another multi value', '--flag333'])
expect(result).toEqual(['--flag1', '"multi value"', '--anotherFlag', '"another multi value"', '--flag333'])
})

it('should handle empty quotes', () => {
const result = tokenize('--flag1 "" --flag2')
expect(result).toEqual(['--flag1', '', '--flag2'])
expect(result).toEqual(['--flag1', '""', '--flag2'])
})

it('should correctly tokenize empty input', () => {
const result = tokenize('')
expect(result).toEqual([])
})

it('should handle quotes without spaces in between', () => {
const result = tokenize('"first""second""third"')
expect(result).toEqual(['first', 'second', 'third'])
it('should handle comma separated and quoted values', () => {
const result = tokenize('--sources .github/scripts/kubernetes-namespace.sh --scriptArguments "setup,kube_name--space-cucumber,some--context--123"')
expect(result).toEqual(['--sources', '.github/scripts/kubernetes-namespace.sh', '--scriptArguments', '"setup,kube_name--space-cucumber,some--context--123"'])
})
})

0 comments on commit 742a63d

Please sign in to comment.