Opts.hs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. module Collage.Opts
  2. ( Command(..)
  3. , Options(..)
  4. , getOpts
  5. ) where
  6. import Control.Applicative ((<|>))
  7. import qualified Options.Applicative as Opt
  8. import qualified System.Directory as Sys
  9. import qualified System.FilePath as Sys
  10. data Command
  11. = Test
  12. | Splice
  13. deriving (Eq, Show)
  14. data Options = Options
  15. { optFile :: FilePath
  16. , optVerbose :: Bool
  17. , optCommand :: Command
  18. } deriving (Eq, Show)
  19. desc :: String
  20. desc = "FINISH ME"
  21. opts :: Opt.ParserInfo Options
  22. opts = Opt.info (p Opt.<**> Opt.helper)
  23. (Opt.progDesc desc <>
  24. Opt.fullDesc <>
  25. Opt.header "arglbargl")
  26. where
  27. p = Options <$> (path <|> pure "")
  28. <*> verbose
  29. <*> Opt.subparser (test <> splice)
  30. path = Opt.strOption
  31. (Opt.short 'f' <>
  32. Opt.long "file" <>
  33. Opt.metavar "PATH" <>
  34. Opt.help "The path to the project file")
  35. verbose = Opt.switch
  36. (Opt.short 'v' <>
  37. Opt.long "verbose" <>
  38. Opt.help "Show debug messages")
  39. test = Opt.command "test" $ Opt.info
  40. (pure Test Opt.<**> Opt.helper)
  41. (Opt.progDesc "test the provided sources")
  42. splice = Opt.command "splice" $ Opt.info
  43. (pure Splice Opt.<**> Opt.helper)
  44. (Opt.progDesc "splice sources into a final draft")
  45. getOpts :: IO Options
  46. getOpts = do
  47. cwd <- Sys.getCurrentDirectory
  48. options <- Opt.execParser opts
  49. return $ if null (optFile options)
  50. then options { optFile = cwd Sys.</> "collage" }
  51. else options