mirror of
https://github.com/zoriya/applicative-getopt.git
synced 2025-12-06 05:36:09 +00:00
Making combinaison work & adding default values
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
29
Main.hs
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user