File: Commands.hs

package info (click to toggle)
haskell-optparse-applicative 0.18.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 400 kB
  • sloc: haskell: 3,369; makefile: 5
file content (49 lines) | stat: -rw-r--r-- 1,117 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE CPP #-}
module Examples.Commands where

import Data.List
import Data.Monoid
import Options.Applicative

#if __GLASGOW_HASKELL__ <= 702
(<>) :: Monoid a => a -> a -> a
(<>) = mappend
#endif

data Sample
  = Hello [String]
  | Goodbye
  deriving (Eq, Show)

hello :: Parser Sample
hello = Hello <$> many (argument str (metavar "TARGET..."))

sample :: Parser Sample
sample = subparser
       ( command "hello"
         (info hello
               (progDesc "Print greeting"))
      <> command "goodbye"
         (info (pure Goodbye)
               (progDesc "Say goodbye"))
       )
      <|> subparser
       ( command "bonjour"
         (info hello
               (progDesc "Print greeting"))
      <> command "au-revoir"
         (info (pure Goodbye)
               (progDesc "Say goodbye"))
      <> commandGroup "French commands:"
      <> hidden
       )

run :: Sample -> IO ()
run (Hello targets) = putStrLn $ "Hello, " ++ intercalate ", " targets ++ "!"
run Goodbye = putStrLn "Goodbye."

opts :: ParserInfo Sample
opts = info (sample <**> helper) idm

main :: IO ()
main = execParser opts >>= run