1 {-# LANGUAGE DerivingStrategies #-}
3 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
5 Copyright : Copyright (C) 2014-2024 Synchrotron SOLEIL
6 L'Orme des Merisiers Saint-Aubin
7 BP 48 91192 GIF-sur-YVETTE CEDEX
10 Maintainer : Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
11 Stability : Experimental
12 Portability: GHC only (not tested)
16 import Control
.Monad
.Catch
(MonadThrow
)
17 import Control
.Monad
.IO.Class
(MonadIO
)
18 import Control
.Monad
.Logger
(LogLevel
(LevelDebug
), LoggingT
,
19 MonadLogger
, filterLogger
,
21 import Control
.Monad
.Reader
(MonadReader
, ReaderT
, ask
,
23 import Data
.Attoparsec
.Text
(parseOnly
)
24 import Data
.Text
(pack
)
25 import Options
.Applicative
(CommandFields
, Mod
, argument
,
26 command
, eitherReader
, execParser
,
27 fullDesc
, header
, help
, helper
,
28 hsubparser
, info
, long
, metavar
,
29 optional
, progDesc
, short
, str
,
31 import Options
.Applicative
.Types
(Parser
)
37 data FullOptions
= FullOptions
Bool Options
40 data Options
= Process
(Maybe FilePath) (Maybe ConfigRange
)
41 | CfgNew ProjectionType
(Maybe FilePath)
42 | CfgUpdate
FilePath (Maybe ConfigRange
)
46 debug
= switch
( long
"debug" <> short
'd
' <> help
"Print debug informations" )
48 config
:: Parser
FilePath
49 config
= argument str
(metavar
"CONFIG")
51 processOptions
:: Parser Options
52 processOptions
= Process
54 <*> optional
(argument
(eitherReader
(parseOnly fieldParser
. pack
)) (metavar
"RANGE"))
56 processCommand
:: Mod CommandFields Options
57 processCommand
= command
"process" (info processOptions
(progDesc
"process data's"))
59 cfgNewOption
:: Parser Options
61 <$> argument
(eitherReader
(parseOnly fieldParser
. pack
)) (metavar
"PROJECTION")
64 cfgNewCommand
:: Mod CommandFields Options
65 cfgNewCommand
= command
"cfg-new" (info cfgNewOption
(progDesc
"new config files"))
67 cfgUpdateOption
:: Parser Options
68 cfgUpdateOption
= CfgUpdate
70 <*> optional
(argument
(eitherReader
(parseOnly fieldParser
. pack
)) (metavar
"RANGE"))
72 cfgUpdateCommand
:: Mod CommandFields Options
73 cfgUpdateCommand
= command
"cfg-update" (info cfgUpdateOption
(progDesc
"update config files"))
75 options
:: Parser FullOptions
78 <*> hsubparser
(processCommand
<> cfgNewCommand
<> cfgUpdateCommand
)
81 App
{ unApp
:: ReaderT FullOptions
(LoggingT
IO) a
}
87 , MonadReader FullOptions
92 runApp
:: FullOptions
-> (LoggingT
IO a
-> IO a
) -> App a
-> IO a
93 runApp env runLogging action
=
94 runLogging
$ runReaderT
(unApp action
) env
98 (FullOptions _ o
) <- ask
100 (Process mf mr
) -> process mf mr
101 (CfgNew p mf
) -> new p mf
102 (CfgUpdate f mr
) -> update f mr
106 let opts
= info
(options
<**> helper
)
108 <> progDesc
"binoculars subcommand"
109 <> header
"binoculars - bin your data's" )
110 fopts
@(FullOptions d _
) <- execParser opts
111 let debugLogging
= if d
then id else filterLogger
(\_ l
-> l
/=LevelDebug
)
112 let runLogging
= runStdoutLoggingT
. debugLogging
113 runApp fopts runLogging app