Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#4305 closed proposal (fixed)

Add showCommandForUser to process:System.Process

Reported by: igloo Owned by:
Priority: normal Milestone: Not GHC
Component: Compiler Version: 6.12.3
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by igloo)

There are many programs (e.g. ghc and Cabal) which run other programs, and when run with -v want to show the user what they are running. The user then often wants to run the command by hand, in order to debug a problem, but this can be tricky when the command or its arguments include spaces or other characters treated specially by shells.

This proposal is for a System.Process.showCommandForUser function in the process package, such that showCommandForUser cmd args can be copied and pasted directly into a shell.

I would have put this in System.Cmd, except that module is earmarked to be deprecated.

The code already existed within System.Process; I've just rearranged it a bit:

showCommandForUser :: FilePath -> [String] -> String
showCommandForUser cmd args = unwords (map translate (cmd : args))

translate :: String -> String
#if mingw32_HOST_OS
translate str = '"' : snd (foldr escape (True,"\"") str)
  where escape '"'  (b,     str) = (True,  '\\' : '"'  : str)
        escape '\\' (True,  str) = (True,  '\\' : '\\' : str)
        escape '\\' (False, str) = (False, '\\' : str)
        escape c    (b,     str) = (False, c : str)
translate str = '\'' : foldr escape "'" str
  where escape '\'' = showString "'\\''"
        escape c    = showChar c

The fallback for rawSystem is now

rawSystem cmd args = system (showCommandForUser cmd args)

except with hugs on Windows, where for some reason it's

rawSystem cmd args = system (cmd ++ showCommandForUser "" args)

which is surely wrong, but how it behaved before.

Discussion period: 2 weeks, until 26 Sep 2010.

Attachments (1)

showCommandForUser.dpatch (4.4 KB) - added by igloo 6 years ago.

Download all attachments as: .zip

Change History (4)

comment:1 Changed 6 years ago by igloo

Description: modified (diff)

Changed 6 years ago by igloo

Attachment: showCommandForUser.dpatch added

comment:3 Changed 6 years ago by igloo

Resolution: fixed
Status: newclosed

Consensus was to apply the patch, with a haddock doc to clarify which shell it was designed to be used with (cmd.exe on Windows, sh otherwise).

Sun Sep 26 22:06:04 BST 2010  Ian Lynagh <>
  * Define showCommandForUser; fixes proposal #4305
Note: See TracTickets for help on using tickets.