Haskell: Parsing command line arguments

后端 未结 4 1044
耶瑟儿~
耶瑟儿~ 2021-02-02 09:14

This more of a style question, rather than a how to.

So I\'ve got a program that needs two command line arguments: a string and an integer.

I implemented it this

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-02 10:08

    There are plenty of argument/option parsing libraries in Haskell that make life easier than with read/getOpt, an example with a modern one (optparse-applicative) might be of interest:

    import Options.Applicative
    
    doStuffWith :: String -> Int -> IO ()
    doStuffWith s n = mapM_ putStrLn $ replicate n s
    
    parser = fmap (,)
             (argument str (metavar "")) <*>
             (argument auto (metavar ""))
    
    main = execParser (info parser fullDesc) >>= (uncurry doStuffWith)
    

提交回复
热议问题