[Haskell-cafe] How to reverse ghc encoding of command line arguments

Ben Franksen ben.franksen at online.de
Sun Nov 16 19:32:08 UTC 2014


Donn Cave wrote:
> quoth Ben Franksen <ben.franksen at online.de>
> ...
>> Perhaps it is simpler to write our own getArgs/getEnv functions and
>> directly convert the data we get from the system to a proper (Unicode)
>> String?
> 
> I may be confused here - trying this out, I seem to be getting
> garbage I don't understand from System.Environment getArgs.
> 
> But there's a System.Posix.Env.ByteString getArgs, that looks like
> just what you propose above.
> 
> import qualified Data.ByteString.Char8 as P
> import qualified System.Posix.Env.ByteString as B
> import qualified Data.Text as T
> import Data.Text.Encoding (decodeUtf8, encodeUtf8)
> 
>     argsb <- B.getArgs
>     putStrLn ("byte args: " ++ show(argsb))
>     let argsu = map (encodeUtf8 . T.pack . P.unpack) argsb
>     putStrLn ("UTF8 byte args: " ++ show(argsu))
> 
> $ ./cvtargs [string that perhaps should not be in this email!]
> byte args: ["S\228kkij\228rven","Polkka"]
> UTF8 byte args: ["S\195\164kkij\195\164rven","Polkka"]

Cool, I wasn't aware that System.Posix had that function. Now I need to see 
what to do for Windows...

Anyway, many thanks to you and everyone else who offered suggestions.

Cheers
Ben
-- 
"Make it so they have to reboot after every typo." -- Scott Adams




More information about the Haskell-Cafe mailing list