Skip to content
Snippets Groups Projects
Commit 79d0dcc8 authored by Alice Brenon's avatar Alice Brenon
Browse files

Fix bug with NonEmpty, add support for Input and re-add option descriptions

parent 78dcee7a
No related branches found
No related tags found
No related merge requests found
{-# LANGUAGE NamedFieldPuns #-}
module GEODE.Options
( Output(..)
, atLeastOne
( Input(..)
, IOConfig(..)
, Output(..)
, input
, ioConfig
, output ) where
import Control.Applicative ((<|>), empty, many, optional)
import Data.List.NonEmpty (NonEmpty(..), nonEmpty)
import Data.Maybe (catMaybes)
import Control.Applicative ((<|>))
import Data.List.NonEmpty (NonEmpty(..))
import Options.Applicative
( Parser, flag', help, long, metavar, short
, strOption )
( Parser, argument, flag', help, long, metavar, short, str, strOption, value )
import Options.Applicative.Types (oneM, manyM, fromM)
import Text.Printf (printf)
data Input = StdIn | File FilePath
input :: String -> Parser Input
input stdinSemantics =
argument
(File <$> str)
( value StdIn
<> metavar "INPUT_FILE"
<> help (printf "path of the file to process (%s)" stdinSemantics) )
data Output = Metadata | TextRoot FilePath | XMLRoot FilePath
output :: Parser Output
output =
flag' Metadata ( long "metadata" <> short 'm')
<|> (TextRoot <$> strOption ( long "text-root" <> short 't'))
<|> (TextRoot <$> strOption ( long "xml-root" <> short 'x'))
flag' Metadata ( long "metadata"
<> short 'm'
<> help "Print metadata for splitted files on stdout" )
<|> (TextRoot <$> strOption
( long "text-root"
<> short 't'
<> help "Root path where to output text files" ))
<|> (XMLRoot <$> strOption
( long "xml-root"
<> short 'x'
<> help "Root path where to output XML files" ))
data IOConfig = IOConfig
{ from :: Input
, to :: NonEmpty Output }
atLeastOne :: Parser a -> Parser (NonEmpty a)
atLeastOne p = (:|) <$> p <*> many p
ioConfig :: String -> Parser IOConfig
ioConfig stdinSemantics = IOConfig
<$> input stdinSemantics
<*> some output
where
-- trick needed to avoid duplicated options in help message (occurring when
-- working directly at the Applicative level)
some p = fromM ((:|) <$> oneM p <*> manyM p)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment