From bb32c0965ad55861793d090865ecd18b121f3464 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 10 Jul 2018 18:24:29 -0700 Subject: [PATCH] add quoteShellCommand --- lib/pure/ospaths.nim | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/pure/ospaths.nim b/lib/pure/ospaths.nim index d0c5ef03f70a0..9a6afe708924a 100644 --- a/lib/pure/ospaths.nim +++ b/lib/pure/ospaths.nim @@ -13,6 +13,7 @@ include "system/inclrtl" import strutils +import sequtils type ReadEnvEffect* = object of ReadIOEffect ## effect that denotes a read @@ -570,6 +571,8 @@ proc expandTilde*(path: string): string {. # TODO: handle `~bob` and `~bob/` which means home of bob result = path +# TODO: consider whether quoteShellPosix, quoteShellWindows, quoteShell, quoteShellCommand +# belong in `strutils` instead; they are not specific to paths proc quoteShellWindows*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} = ## Quote s, so it can be safely passed to Windows API. ## Based on Python's subprocess.list2cmdline @@ -621,6 +624,15 @@ when defined(windows) or defined(posix) or defined(nintendoswitch): else: return quoteShellPosix(s) + proc quoteShellCommand*(args: openArray[string]): string = + ## Concatenates and quotes shell arguments `args` + runnableExamples: + when defined(posix): + assert quoteShellCommand(["aaa", "", "c d"]) == "aaa '' 'c d'" + when defined(windows): + assert quoteShellCommand(["aaa", "", "c d"]) == "aaa \"\" \"c d\"" + result = args.map(quoteShell).join(" ") + when isMainModule: assert quoteShellWindows("aaa") == "aaa" assert quoteShellWindows("aaa\"") == "aaa\\\""