Making combinaison work & adding default values

This commit is contained in:
Zoe Roux
2021-03-11 15:23:33 +01:00
parent 9b35ef6ccf
commit 450d292e96
3 changed files with 31 additions and 7 deletions

View File

@@ -23,7 +23,9 @@ getOpt p args = case runParser p args of
runParser :: Parser a -> [String] -> Maybe (Parser a, [String])
runParser p@(DefParser _) args = Nothing
runParser (OptParser _ _) [] = Nothing
runParser (OptParser opt next) [] = do
def <- defaultValue opt
return (fmap def next, [])
runParser (OptParser _ _) [_] = Nothing -- TODO remove this and support default values
runParser (OptParser opt next) (identifier:arg:args)
| optionMatch opt identifier = do

View File

@@ -17,6 +17,7 @@ instance Applicative Parser where
pure a = DefParser a
-- (<*>) f (a -> b) -> f a -> f b
(<*>) (DefParser f) p = fmap f p
(<*>) (DefParser f) p = f <$> p
-- (<*>) (OptParser (Option (a -> b -> c)) Parser (a -> b)) -> Parser a -> Parser c
(<*>) (OptParser f next) p = OptParser () next
-- Ret: OptParser (Option ((a, b) -> c)) (Parser (a, b)) :: Parser (a, b)
(<*>) (OptParser f next) p = OptParser (uncurry <$> f) (fmap (,) next <*> p)

29
Main.hs
View File

@@ -24,14 +24,35 @@ getParser = Configuration
<> help "At witch line should we start"
<> value 0
)
<*> option (Just . auto) (
long "lines"
<> short 'l'
<> meta "LINES"
<> help "The number of lines to print"
<> value Nothing
)
<*> option auto (
long "window"
<> short 'w'
<> meta "WINDOW"
<> help "The size of the window (how long a line should be)"
<> value 80
)
<*> option auto (
long "move"
<> short 'm'
<> meta "MOVE"
<> help "Offset every lines."
<> value 0
)
data Configuration = Configuration {
rule :: Int,
start :: Int
-- lines :: Maybe Int,
-- window :: Int,
-- move :: Int
start :: Int,
lines :: Maybe Int,
window :: Int,
move :: Int
} deriving Show