Haskell: Parsing command line arguments

2020-05-20 08:19发布

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 way:

main = do
  args@(~( aString : aInteger : [] ) ) <- getArgs
  let parsed@( ~[(n,_)] ) = reads aInteger
  if length args /= 2 || L.null parsed
    then do
      name <- getProgName
      hPutStrLn stderr $ "usage: " ++ name ++ " <string> <integer>"
      exitFailure
    else do
      doStuffWith aString n

While this works, this is the first time I've really used command line args in Haskell, so I'm not sure whether this is a horribly awkward and unreadable way to do what I want.

Using lazy pattern matching works, but I could see how it could be frowned upon by other coders. And the use of reads to see if I got a successful parse definitely felt awkward when writing it.

Is there a more idiomatic way to do this?

4条回答
▲ chillily
2楼-- · 2020-05-20 08:24

I agree the optparse-applicative package is very nice. Awesome! Let me give an up-to-date example.

The program takes as arguments a string and an integer n, returns the string replicated n times, and it has a flag which reverses the string.

-- file: repstring.hs
import Options.Applicative
import Data.Monoid ((<>))

data Sample = Sample
  { string :: String
  , n :: Int
  , flip :: Bool }

replicateString :: Sample -> IO ()
replicateString (Sample string n flip) = 
    do 
      if not flip then putStrLn repstring else putStrLn $ reverse repstring
          where repstring = foldr (++) "" $ replicate n string

sample :: Parser Sample
sample = Sample
     <$> argument str 
          ( metavar "STRING"
         <> help "String to replicate" )
     <*> argument auto
          ( metavar "INTEGER"
         <> help "Number of replicates" )
     <*> switch
          ( long "flip"
         <> short 'f'
         <> help "Whether to reverse the string" )

main :: IO ()
main = execParser opts >>= replicateString
  where
    opts = info (helper <*> sample)
      ( fullDesc
     <> progDesc "Replicate a string"
     <> header "repstring - an example of the optparse-applicative package" )

Once the file is compiled (with ghc as usual):

$ ./repstring --help
repstring - an example of the optparse-applicative package

Usage: repstring STRING INTEGER [-f|--flip]
  Replicate a string

Available options:
  -h,--help                Show this help text
  STRING                   String to replicate
  INTEGER                  Number of replicates
  -f,--flip                Whether to reverse the string

$ ./repstring "hi" 3 
hihihi
$ ./repstring "hi" 3 -f
ihihih

Now, assume you want an optional argument, a name to append at the end of the string:

-- file: repstring2.hs
import Options.Applicative
import Data.Monoid ((<>))
import Data.Maybe (fromJust, isJust)

data Sample = Sample
  { string :: String
  , n :: Int
  , flip :: Bool
  , name :: Maybe String }

replicateString :: Sample -> IO ()
replicateString (Sample string n flip maybeName) = 
    do 
      if not flip then putStrLn $ repstring ++ name  else putStrLn $ reverse repstring ++ name
          where repstring = foldr (++) "" $ replicate n string
                name = if isJust maybeName then fromJust maybeName else ""

sample :: Parser Sample
sample = Sample
     <$> argument str 
          ( metavar "STRING"
         <> help "String to replicate" )
     <*> argument auto
          ( metavar "INTEGER"
         <> help "Number of replicates" )
     <*> switch
          ( long "flip"
         <> short 'f'
         <> help "Whether to reverse the string" )
     <*> ( optional $ strOption 
          ( metavar "NAME"
         <> long "append"
         <> short 'a'
         <> help "Append name" ))

Compile and have fun:

$ ./repstring2 "hi" 3 -f -a rampion
ihihihrampion
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-05-20 08:30

I suggest using a case expression:

main :: IO ()
main = do
  args <- getArgs
  case args of
    [aString, aInteger] | [(n,_)] <- reads aInteger ->
      doStuffWith aString n
    _ -> do
      name <- getProgName
      hPutStrLn stderr $ "usage: " ++ name ++ " <string> <integer>"
      exitFailure

The binding in a guard used here is a pattern guard, a new feature added in Haskell 2010 (and a commonly-used GHC extension before that).

Using reads like this is perfectly acceptable; it's basically the only way to recover properly from invalid reads, at least until we get readMaybe or something of its ilk in the standard library (there have been proposals to do it over the years, but they've fallen prey to bikeshedding). Using lazy pattern matching and conditionals to emulate a case expression is less acceptable :)

Another possible alternative, using the view patterns extension, is

case args of
  [aString, reads -> [(n,_)]] ->
    doStuffWith aString n
  _ -> ...

This avoids the one-use aInteger binding, and keeps the "parsing logic" close to the structure of the argument list. However, it's not standard Haskell (although the extension is by no means controversial).

For more complex argument handling, you might want to look into a specialised module — System.Console.GetOpt is in the standard base library, but only handles options (not argument parsing), while cmdlib and cmdargs are more "full-stack" solutions (although I caution you to avoid the "Implicit" mode of cmdargs, as it's a gross impure hack to make the syntax a bit nicer; the "Explicit" mode should be just fine, however).

查看更多
来,给爷笑一个
4楼-- · 2020-05-20 08:40

These days, I'm a big fan of optparse-generic for parsing command line arguments:

  • it lets you parse arguments (not just options)
  • it lets you parse options (not just arguments)
  • you can annotate the arguments to provide a useful help
  • but you don't have to

As your program matures, you may want to come up with a complete help, and a well-annotated options data type, which options-generic is great at. But it's also great at parsing lists and tuples without any annotation at all, so you can hit the ground running:

For example

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Options.Generic

main :: IO ()
main = do
  (n, c) <- getRecord "Example program"
  putStrLn $ replicate n c

Runs as:

$ ./OptparseGenericExample
Missing: INT CHAR

Usage: OptparseGenericExample INT CHAR
$ ./OptparseGenericExample 5 c
ccccc
查看更多
forever°为你锁心
5楼-- · 2020-05-20 08:43

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 "<string>")) <*>
         (argument auto (metavar "<integer>"))

main = execParser (info parser fullDesc) >>= (uncurry doStuffWith)
查看更多
登录 后发表回答