Add a -f option for reading files to process from another file or stdin

This commit is contained in:
John Wiegley 2018-04-05 20:43:13 -07:00
parent bd4a5f376b
commit a0651ad2fd

View file

@ -22,6 +22,7 @@ data Options = Options
, evaluate :: Bool
, check :: Bool
, expression :: Maybe String
, fromFile :: Maybe FilePath
, filePaths :: [FilePath]
}
@ -45,22 +46,43 @@ mainOptions = Options
( short 'e'
<> long "expr"
<> help "Expression to parse or evaluate"))
<*> optional (strOption
( short 'f'
<> long "file"
<> help "Parse all of the files given in FILE; - means stdin"))
<*> many (strArgument (metavar "FILE" <> help "Path of file to parse"))
main :: IO ()
main = do
opts <- execParser optsDef
(eres, mpath) <- case expression opts of
Just s -> return (parseNixStringLoc s, Nothing)
Nothing -> case filePaths opts of
[] -> (, Nothing) . parseNixStringLoc <$> getContents
["-"] -> (, Nothing) . parseNixStringLoc <$> getContents
[path] -> (, Just path) <$> parseNixFileLoc path
_ -> error "hnix doesn't support multiple path arguments yet"
case expression opts of
Just s -> handleResult opts Nothing (parseNixStringLoc s)
Nothing -> case fromFile opts of
Just "-" ->
mapM_ (processFile opts) =<< (lines <$> getContents)
Just path ->
mapM_ (processFile opts) =<< (lines <$> readFile path)
Nothing -> case filePaths opts of
[] ->
handleResult opts Nothing . parseNixStringLoc
=<< getContents
["-"] ->
handleResult opts Nothing . parseNixStringLoc
=<< getContents
paths ->
mapM_ (processFile opts) paths
where
optsDef :: ParserInfo Options
optsDef = info (helper <*> mainOptions)
(fullDesc <> progDesc "" <> header "hnix")
processFile opts path = do
eres <- parseNixFileLoc path
handleResult opts (Just path) eres
-- print . printNix =<< Nix.eval [nix|1 + 3|]
case eres of
handleResult opts mpath = \case
Failure err -> hPutStrLn stderr $ "Parse failed: " ++ show err
Success expr -> do
when (check opts) $
@ -78,7 +100,3 @@ main = do
. renderPretty 0.4 80
. prettyNix
. stripAnnotation $ expr
where
optsDef :: ParserInfo Options
optsDef = info (helper <*> mainOptions)
(fullDesc <> progDesc "" <> header "hnix")