|
| 1 | +-- app/OptParse.hs |
| 2 | + |
| 3 | +-- | Command-line options parsing |
| 4 | + |
| 5 | +module OptParse |
| 6 | + ( Options(..) |
| 7 | + , SingleInput(..) |
| 8 | + , SingleOutput(..) |
| 9 | + , parse |
| 10 | + ) |
| 11 | + where |
| 12 | + |
| 13 | +import Data.Maybe (fromMaybe) |
| 14 | +import Options.Applicative |
| 15 | + |
| 16 | +------------------------------------------------ |
| 17 | +-- * Our command-line options model |
| 18 | + |
| 19 | +-- | Model |
| 20 | +data Options |
| 21 | + = ConvertSingle SingleInput SingleOutput |
| 22 | + | ConvertDir FilePath FilePath |
| 23 | + deriving Show |
| 24 | + |
| 25 | +-- | A single input source |
| 26 | +data SingleInput |
| 27 | + = Stdin |
| 28 | + | InputFile FilePath |
| 29 | + deriving Show |
| 30 | + |
| 31 | +-- | A single output sink |
| 32 | +data SingleOutput |
| 33 | + = Stdout |
| 34 | + | OutputFile FilePath |
| 35 | + deriving Show |
| 36 | + |
| 37 | +------------------------------------------------ |
| 38 | +-- * Parser |
| 39 | + |
| 40 | +-- | Parse command-line options |
| 41 | +parse :: IO Options |
| 42 | +parse = execParser opts |
| 43 | + |
| 44 | +opts :: ParserInfo Options |
| 45 | +opts = |
| 46 | + info (pOptions <**> helper) |
| 47 | + ( fullDesc |
| 48 | + <> header "hs-blog-gen - a static blog generator" |
| 49 | + <> progDesc "Convert markup files or directories to html" |
| 50 | + ) |
| 51 | + |
| 52 | +-- | Parser for all options |
| 53 | +pOptions :: Parser Options |
| 54 | +pOptions = |
| 55 | + subparser |
| 56 | + ( command |
| 57 | + "convert" |
| 58 | + ( info |
| 59 | + (helper <*> pConvertSingle) |
| 60 | + (progDesc "Convert a single markup source to html") |
| 61 | + ) |
| 62 | + <> command |
| 63 | + "convert-dir" |
| 64 | + ( info |
| 65 | + (helper <*> pConvertDir) |
| 66 | + (progDesc "Convert a directory of markup files to html") |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | +------------------------------------------------ |
| 71 | +-- * Single source to sink conversion parser |
| 72 | + |
| 73 | +-- | Parser for single source to sink option |
| 74 | +pConvertSingle :: Parser Options |
| 75 | +pConvertSingle = |
| 76 | + ConvertSingle <$> pSingleInput <*> pSingleOutput |
| 77 | + |
| 78 | +-- | Parser for single input source |
| 79 | +pSingleInput :: Parser SingleInput |
| 80 | +pSingleInput = |
| 81 | + fromMaybe Stdin <$> optional pInputFile |
| 82 | + |
| 83 | +-- | Parser for single output sink |
| 84 | +pSingleOutput :: Parser SingleOutput |
| 85 | +pSingleOutput = |
| 86 | + fromMaybe Stdout <$> optional pOutputFile |
| 87 | + |
| 88 | +-- | Input file parser |
| 89 | +pInputFile :: Parser SingleInput |
| 90 | +pInputFile = fmap InputFile parser |
| 91 | + where |
| 92 | + parser = |
| 93 | + strOption |
| 94 | + ( long "input" |
| 95 | + <> short 'i' |
| 96 | + <> metavar "FILE" |
| 97 | + <> help "Input file" |
| 98 | + ) |
| 99 | + |
| 100 | +-- | Output file parser |
| 101 | +pOutputFile :: Parser SingleOutput |
| 102 | +pOutputFile = OutputFile <$> parser |
| 103 | + where |
| 104 | + parser = |
| 105 | + strOption |
| 106 | + ( long "output" |
| 107 | + <> short 'o' |
| 108 | + <> metavar "FILE" |
| 109 | + <> help "Output file" |
| 110 | + ) |
| 111 | + |
| 112 | +------------------------------------------------ |
| 113 | +-- * Directory conversion parser |
| 114 | + |
| 115 | +pConvertDir :: Parser Options |
| 116 | +pConvertDir = |
| 117 | + ConvertDir <$> pInputDir <*> pOutputDir |
| 118 | + |
| 119 | +-- | Parser for input directory |
| 120 | +pInputDir :: Parser FilePath |
| 121 | +pInputDir = |
| 122 | + strOption |
| 123 | + ( long "input" |
| 124 | + <> short 'i' |
| 125 | + <> metavar "DIRECTORY" |
| 126 | + <> help "Input directory" |
| 127 | + ) |
| 128 | + |
| 129 | +-- | Parser for output directory |
| 130 | +pOutputDir :: Parser FilePath |
| 131 | +pOutputDir = |
| 132 | + strOption |
| 133 | + ( long "output" |
| 134 | + <> short 'o' |
| 135 | + <> metavar "DIRECTORY" |
| 136 | + <> help "Output directory" |
| 137 | + ) |
0 commit comments