fix(cli): allow flag values that start with -- via --flag=value (#2892)
This commit is contained in:
parent
5783c4192b
commit
563d4ff64b
|
|
@ -10,4 +10,31 @@ describe('parseArgs', () => {
|
|||
expect(parsed.flags.get('value')).toBe('')
|
||||
expect(parsed.flags.get('json')).toBe(true)
|
||||
})
|
||||
|
||||
it('accepts a flag value that starts with -- via the = form', () => {
|
||||
const parsed = parseArgs(['terminal', 'send', '--text=--help'])
|
||||
|
||||
expect(parsed.commandPath).toEqual(['terminal', 'send'])
|
||||
expect(parsed.flags.get('text')).toBe('--help')
|
||||
})
|
||||
|
||||
it('splits --flag=value on the first = so values may contain =', () => {
|
||||
const parsed = parseArgs(['set', 'cookie', '--value=a=b=c'])
|
||||
|
||||
expect(parsed.flags.get('value')).toBe('a=b=c')
|
||||
})
|
||||
|
||||
it('treats --flag= as an empty string value', () => {
|
||||
const parsed = parseArgs(['--value='])
|
||||
|
||||
expect(parsed.flags.get('value')).toBe('')
|
||||
})
|
||||
|
||||
it('still parses boolean flags and space-separated values', () => {
|
||||
const parsed = parseArgs(['tab', 'create', '--json', '--url', 'https://example.com'])
|
||||
|
||||
expect(parsed.commandPath).toEqual(['tab', 'create'])
|
||||
expect(parsed.flags.get('json')).toBe(true)
|
||||
expect(parsed.flags.get('url')).toBe('https://example.com')
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -29,7 +29,17 @@ export function parseArgs(argv: string[]): ParsedArgs {
|
|||
continue
|
||||
}
|
||||
|
||||
const flag = token.slice(2)
|
||||
const assignment = token.slice(2)
|
||||
// Why: `--flag=value` is the only unambiguous way to pass a value that
|
||||
// itself starts with `--` (e.g. `--text=--help`); the space-separated form
|
||||
// treats a `--`-leading next token as a new flag, so it can't express one.
|
||||
const equalsIndex = assignment.indexOf('=')
|
||||
if (equalsIndex !== -1) {
|
||||
flags.set(assignment.slice(0, equalsIndex), assignment.slice(equalsIndex + 1))
|
||||
continue
|
||||
}
|
||||
|
||||
const flag = assignment
|
||||
const hasNext = i + 1 < argv.length
|
||||
const next = argv[i + 1]
|
||||
if (!hasNext || next.startsWith('--')) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue