Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escapable commas in CLI options? #962

Open
npetruzzelli opened this issue Jan 2, 2025 · 1 comment
Open

Escapable commas in CLI options? #962

npetruzzelli opened this issue Jan 2, 2025 · 1 comment

Comments

@npetruzzelli
Copy link

The commander argument parser for the --ignore option:

function collect(value: string, previous: string[]): string[] {
if (
!previous ||
previous === defaultIgnoreGlobs ||
previous === defaultHandlers ||
previous === defaultResolvers
) {
previous = [];
}
const values = value.split(',');
return previous.concat(values);
}

.option(
'-i, --ignore <glob>',
'Comma separated list of glob patterns which will ignore the paths that match. Can also be used multiple times.',
collect,
defaultIgnoreGlobs,
)

... splits on a comma, which makes it impossible to use glob brace expansion for this option or commas in general for any option that also uses this argument parser.


Would supporting escapable commas be possible?


To avoid breaking changes you may need to make it opt-in (behind a new option --comma-escaping or similar.)

The problem seems common enough and has been solved different ways in different languages. Here is a rough first pass after a little (very little) research:

/**
 * Split on commas (","), unless they are escaped with a backslash("\,").
 *
 * Escaped commas are replaced with individual commas.
 */
export function splitOnComma(str: string): string[] {
  if (typeof str !== 'string') {
    const errorMessage = 'input must be a string'
    throw new TypeError(errorMessage)
  }
  const DELIMETER = ','
  const ESCAPE_CHAR = '\\'
  const ESCAPED_DELIMETER = ESCAPE_CHAR + DELIMETER
  const hasEscapedDelimeter = str.includes(ESCAPED_DELIMETER)

  // Negative Lookbehind: https://www.regular-expressions.info/lookaround.html
  const result = str.split(/,(?<!\\,)/)

  if (hasEscapedDelimeter) {
    return result.map((subStr) => subStr.replace(/\\,/gi, DELIMETER))
  }
  return result
}

I don't have experience with negative lookbehinds. I wrote the example to favor being excessively self-documenting. Refinements could include:

  • improved error message
  • reducing the number of lines needed
  • adding unit tests
  • if the build tools are configured for ES2021+, String.prototype.replaceAll could be used instead.
    • subStr.replaceAll(ESCAPED_DELIMETER, DELIMETER))
  • perform the replacement in fewer operations / general performance improvement
@npetruzzelli
Copy link
Author

npetruzzelli commented Jan 2, 2025

In fairness, a counter argument:

This only seems to affect "lists" within brace expansion, and something similar can be accomplished with the Regex Groups syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant