Skip to content

Commit 9915ad1

Browse files
authored
Merge pull request #6726 from commercialhaskell/reorg-parsers
Refactoring to put shared flags parser in its own module
2 parents 480662b + abca13d commit 9915ad1

File tree

6 files changed

+48
-29
lines changed

6 files changed

+48
-29
lines changed

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ library:
229229
- Stack.Options.DotParser
230230
- Stack.Options.EvalParser
231231
- Stack.Options.ExecParser
232+
- Stack.Options.FlagsParser
232233
- Stack.Options.GhcBuildParser
233234
- Stack.Options.GhciParser
234235
- Stack.Options.GhcVariantParser

src/Stack/Options/BuildParser.hs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,24 @@ License : BSD-3-Clause
88

99
module Stack.Options.BuildParser
1010
( buildOptsParser
11-
, flagsParser
12-
, targetsParser
1311
) where
1412

1513
import qualified Data.List as L
16-
import qualified Data.Map as Map
1714
import qualified Data.Text as T
1815
import Options.Applicative
1916
( Parser, completer, flag, flag', help, internal, long
20-
, metavar, option, strOption, switch, value
17+
, metavar, strOption, switch, value
2118
)
2219
import Options.Applicative.Args ( cmdOption )
2320
import Options.Applicative.Builder.Extra
2421
( firstBoolFlagsNoDefault, textArgument, textOption )
25-
import Stack.Options.Completion
26-
( flagCompleter, ghcOptsCompleter, targetCompleter )
27-
import Stack.Options.PackageParser ( readFlag )
22+
import Stack.Options.Completion ( ghcOptsCompleter, targetCompleter )
23+
import Stack.Options.FlagsParser ( flagsParser )
2824
import Stack.Options.Utils ( hideMods )
2925
import Stack.Prelude
3026
import Stack.Types.BuildOptsCLI
31-
( ApplyCLIFlag, BuildCommand, BuildOptsCLI (..)
32-
, BuildSubset (..), FileWatchOpts (..)
27+
( BuildCommand, BuildOptsCLI (..), BuildSubset (..)
28+
, FileWatchOpts (..)
3329
)
3430

3531
-- | Parser for CLI-only build arguments
@@ -127,7 +123,7 @@ buildOptsParser cmd = BuildOptsCLI
127123
<> internal
128124
)
129125

130-
-- | Parser for build targets. Also used by the @stack dot@ command.
126+
-- | Parser for build targets.
131127
targetsParser :: Parser [Text]
132128
targetsParser =
133129
many (textArgument
@@ -139,21 +135,6 @@ targetsParser =
139135
\for details."
140136
))
141137

142-
-- | Parser for the @--flag@ option, for Cabal flags.
143-
flagsParser :: Parser (Map.Map ApplyCLIFlag (Map.Map FlagName Bool))
144-
flagsParser = Map.unionsWith Map.union
145-
<$> many (option readFlag
146-
( long "flag"
147-
<> completer flagCompleter
148-
<> metavar "PACKAGE:[-]FLAG"
149-
<> help "Set (or unset) the Cabal flag for the package (or use '*' for \
150-
\all packages) (can be specified multiple times). Applies to \
151-
\project packages, packages included directly in the snapshot, \
152-
\and extra-deps. Takes precedence over any Cabal flags \
153-
\specified for the package in the snapshot or in the \
154-
\project-level configuration file (stack.yaml)."
155-
))
156-
157138
progsOptionsParser :: Parser [(Text, [Text])]
158139
progsOptionsParser =
159140
dummyProgOptionsParser

src/Stack/Options/DotParser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import Options.Applicative
2222
, strOption, switch
2323
)
2424
import Options.Applicative.Builder.Extra ( boolFlags, textArgument )
25-
import Stack.Options.BuildParser ( flagsParser )
2625
import Stack.Options.Completion ( targetCompleter )
26+
import Stack.Options.FlagsParser ( flagsParser )
2727
import Stack.Prelude
2828
import Stack.Types.DotOpts ( DotOpts (..) )
2929

src/Stack/Options/FlagsParser.hs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
4+
{-|
5+
Module : Stack.Options.FlagsParser
6+
Description : Parser for one or more Cabal flags.
7+
License : BSD-3-Clause
8+
9+
Parser for one or more Cabal flags.
10+
-}
11+
12+
module Stack.Options.FlagsParser
13+
( flagsParser
14+
) where
15+
16+
import qualified Data.Map as Map
17+
import Options.Applicative
18+
( Parser, completer, help, long, metavar, option )
19+
import Stack.Options.Completion ( flagCompleter )
20+
import Stack.Options.PackageParser ( readFlag )
21+
import Stack.Prelude
22+
import Stack.Types.BuildOptsCLI ( ApplyCLIFlag )
23+
24+
-- | Parser for one or more @--flag@ options, each for a Cabal flag.
25+
flagsParser :: Parser (Map.Map ApplyCLIFlag (Map.Map FlagName Bool))
26+
flagsParser = Map.unionsWith Map.union
27+
<$> many (option readFlag
28+
( long "flag"
29+
<> completer flagCompleter
30+
<> metavar "PACKAGE:[-]FLAG"
31+
<> help "Set (or unset) the Cabal flag for the package (or use '*' for \
32+
\all packages) (can be specified multiple times). Applies to \
33+
\project packages, packages included directly in the snapshot, \
34+
\and extra-deps. Takes precedence over any Cabal flags \
35+
\specified for the package in the snapshot or in the \
36+
\project-level configuration file (stack.yaml)."
37+
))

src/Stack/Options/GhciParser.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import Options.Applicative.Builder.Extra
2121
)
2222
import Stack.Config ( packagesParser )
2323
import Stack.Ghci ( GhciOpts (..) )
24-
import Stack.Options.BuildParser ( flagsParser )
25-
import Stack.Options.Completion
26-
( ghcOptsCompleter, targetCompleter )
24+
import Stack.Options.Completion ( ghcOptsCompleter, targetCompleter )
25+
import Stack.Options.FlagsParser ( flagsParser )
2726
import Stack.Prelude
2827

2928
-- | Parser for GHCI options

stack.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ library
265265
Stack.Options.DotParser
266266
Stack.Options.EvalParser
267267
Stack.Options.ExecParser
268+
Stack.Options.FlagsParser
268269
Stack.Options.GhcBuildParser
269270
Stack.Options.GhciParser
270271
Stack.Options.GhcVariantParser

0 commit comments

Comments
 (0)