From 05355d4aab533d0c55666412b6c250e644bc7088 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 18 Nov 2013 13:51:55 -0400 Subject: use optparse-applicative --- debian/control | 3 ++- git-repair.cabal | 2 +- git-repair.hs | 34 ++++++++++++++++++---------------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/debian/control b/debian/control index f4999d7..bf14911 100644 --- a/debian/control +++ b/debian/control @@ -12,7 +12,8 @@ Build-Depends: libghc-extensible-exceptions-dev, libghc-unix-compat-dev, libghc-utf8-string-dev, - libghc-async-dev + libghc-async-dev, + libghc-optparse-applicative-dev Maintainer: Joey Hess Standards-Version: 3.9.4 Vcs-Git: git://git-repair.branchable.com/ diff --git a/git-repair.cabal b/git-repair.cabal index e649be0..15acb79 100644 --- a/git-repair.cabal +++ b/git-repair.cabal @@ -23,7 +23,7 @@ Executable git-repair Build-Depends: MissingH, hslogger, directory, filepath, containers, mtl, network, extensible-exceptions, unix-compat, bytestring, base >= 4.5, base < 5, IfElse, text, process, - utf8-string, async + utf8-string, async, optparse-applicative if (! os(windows)) Build-Depends: unix diff --git a/git-repair.hs b/git-repair.hs index f726893..9ece7e5 100644 --- a/git-repair.hs +++ b/git-repair.hs @@ -5,34 +5,36 @@ - Licensed under the GNU GPL version 3 or higher. -} -import System.Environment import Data.Tuple.Utils +import Options.Applicative import Common import qualified Git.CurrentRepo import qualified Git.Repair import qualified Git.Config -header :: String -header = "Usage: git-repair" +data Settings = Settings + { forced :: Bool + } -usage :: a -usage = error $ "bad parameters\n\n" ++ header - -parseArgs :: IO Bool -parseArgs = do - args <- getArgs - return $ or $ map parse args +parseSettings :: Parser Settings +parseSettings = Settings + <$> switch forceopt where - parse "--force" = True - parse _ = usage + forceopt = long "force" + <> help "Force recovery, even if data is lost" main :: IO () -main = do - forced <- parseArgs - +main = execParser opts >>= repair + where + opts = info (helper <*> parseSettings) desc + desc = fullDesc + <> header "git-repair - repair a damanged git repository" + +repair :: Settings -> IO () +repair settings = do g <- Git.Config.read =<< Git.CurrentRepo.get - ifM (fst3 <$> Git.Repair.runRepair forced g) + ifM (fst3 <$> Git.Repair.runRepair (forced settings) g) ( exitSuccess , exitFailure ) -- cgit v1.2.3