diff --git a/__tests__/lib/cli/format.js b/__tests__/lib/cli/format.js index 26dd69ba1..09f19a7e5 100644 --- a/__tests__/lib/cli/format.js +++ b/__tests__/lib/cli/format.js @@ -33,7 +33,15 @@ describe( 'utils/cli/format', () => { }, { input: [ '{"json":broken json with spaces}' ], - expected: [ '"{"json":broken json with spaces}"' ], + expected: [ '"{\\"json\\":broken json with spaces}"' ], + }, + { + input: [ '--foo=bar1 "bar2" "bar3"' ], + expected: [ '--foo="bar1 \\"bar2\\" \\"bar3\\""' ], + }, + { + input: [ '--foo', 'bar1 "bar2" "bar3"' ], + expected: [ '--foo', '"bar1 \\"bar2\\" \\"bar3\\""' ], }, ] )( 'should requote args when needed - %o', ( { input, expected } ) => { const result = requoteArgs( input ); diff --git a/src/lib/cli/format.ts b/src/lib/cli/format.ts index c6448a450..6697477c4 100644 --- a/src/lib/cli/format.ts +++ b/src/lib/cli/format.ts @@ -134,11 +134,11 @@ export function keyValue( values: Tuple[] ): string { export function requoteArgs( args: string[] ): string[] { return args.map( arg => { if ( arg.includes( '--' ) && arg.includes( '=' ) && arg.includes( ' ' ) ) { - return arg.replace( /^--([^=]*)=(.*)$/, '--$1="$2"' ); + return arg.replace( /"/g, '\\"' ).replace( /^--([^=]*)=(.*)$/, '--$1="$2"' ); } if ( arg.includes( ' ' ) && ! isJsonObject( arg ) ) { - return `"${ arg }"`; + return `"${ arg.replace( /"/g, '\\"' ) }"`; } return arg;