Haskell: Parsing command line arguments

后端 未结 4 1043
耶瑟儿~
耶瑟儿~ 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:00

    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
    
    0 讨论(0)
  • 2021-02-02 10:04

    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
    
    0 讨论(0)
  • 2021-02-02 10:07

    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).

    0 讨论(0)
  • 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 "<string>")) <*>
             (argument auto (metavar "<integer>"))
    
    main = execParser (info parser fullDesc) >>= (uncurry doStuffWith)
    
    0 讨论(0)
提交回复
热议问题