From 4dc9ac0d9f5b5d1865a0e32dbf4f21d6af38232a Mon Sep 17 00:00:00 2001 From: Ignat Insarov Date: Tue, 6 Sep 2022 14:00:08 +0200 Subject: [PATCH] Say the help message when given no targets. --- cabal-prettify.cabal | 4 +++- executables/cabal-prettify/Main.hs | 37 ++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/cabal-prettify.cabal b/cabal-prettify.cabal index 70aa580..f88919f 100644 --- a/cabal-prettify.cabal +++ b/cabal-prettify.cabal @@ -1,6 +1,6 @@ cabal-version: 3.0 name: cabal-prettify -version: 0.2.0.8 +version: 0.2.1.0 author: Ignat Insarov maintainer: kindaro@gmail.com tested-with: GHC == 8.10.7, GHC == 9.0.1, GHC == 9.2.1 @@ -14,6 +14,7 @@ common commons , Cabal , directory , filepath + , generic-deriving , mtl , optparse-applicative , parsec @@ -25,6 +26,7 @@ common commons default-extensions: ApplicativeDo BlockArguments + DeriveGeneric DuplicateRecordFields FlexibleContexts FlexibleInstances diff --git a/executables/cabal-prettify/Main.hs b/executables/cabal-prettify/Main.hs index 5333cb9..22931c6 100644 --- a/executables/cabal-prettify/Main.hs +++ b/executables/cabal-prettify/Main.hs @@ -18,6 +18,9 @@ import Control.Monad.Trans.Writer import System.Directory import Control.Monad import System.IO.Error +import GHC.Generics (Generic) +import Generics.Deriving.Monoid +import Generics.Deriving.Semigroup import Distribution.Prettify @@ -46,17 +49,27 @@ run Command {..} = do if and outcomes then exitSuccess else exitWith (ExitFailure 1) data Targets = Targets - { thisPackage ∷ Bool - , standardInput ∷ Bool + { thisPackage ∷ Any + , standardInput ∷ Any , arguments ∷ [FilePath] - } deriving (Eq, Ord, Show, Read) - -parseTargets ∷ Parser Targets -parseTargets = do - thisPackage ← switch (long "this" <> help "Prettify the configuration file of the package you are in right now.") - standardInput ← switch (long "filter" <> help "Prettify standard input.") - arguments ← many (argument str (metavar "{cabal files}")) - pure Targets {..} + } deriving (Eq, Ord, Show, Read, Generic) +instance Semigroup Targets where (<>) = gsappenddefault +instance Monoid Targets where mempty = gmemptydefault + +checkFlag ∷ Mod FlagFields ( ) → Parser ( ) +checkFlag = flag' ( ) + +parseThisPackage, parseStandardInput, parseArguments, parseTargets ∷ Parser Targets +parseThisPackage = do + checkFlag (long "this" <> help "Prettify the configuration file of the package you are in right now.") + pure do mempty {thisPackage = Any True} +parseStandardInput = do + checkFlag (long "filter" <> help "Prettify standard input.") + pure do mempty {standardInput = Any True} +parseArguments = do + arguments ← some (argument (str @FilePath) (metavar "{cabal files}")) + pure do mempty {arguments = arguments} +parseTargets = fmap mconcat do some (parseThisPackage <|> parseStandardInput <|> parseArguments) data Settings = Settings { check ∷ Bool @@ -77,10 +90,10 @@ data Action = Action processTargetsWithSettings ∷ Targets -> Settings -> IO [Action] processTargetsWithSettings Targets {..} settings = (fmap catMaybes ∘ sequence ∘ fmap sequence ∘ execWriter) do say do - whence thisPackage do + whence (getAny thisPackage) do pathToCabalFile ← Cabal.defaultPackageDesc Cabal.normal pure Action {target = Just pathToCabalFile, ..} - say do whence standardInput (pure Action {target = Nothing, ..}) + say do whence (getAny standardInput) (pure Action {target = Nothing, ..}) tell do for arguments \ pathToCabalFile → Just (pure Action {target = Just pathToCabalFile, ..}) runAction ∷ Action → IO Bool