Skip to content

Commit e5033df

Browse files
authored
Merge pull request #6072 from unisonweb/cp/single-quote-args
2 parents b715d59 + 0fafe93 commit e5033df

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

unison-cli/src/Unison/CommandLine.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ expandArguments numberedArgs params args = do
104104
( \acc (_, param) arg ->
105105
case arg of
106106
-- Don't expand numbers in quoted args
107-
Left (InputPattern.QuotedArg quoted _) -> Right (RawArg quoted : acc, [])
107+
Left (InputPattern.QuotedArg quoted _ _) -> Right (RawArg quoted : acc, [])
108108
Left (InputPattern.UnquotedArg raw) -> Right $ (RawArg raw : acc, [])
109109
Left (InputPattern.NumberedArg n) ->
110110
case expandNumber numberedArgs n of
@@ -212,7 +212,7 @@ parseInput codebase projPath currentProjectRoot numberedArgs patterns cliArgs =
212212
[] -> throwE NoCommand
213213
(NumberedArg {} : _) -> throwE NoCommand
214214
(UnquotedArg cmd : args) -> pure (cmd, args)
215-
(QuotedArg cmd _ : args) -> pure (cmd, args)
215+
(QuotedArg cmd _ _ : args) -> pure (cmd, args)
216216
pat@(InputPattern {params, parse}) <- case Map.lookup cmd patterns of
217217
Just pat -> pure pat
218218
Nothing -> throwE $ UnknownCommand cmd

unison-cli/src/Unison/CommandLine/Completion.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ haskelineTabComplete patterns codebase authedHTTPClient ppCtx = \(beforeCursorRe
9696

9797
let finalize completion =
9898
let newReplacement = case lastArg of
99-
QuotedArg _ _
100-
| completion.isFinished -> "\"" <> completion.replacement <> "\""
101-
QuotedArg _ False -> "\"" <> completion.replacement <> "\""
102-
QuotedArg _ True -> "\"" <> completion.replacement
99+
QuotedArg _ _ quoteChar
100+
| completion.isFinished -> [quoteChar] <> completion.replacement <> [quoteChar]
101+
QuotedArg _ False quoteChar -> [quoteChar] <> completion.replacement <> [quoteChar]
102+
QuotedArg _ True quoteChar -> [quoteChar] <> completion.replacement
103103
UnquotedArg _ -> completion.replacement
104104
NumberedArg _ -> completion.replacement
105105
in completion {Line.replacement = newReplacement}

unison-cli/src/Unison/CommandLine/InputPattern.hs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ data CliArg
259259
| QuotedArg
260260
String
261261
Bool -- whether the quote was terminated
262+
Char -- the quote character used
262263
| UnquotedArg String
263264
deriving (Eq, Show)
264265

@@ -271,8 +272,8 @@ renderCliArg =
271272
NumberedRange s e -> show s <> "-" <> show e
272273
NumberedAfterStart s -> show s <> "-"
273274
NumberedBeforeEnd e -> "-" <> show e
274-
QuotedArg s False -> "\"" <> s <> "\""
275-
QuotedArg s True -> "\"" <> s
275+
QuotedArg s False quoteChar -> [quoteChar] <> s <> [quoteChar]
276+
QuotedArg s True quoteChar -> [quoteChar] <> s
276277
UnquotedArg s -> s
277278

278279
-- | Like `renderCliArg`, but does not include quotes regardless of whether the argument was quoted.
@@ -284,8 +285,8 @@ renderCliArgUnquoted =
284285
NumberedRange s e -> show s <> "-" <> show e
285286
NumberedAfterStart s -> show s <> "-"
286287
NumberedBeforeEnd e -> "-" <> show e
287-
QuotedArg s False -> s
288-
QuotedArg s True -> s
288+
QuotedArg s False _quoteChar -> s
289+
QuotedArg s True _quoteChar -> s
289290
UnquotedArg s -> s
290291

291292
-- | Like `parseArgs`, but indicates whether each argument was quoted, and also whether the quote was terminated..
@@ -308,7 +309,7 @@ argP = do
308309
escapedQuote :: Parser Char
309310
escapedQuote = do
310311
_ <- MP.char '\\'
311-
MP.char '"'
312+
MP.char '"' <|> MP.char '\''
312313

313314
numberedArgP :: Parser CliArg
314315
numberedArgP = do
@@ -334,13 +335,13 @@ argP = do
334335

335336
quotedArgP :: Parser CliArg
336337
quotedArgP = do
337-
_ <- MP.char '"'
338+
quoteChar <- MP.char '"' <|> MP.char '\''
338339
(content, hasUnterminatedQuote) <-
339340
MP.manyTill_
340341
(escapedQuote <|> MP.anySingle)
341342
-- Treat EOF as closing quote so completion still functions on unterminated quotes
342-
(((MP.char '"') $> False) <|> (MP.eof $> True))
343-
pure $ QuotedArg content hasUnterminatedQuote
343+
(((MP.char quoteChar) $> False) <|> (MP.eof $> True))
344+
pure $ QuotedArg content hasUnterminatedQuote quoteChar
344345
unquotedArgP :: Parser CliArg
345346
unquotedArgP = do
346347
UnquotedArg <$> MP.some (MP.satisfy (not . Char.isSpace))
@@ -349,14 +350,14 @@ argP = do
349350
-- Just [UnquotedArg "one",UnquotedArg "two",UnquotedArg "three"]
350351
--
351352
-- >>> MP.parseMaybe argsP "\"one two\" three"
352-
-- Just [QuotedArg "one two" False,UnquotedArg "three"]
353+
-- Just [QuotedArg "one two" False '"',UnquotedArg "three"]
353354
--
354355
-- >>> MP.parseMaybe argsP "one two three"
355356
-- Just [UnquotedArg "one",UnquotedArg "two",UnquotedArg "three"]
356357
--
357358
-- Unfinished quote should auto-close quote at end of input, but indicate that it was unterminated
358359
-- >>> MP.parseMaybe argsP "one two \"three four"
359-
-- Just [UnquotedArg "one",UnquotedArg "two",QuotedArg "three four" True]
360+
-- Just [UnquotedArg "one",UnquotedArg "two",QuotedArg "three four" True '"']
360361
--
361362
-- Should require args to take up a whole segment, and should fall back to raw args.
362363
-- >>> MP.parseMaybe argsP "1.2.3 abc-def"

unison-src/transcripts/idempotent/input-parsing.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ scratch/main> update
1818
Quoting allows spaces in arguments.
1919

2020
``` ucm
21-
scratch/main> run main "all one arg" "contains escaped \" quote" second third
21+
scratch/main> run main "all one arg" "contains escaped \" quote" 'single quoted' second third
2222
2323
Right
2424
[ "all one arg"
2525
, "contains escaped \" quote"
26+
, "single quoted"
2627
, "second"
2728
, "third"
2829
]

0 commit comments

Comments
 (0)