Skip to content

Instantly share code, notes, and snippets.

@HirotoShioi
Created May 8, 2018 07:11
Show Gist options
  • Save HirotoShioi/bab9b11abb4f6455e09c6046482f5062 to your computer and use it in GitHub Desktop.
Save HirotoShioi/bab9b11abb4f6455e09c6046482f5062 to your computer and use it in GitHub Desktop.
optparse implementation using subparser
module Main where
import Data.Semigroup ((<>))
import Options.Applicative
import Paths_opt_parse (version)
data CLI
= CollectEmails
| ProcessTicket Int
| ProcessTickets
| ShowStatistics
deriving (Eq, Show)
processTicket :: Parser CLI
processTicket = ProcessTicket <$>
argument auto (metavar "TICKET_ID"
<> help "Specify ticket id to analyze")
cli :: Parser CLI
cli = subparser
(
command "collectEmails" (info (pure CollectEmails)
(progDesc "Collect email addresses from Zendesk"))
<> command "processTickets" (info (pure ProcessTickets)
(progDesc "Process all the tickets in the Zendesk"))
<> command "processTicket" (info processTicket
(progDesc "Process Zendesk ticket of an given <TICKET_ID>"))
<> command "showStats" (info (pure ShowStatistics)
(progDesc "Show statistics of a Zendesk ticket"))
)
execCli :: CLI -> IO ()
execCli CollectEmails = putStrLn "Collecting emails"
execCli (ProcessTicket num) = putStrLn $ "Process ticket of an id: " <> show num
execCli ProcessTickets = putStrLn "Process tickets"
execCli ShowStatistics = putStrLn "Collecting statistics..."
main :: IO ()
main = execCli =<< execParser opts
where
opts = info (cli <**> helper <**> versionHelper)
( fullDesc
<> header "Log classifier"
<> progDesc "Client for peforming analysis on Zendesk"
)
versionHelper =
infoOption
("Log classifier version" <> show version)
(long "version" <> help "Show version")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment