module CmdArgs (
AttCmdArgs(..)
, withCmdArgs
)
where
import Data.Monoid ( (<>), mconcat )
import Options.Applicative hiding (helper, strOption)
import qualified Options.Applicative as Op
import Options.Applicative.Types (readerAsk)
import System.Environment
import DeliveryHeaders ( Addr(..) )
strOption :: Mod OptionFields String -> Parser String
strOption = Op.strOption
helper :: Parser (a -> a)
helper = abortOption ShowHelpText $ mconcat
[ long "help"
, help "Show this help text"
, hidden ]
addr :: ReadM (Maybe Addr)
addr = (Just . Addr) <$> str
name :: ReadM (Maybe String)
name = Just <$> str
mode :: Char -> ReadM ()
mode validMode = do
x <- readerAsk
if x == [validMode]
then return ()
else readerError $ "can only take mode '" <> [validMode] <> "' as arg"
data AttCmdArgs = AttCmdArgs
{
senderEnvelopeAddress :: Maybe Addr
, senderFullName :: Maybe String
, recipients :: [Addr]
}
deriving (Eq, Show)
attCmdArgs :: Parser AttCmdArgs
attCmdArgs = AttCmdArgs
<$> option addr
( short 'f'
<> value Nothing
<> metavar "ADDRESS"
<> help "Sender envelope address" )
<*> ( option name
( short 'F'
<> value Nothing
<> metavar "NAME"
<> help "Sender full name" )
<* option (mode 'm')
( short 'b'
<> value ()
<> metavar "MODE"
<> help "-bm: Read input from stdin" )
<* switch
( short 'i'
<> help "(ignored, used only for compatibility with sendmail")
<* many ( strOption
( short 'o'
<> help "(ignored, used only for compatibility with sendmail"))
<* many ( strOption
( short 'O'
<> help "(ignored, used only for compatibility with sendmail"))
<* many ( strOption
( short 'B'
<> help "(ignored, used only for compatibility with sendmail"))
<* many ( strOption
( short 'q'
<> help "(ignored, used only for compatibility with sendmail"))
<* switch
( short 'v'
<> help "(ignored, used only for compatibility with sendmail")
)
<*> some ( argument (Addr <$> str) (metavar "RECIPIENTS...") )
version :: String
version = "0.1.0.2"
testMain :: IO ()
testMain =
withArgs [
"-f", "someenvddr"
, "-F", "Joe Bloggs"
, "-bm"
, "a@b.com"
]
main
main :: IO ()
main = execParser opts >>= doStuff
where
opts = info (helper <*> attCmdArgs)
( fullDesc
<> progDesc desc
<> header h )
desc = "read a message on stdin and deliver it to user in config file"
h = "attomail v " <> version <> "simple mail delivery to one user"
doStuff :: AttCmdArgs -> IO ()
doStuff x@AttCmdArgs{} =
print x
withCmdArgs :: (AttCmdArgs -> IO b) -> IO b
withCmdArgs f = execParser opts >>= f
where
opts = info (helper <*> attCmdArgs)
( fullDesc
<> progDesc desc
<> header h )
desc = "read a message on stdin and deliver it to user in config file"
h = "attomail v " <> version <> "simple mail delivery to one user"