@@ -17,16 +17,18 @@ module Xrefcheck.CLI
1717 , getCommand
1818 ) where
1919
20+ import qualified Data.Char as C
2021import qualified Data.List as L
22+ import qualified Data.Text as T
2123import Data.Version (showVersion )
22- import Options.Applicative ( Parser , ReadM , command , eitherReader , execParser , flag' , footerDoc , fullDesc ,
23- help , helper , hsubparser , info , infoOption , long , metavar , option , progDesc ,
24- short , strOption , switch , value )
24+ import Options.Applicative
25+ ( Parser , ReadM , command , eitherReader , execParser , flag' , footerDoc , fullDesc , help , helper ,
26+ hsubparser , info , infoOption , long , metavar , option , progDesc , short , strOption , switch , value )
2527import Options.Applicative.Help.Pretty (Doc , displayS , fill , fillSep , indent , renderPretty , text )
2628
2729import Paths_xrefcheck (version )
28- import Xrefcheck.Config
2930import Xrefcheck.Core
31+ import Xrefcheck.Scan
3032
3133modeReadM :: ReadM VerifyMode
3234modeReadM = eitherReader $ \ s ->
@@ -45,7 +47,7 @@ modeReadM = eitherReader $ \s ->
4547
4648data Command
4749 = DefaultCommand Options
48- | DumpConfig FilePath
50+ | DumpConfig Flavor FilePath
4951
5052data Options = Options
5153 { oConfigPath :: Maybe FilePath
@@ -71,6 +73,24 @@ addTraversalOptions TraversalConfig{..} (TraversalOptions ignored) =
7173defaultConfigPaths :: [FilePath ]
7274defaultConfigPaths = [" ./xrefcheck.yaml" , " ./.xrefcheck.yaml" ]
7375
76+ -- | Strictly speaking, what config we will dump depends on the repository type:
77+ -- this affects Markdown flavor, things excluded by default, e.t.c.
78+ --
79+ -- But at the moment there is one-to-one correspondence between repository types
80+ -- and flavors, so we write a type alias here.
81+ type RepoType = Flavor
82+
83+ repoTypeReadM :: ReadM RepoType
84+ repoTypeReadM = eitherReader $ \ name ->
85+ maybeToRight (failureText name) $ L. lookup (map C. toLower name) allRepoTypesNamed
86+ where
87+ allRepoTypesNamed =
88+ allRepoTypes <&> \ ty -> (toString $ T. toLower (show ty), ty)
89+ failureText name =
90+ " Unknown repository type: " <> show name <> " \n \
91+ \Expected one of: " <> mconcat (intersperse " , " $ map show allRepoTypes)
92+ allRepoTypes = allFlavors
93+
7494optionsParser :: Parser Options
7595optionsParser = do
7696 oConfigPath <- optional . strOption $
@@ -122,13 +142,23 @@ traversalOptionsParser = do
122142 help " Files and folders which we pretend do not exist."
123143 return TraversalOptions {.. }
124144
125- dumpConfigOptions :: Parser FilePath
145+ dumpConfigOptions :: Parser Command
126146dumpConfigOptions = hsubparser $
127147 command " dump-config" $
128148 info parser $
129149 progDesc " Dump default configuration into a file."
130150 where
131- parser = strOption $
151+ parser = DumpConfig <$> repoTypeOption <*> outputOption
152+
153+ repoTypeOption =
154+ option repoTypeReadM $
155+ short ' t' <>
156+ long " type" <>
157+ metavar " REPOSITORY TYPE" <>
158+ help " Git repository type."
159+
160+ outputOption =
161+ strOption $
132162 short ' o' <>
133163 long " output" <>
134164 metavar " FILEPATH" <>
@@ -138,7 +168,7 @@ dumpConfigOptions = hsubparser $
138168totalParser :: Parser Command
139169totalParser = asum
140170 [ DefaultCommand <$> optionsParser
141- , DumpConfig <$> dumpConfigOptions
171+ , dumpConfigOptions
142172 ]
143173
144174versionOption :: Parser (a -> a )
0 commit comments