Skip to content

Commit 6c083a9

Browse files
committed
Fix command line parsing
1 parent b02f6bb commit 6c083a9

File tree

1 file changed

+121
-12
lines changed

1 file changed

+121
-12
lines changed

cabal-install/src/Distribution/Client/CmdOutdated.hs

Lines changed: 121 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module Distribution.Client.CmdOutdated
1111
import qualified Data.Set as Set
1212

1313
import Distribution.Client.Compat.Prelude
14-
1514
import Distribution.Client.Config
1615
( SavedConfig
1716
( savedGlobalFlags
@@ -27,7 +26,6 @@ import Distribution.Client.NixStyleOptions
2726
import Distribution.Client.Outdated
2827
( IgnoreMajorVersionBumps (..)
2928
, ListOutdatedSettings (..)
30-
, OutdatedFlags (..)
3129
)
3230
import qualified Distribution.Client.Outdated as V1Outdated
3331
import Distribution.Client.ProjectConfig
@@ -46,7 +44,8 @@ import Distribution.Client.Sandbox
4644
( loadConfigOrSandboxConfig
4745
)
4846
import Distribution.Client.Setup
49-
( GlobalFlags (..)
47+
( ConfigFlags (..)
48+
, GlobalFlags (..)
5049
, configCompilerAux'
5150
, withRepoContext
5251
)
@@ -56,14 +55,27 @@ import Distribution.Client.Types.PackageLocation
5655
import Distribution.Client.Types.PackageSpecifier
5756
( PackageSpecifier (..)
5857
)
58+
import qualified Distribution.Compat.CharParsing as P
59+
import Distribution.ReadE
60+
( parsecToReadE
61+
)
5962
import Distribution.Simple.Command
6063
( CommandUI (..)
64+
, OptionField
65+
, ShowOrParseArgs
66+
, optArg
67+
, option
68+
, reqArg
6169
, usageAlternatives
6270
)
6371
import Distribution.Simple.Flag
64-
( flagToMaybe
72+
( Flag (..)
73+
, flagToMaybe
6574
, fromFlagOrDefault
6675
)
76+
import Distribution.Simple.Setup
77+
( trueArg
78+
)
6779
import Distribution.Simple.Utils
6880
( debug
6981
, dieWithException
@@ -101,13 +113,13 @@ outdatedCommand =
101113
{ commandName = "v2-outdated"
102114
, commandSynopsis = "Check for outdated dependencies."
103115
, commandUsage = usageAlternatives "v2-outdated" ["[FLAGS]", "[PACKAGES]"]
104-
, commandDefaultFlags = defaultNixStyleFlags V1Outdated.defaultOutdatedFlags
116+
, commandDefaultFlags = defaultNixStyleFlags defaultOutdatedFlags
105117
, commandDescription = Just $ \_ ->
106118
wrapText $
107119
"Checks for outdated dependencies in the package description file "
108120
++ "or freeze file"
109121
, commandNotes = Nothing
110-
, commandOptions = nixStyleOptions V1Outdated.outdatedOptions
122+
, commandOptions = nixStyleOptions outdatedOptions
111123
}
112124

113125
-- | To a first approximation, the @outdated@ command runs the first phase of
@@ -133,10 +145,11 @@ outdatedAction flags _extraArgs globalFlags = do
133145
sourcePkgDb <- IndexUtils.getSourcePackages verbosity repoContext
134146
prjBaseCtxt <- establishProjectBaseContext verbosity cliConfig OtherCommand
135147
pkgVerConstraints <-
136-
if | v1FreezeFile -> V1Outdated.depsFromFreezeFile verbosity
137-
| v2FreezeFile ->
138-
V1Outdated.depsFromNewFreezeFile verbosity globalFlags comp platform mprojectDir mprojectFile
139-
| otherwise -> pure $ extractPackageVersionConstraints (localPackages prjBaseCtxt)
148+
if
149+
| v1FreezeFile -> V1Outdated.depsFromFreezeFile verbosity
150+
| v2FreezeFile ->
151+
V1Outdated.depsFromNewFreezeFile verbosity globalFlags comp platform mprojectDir mprojectFile
152+
| otherwise -> pure $ extractPackageVersionConstraints (localPackages prjBaseCtxt)
140153

141154
debug verbosity $
142155
"Dependencies loaded: " ++ intercalate ", " (map prettyShow pkgVerConstraints)
@@ -155,7 +168,6 @@ outdatedAction flags _extraArgs globalFlags = do
155168
globalFlags
156169
flags
157170
mempty -- ClientInstallFlags, not needed here
158-
159171
outdatedFlags :: OutdatedFlags
160172
outdatedFlags = extraFlags flags
161173

@@ -185,7 +197,29 @@ outdatedAction flags _extraArgs globalFlags = do
185197
verbosity =
186198
if quiet
187199
then silent
188-
else fromFlagOrDefault normal (outdatedVerbosity outdatedFlags)
200+
else fromFlagOrDefault normal (configVerbosity $ configFlags flags)
201+
202+
data OutdatedFlags = OutdatedFlags
203+
{ outdatedFreezeFile :: Flag Bool
204+
, outdatedNewFreezeFile :: Flag Bool
205+
, outdatedSimpleOutput :: Flag Bool
206+
, outdatedExitCode :: Flag Bool
207+
, outdatedQuiet :: Flag Bool
208+
, outdatedIgnore :: [PackageName]
209+
, outdatedMinor :: Maybe IgnoreMajorVersionBumps
210+
}
211+
212+
defaultOutdatedFlags :: OutdatedFlags
213+
defaultOutdatedFlags =
214+
OutdatedFlags
215+
{ outdatedFreezeFile = mempty
216+
, outdatedNewFreezeFile = mempty
217+
, outdatedSimpleOutput = mempty
218+
, outdatedExitCode = mempty
219+
, outdatedQuiet = mempty
220+
, outdatedIgnore = mempty
221+
, outdatedMinor = mempty
222+
}
189223

190224
extractPackageVersionConstraints :: [PackageSpecifier (SourcePackage UnresolvedPkgLoc)] -> [PackageVersionConstraint]
191225
extractPackageVersionConstraints =
@@ -214,3 +248,78 @@ genericPackageDependencies gpd =
214248
where
215249
extract :: forall a confVar. Semigroup a => (UnqualComponentName, CondTree confVar [Dependency] a) -> [Dependency]
216250
extract = snd . ignoreConditions . snd
251+
252+
outdatedOptions :: ShowOrParseArgs -> [OptionField OutdatedFlags]
253+
outdatedOptions _showOrParseArgs =
254+
[ option
255+
[]
256+
["freeze-file", "v1-freeze-file"]
257+
"Act on the freeze file"
258+
outdatedFreezeFile
259+
(\v flags -> flags{outdatedFreezeFile = v})
260+
trueArg
261+
, option
262+
[]
263+
["v2-freeze-file", "new-freeze-file"]
264+
"Act on the new-style freeze file (default: cabal.project.freeze)"
265+
outdatedNewFreezeFile
266+
(\v flags -> flags{outdatedNewFreezeFile = v})
267+
trueArg
268+
, option
269+
[]
270+
["simple-output"]
271+
"Only print names of outdated dependencies, one per line"
272+
outdatedSimpleOutput
273+
(\v flags -> flags{outdatedSimpleOutput = v})
274+
trueArg
275+
, option
276+
[]
277+
["exit-code"]
278+
"Exit with non-zero when there are outdated dependencies"
279+
outdatedExitCode
280+
(\v flags -> flags{outdatedExitCode = v})
281+
trueArg
282+
, option
283+
['q']
284+
["quiet"]
285+
"Don't print any output. Implies '--exit-code' and '-v0'"
286+
outdatedQuiet
287+
(\v flags -> flags{outdatedQuiet = v})
288+
trueArg
289+
, option
290+
[]
291+
["ignore"]
292+
"Packages to ignore"
293+
outdatedIgnore
294+
(\v flags -> flags{outdatedIgnore = v})
295+
(reqArg "PKGS" pkgNameListParser (map prettyShow))
296+
, option
297+
[]
298+
["minor"]
299+
"Ignore major version bumps for these packages"
300+
outdatedMinor
301+
(\v flags -> flags{outdatedMinor = v})
302+
( optArg
303+
"PKGS"
304+
ignoreMajorVersionBumpsParser
305+
("", Just IgnoreMajorVersionBumpsAll)
306+
ignoreMajorVersionBumpsPrinter
307+
)
308+
]
309+
where
310+
ignoreMajorVersionBumpsPrinter
311+
:: Maybe IgnoreMajorVersionBumps
312+
-> [Maybe String]
313+
ignoreMajorVersionBumpsPrinter Nothing = []
314+
ignoreMajorVersionBumpsPrinter (Just IgnoreMajorVersionBumpsNone) = []
315+
ignoreMajorVersionBumpsPrinter (Just IgnoreMajorVersionBumpsAll) = [Nothing]
316+
ignoreMajorVersionBumpsPrinter (Just (IgnoreMajorVersionBumpsSome pkgs)) =
317+
map (Just . prettyShow) pkgs
318+
319+
ignoreMajorVersionBumpsParser =
320+
(Just . IgnoreMajorVersionBumpsSome) `fmap` pkgNameListParser
321+
322+
pkgNameListParser =
323+
parsecToReadE
324+
("Couldn't parse the list of package names: " ++)
325+
(fmap toList (P.sepByNonEmpty parsec (P.char ',')))

0 commit comments

Comments
 (0)