Skip to content

Commit

Permalink
Hacky workaround for optparse-applicative issue with stack exec --help (
Browse files Browse the repository at this point in the history
fixes #806)

Downside: usage information now looks like:

Usage: stack exec [CMD -- ARGS (e.g. stack ghc -- X.hs -o x)]

Notice how "CMD" is inside the square brackets. Still seems like a
worthwhile tradeoff to me.
  • Loading branch information
snoyberg committed Sep 2, 2015
1 parent b0193e0 commit 4fee27f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Bug fixes:
* Create missing directories for `stack sdist`
* Don't ignore .cabal files with extra periods [#895](https://github.com/commercialhaskell/stack/issues/895)
* Deprecate unused `--optimizations` flag
* Hacky workaround for optparse-applicative issue with `stack exec --help` [#806](https://github.com/commercialhaskell/stack/issues/806)

## 0.1.3.1

Expand Down
11 changes: 6 additions & 5 deletions src/Stack/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -445,18 +445,19 @@ execOptsParser :: Maybe String -- ^ command
-> Parser ExecOpts
execOptsParser mcmd =
ExecOpts
<$> maybe eoCmdParser pure mcmd
<$> pure mcmd
<*> eoArgsParser
<*> (eoPlainParser <|>
ExecOptsEmbellished
<$> eoEnvSettingsParser
<*> eoPackagesParser)
where
eoCmdParser :: Parser String
eoCmdParser = strArgument (metavar "CMD")

eoArgsParser :: Parser [String]
eoArgsParser = many (strArgument (metavar "-- ARGS (e.g. stack ghc -- X.hs -o x)"))
eoArgsParser = many (strArgument (metavar meta))
where
meta =
(maybe ("CMD ") (const "") mcmd) ++
"-- ARGS (e.g. stack ghc -- X.hs -o x)"

eoEnvSettingsParser :: Parser EnvSettings
eoEnvSettingsParser = EnvSettings
Expand Down
5 changes: 4 additions & 1 deletion src/Stack/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ data EnvSettings = EnvSettings
deriving (Show, Eq, Ord)

data ExecOpts = ExecOpts
{ eoCmd :: !String
{ eoCmd :: !(Maybe String)
-- ^ Usage of @Maybe@ here is nothing more than a hack, to avoid some weird
-- bug in optparse-applicative. See:
-- https://github.com/commercialhaskell/stack/issues/806
, eoArgs :: ![String]
, eoExtra :: !ExecOptsExtra
}
Expand Down
13 changes: 9 additions & 4 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -761,19 +761,24 @@ sdistCmd dirs go =

-- | Execute a command.
execCmd :: ExecOpts -> GlobalOpts -> IO ()
execCmd ExecOpts {..} go@GlobalOpts{..} =
execCmd ExecOpts {..} go@GlobalOpts{..} = do
(cmd, args) <-
case (eoCmd, eoArgs) of
(Just cmd, args) -> return (cmd, args)
(Nothing, cmd:args) -> return (cmd, args)
(Nothing, []) -> error "You must provide a command to exec, e.g. 'stack exec echo Hello World'"
case eoExtra of
ExecOptsPlain -> do
(manager,lc) <- liftIO $ loadConfigWithOpts go
withUserFileLock (configStackRoot $ lcConfig lc) $ \lk ->
runStackTGlobal manager (lcConfig lc) go $
Docker.execWithOptionalContainer
(lcProjectRoot lc)
(return (eoCmd, eoArgs, [], id))
(return (cmd, args, [], id))
-- Unlock before transferring control away, whether using docker or not:
(Just $ liftIO $ unlockFile lk)
(runStackTGlobal manager (lcConfig lc) go $ do
exec plainEnvSettings eoCmd eoArgs)
exec plainEnvSettings cmd args)
Nothing
Nothing -- Unlocked already above.
ExecOptsEmbellished {..} ->
Expand All @@ -784,7 +789,7 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
{ boptsTargets = map T.pack targets
}
liftIO $ unlockFile lk -- Unlock before transferring control away.
exec eoEnvSettings eoCmd eoArgs
exec eoEnvSettings cmd args

-- | Run GHCi in the context of a project.
ghciCmd :: GhciOpts -> GlobalOpts -> IO ()
Expand Down

0 comments on commit 4fee27f

Please sign in to comment.