60 lines
1.3 KiB
Haskell
60 lines
1.3 KiB
Haskell
{-# LANGUAGE LambdaCase #-}
|
|
|
|
module Main where
|
|
|
|
import Options.Applicative
|
|
import SortImports (sortImports)
|
|
import System.Environment (getArgs)
|
|
import System.IO (hReady, stdin)
|
|
|
|
versionNumber :: String
|
|
versionNumber = "v1.2.0"
|
|
|
|
version :: Parser Options
|
|
version = flag' Version $ mconcat
|
|
[ long "version"
|
|
, short 'v'
|
|
, help "Version number"
|
|
]
|
|
|
|
data Options
|
|
= Version
|
|
| FileInput FilePath
|
|
| StdInput
|
|
|
|
fileInput :: Parser Options
|
|
fileInput = FileInput <$> strOption (mconcat
|
|
[ long "file"
|
|
, short 'f'
|
|
, metavar "FILENAME"
|
|
, help "Input file"
|
|
])
|
|
|
|
stdInput :: Parser Options
|
|
stdInput = flag' StdInput $ mconcat
|
|
[ long "stdin"
|
|
, help "Read input from stdin"
|
|
]
|
|
|
|
options :: Parser Options
|
|
options = version <|> fileInput <|> stdInput
|
|
|
|
run :: Options -> IO ()
|
|
run = \case
|
|
Version -> putStrLn versionNumber
|
|
FileInput path -> readFile path >>= putStr . sortImports
|
|
StdInput -> interact sortImports
|
|
|
|
main :: IO ()
|
|
main = do
|
|
noArgs <- null <$> getArgs
|
|
anyStdin <- hReady stdin
|
|
if noArgs && anyStdin
|
|
then run StdInput
|
|
else execParser opts >>= run
|
|
where opts = info (options <**> helper) $ mconcat
|
|
[ briefDesc
|
|
, header "sort-imports - Sort Haskell import statements"
|
|
, footer "Input is read from stdin by default if no arguments are provided."
|
|
]
|