Skip to content

Commit

Permalink
Switch to optparse-applicative.
Browse files Browse the repository at this point in the history
  • Loading branch information
sw17ch committed Jan 13, 2013
1 parent 53f97df commit cf3436e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 55 deletions.
24 changes: 12 additions & 12 deletions plunge.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ executable plunge
hs-source-dirs: src
main-is: Plunge.hs
-- other-modules:
build-depends: base >= 4.5,
language-c >= 0.4,
bytestring >= 0.9.2,
directory >= 1.1,
parsec >= 3.1,
containers >= 0.4,
mtl >= 2.1,
pretty >= 1.1,
prettyclass >= 1.0,
process >= 1.1,
pretty-show >= 1.2,
cmdargs >= 0.10.1
build-depends: base >= 4.5,
language-c >= 0.4,
bytestring >= 0.9.2,
directory >= 1.1,
parsec >= 3.1,
containers >= 0.4,
mtl >= 2.1,
pretty >= 1.1,
prettyclass >= 1.0,
process >= 1.1,
pretty-show >= 1.2,
optparse-applicative >= 0.5.2
7 changes: 4 additions & 3 deletions src/Plunge.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Main where

import System.Console.CmdArgs
import Options.Applicative

import Plunge.Options
import Plunge.Parsers.PreprocessorOutput
Expand All @@ -10,9 +10,10 @@ import Plunge.Printers.Analytics
import Plunge.Types.PreprocessorOutput

main :: IO ()
main = do
opts <- cmdArgs defaultOpts
main = execParser options >>= runWithOptions

runWithOptions :: Options -> IO ()
runWithOptions opts = do
cppResult <- preprocessFile (inputFile opts) (gccOptions opts)
cData <- readFile (inputFile opts)

Expand Down
86 changes: 46 additions & 40 deletions src/Plunge/Options.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{-# LANGUAGE DeriveDataTypeable #-}
module Plunge.Options ( Options(..)
, defaultOpts
, options
) where

import Paths_plunge

import Data.Data
import Data.List
import Data.Monoid
import Data.Version
import System.Console.CmdArgs
import Options.Applicative

data Options = Options { inputFile :: FilePath
, gccOptions :: [String]
Expand All @@ -19,37 +18,51 @@ data Options = Options { inputFile :: FilePath
-- , colorize :: Bool
, verticalSep :: String
, horizSep :: String
} deriving (Show, Data, Typeable)
} deriving (Show)

defaultOpts :: Options
defaultOpts = Options { inputFile = def &= explicit &= name "input-file"
&= name "i"
&= typ "FILE"
&= inputFile_help
, gccOptions = def &= explicit &= name "gcc-option"
&= name "g"
&= typ "OPTION"
&= gccOptions_help
, linePadder = " " &= explicit &= name "line-padding"
&= name "p"
&= typ "STRING"
, emptyLine = "." &= explicit &= name "empty-line-padding"
&= name "e"
&= typ "STRING"
, maxWidth = Nothing &= explicit &= name "max-width"
&= name "w"
&= typ "NUMBER"
-- , showLineNums = False &= name "show-line-numbers"
-- , colorize = False &= name "colorize"
, verticalSep = " | " &= explicit &= name "vertical-sep"
&= name "v"
&= typ "STRING"
, horizSep = "-" &= explicit &= name "horizontal-sep"
&= name "h"
&= typ "STRING"
} &= program "plunge"
&= summary summary_str
optParser :: Parser Options
optParser = Options
<$> strOption
( long "input-file" <> short 'i'
<> metavar "FILE"
<> help "The C file to analyze." )
<*> (many $ strOption
( long "gcc-option" <> short 'g'
<> metavar "OPTION"
<> help "An option to pass to GCC. Can be specified multiple times." ))
<*> option
( long "line-pad" <> short 'p'
<> metavar "STRING"
<> help "String to use to pad lines."
<> value " "
<> showDefault )
<*> option
( long "empty-line" <> short 'e'
<> metavar "STRING"
<> help "String to use to represent empty lines"
<> value "-"
<> showDefault )
<*> optional ( option
( long "max-width" <> short 'm'
<> metavar "NUMBER"
<> help "How wide each column of output is allowed to be"
<> value 80
<> showDefault ))
<*> option
( long "vert-sep" <> short 'v'
<> metavar "STRING"
<> help "What string to use to separate the two columns"
<> value " | "
<> showDefault )
<*> option
( long "horiz-sep" <> short 'h'
<> metavar "STRING"
<> help "What string to use to separate horizontal segments"
<> value "-"
<> showDefault )

options :: ParserInfo Options
options = info (helper <*> optParser) (header summary_str)

summary_str :: String
summary_str = let tags = versionTags version
Expand All @@ -58,10 +71,3 @@ summary_str = let tags = versionTags version
[] -> ""
_ -> " (" ++ (concat $ intersperse ", " $ tags) ++ ")"
in "Plunge " ++ branch_str ++ tags_str ++ ", (C) John Van Enk 2012"


inputFile_help :: Ann
inputFile_help = help "The C file to analyze."

gccOptions_help :: Ann
gccOptions_help = help "An option to pass to GCC. Can be specified multiple times."

0 comments on commit cf3436e

Please sign in to comment.