Skip to content

Commit

Permalink
add quoteShellCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Aug 8, 2018
1 parent 29aec78 commit bb32c09
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/pure/ospaths.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
include "system/inclrtl"

import strutils
import sequtils

type
ReadEnvEffect* = object of ReadIOEffect ## effect that denotes a read
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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\\\""
Expand Down

0 comments on commit bb32c09

Please sign in to comment.