Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cabal/src/Distribution/Simple/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module Distribution.Simple.Command
-- * Option Descriptions
, OptDescr (..)
, fmapOptDescr
, commandOptionsUsageInfo
, Description
, SFlags
, LFlags
Expand Down Expand Up @@ -467,6 +468,11 @@ commandHelp command pname =
where
cname = commandName command

-- | Render a table of options (given as 'OptionField's) for inclusion in a
-- help text, in the same format as the options section of 'commandHelp'.
commandOptionsUsageInfo :: String -> [OptionField flags] -> String
commandOptionsUsageInfo header = GetOpt.usageInfo header . concatMap viewAsGetOpt

-- | Default "usage" documentation text for commands.
usageDefault :: String -> String -> String
usageDefault name pname =
Expand Down Expand Up @@ -708,7 +714,7 @@ commandsRunWithFallback globalCommand commands defaultCommand args =
case lookupCommand name of
[Command _ _ action _] ->
case action ("--help" : cmdArgs') of
CommandHelp help -> pure $ CommandHelp help
CommandHelp help -> pure $ CommandReadyToGo (flags, CommandHelp help)
CommandList _ -> pure $ CommandList []
_ -> pure $ CommandHelp globalHelp
_ -> do
Expand Down
15 changes: 15 additions & 0 deletions cabal-install/src/Distribution/Client/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ import Distribution.Simple.Command
, CommandSpec (..)
, CommandType (..)
, CommandUI (..)
, ShowOrParseArgs (..)
, commandAddAction
, commandFromSpec
, commandOptionsUsageInfo
, commandShowOptions
, commandsRunWithFallback
, defaultCommandFallback
Expand Down Expand Up @@ -324,6 +326,18 @@ main args = do

mainWorker . (++ args1) =<< expandResponse args0

-- | The section listing the global flags, appended to the help text of
-- every command, so that global options such as @--store-dir@ can be
-- discovered there (see issue #7587).
globalFlagsSection :: String
globalFlagsSection =
"\n"
++ commandOptionsUsageInfo
"Global flags:"
(commandOptions (globalCommand []) ShowArgs)
++ "\nGlobal flags must be given before the command, e.g.\n"
++ "`cabal --store-dir=DIR build`.\n"

-- | Check whether assertions are enabled and print a warning in that case.
warnIfAssertionsAreEnabled :: IO ()
warnIfAssertionsAreEnabled =
Expand Down Expand Up @@ -405,6 +419,7 @@ mainWorker args = do
printCommandHelp help = do
pname <- getProgName
putStr (help pname)
putStr globalFlagsSection
printGlobalHelp help = do
pname <- getProgName
configFile <- defaultConfigFile
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# cabal build
# cabal --help
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Test.Cabal.Prelude

main = cabalTest $ do
-- The help of an individual command should mention the global flags.
buildHelp <- cabal' "build" ["--help"]
assertOutputContains "Global flags:" buildHelp
assertOutputContains "--store-dir=DIR" buildHelp

-- The same help text is reachable via `cabal help build`.
buildHelp2 <- cabal_raw' ["help", "build"] Nothing
assertOutputContains "--store-dir=DIR" buildHelp2

-- For comparison, the global help has always listed `--store-dir`.
globalHelp <- cabal' "--help" []
assertOutputContains "--store-dir=DIR" globalHelp
41 changes: 41 additions & 0 deletions changelog.d/12299.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
synopsis: List global flags in the help text of every command
packages: [Cabal, cabal-install]
prs: 12299
issues: 7587
---

Example of the new `cabal build --help` output:

```diff
Examples:
cabal build
Build the package in the current directory or all packages in the project
cabal build pkgname
Build the package named pkgname in the project
cabal build ./pkgfoo
Build the package in the ./pkgfoo directory
cabal build cname
Build the component named cname in the project
cabal build cname --enable-profiling
Build the component in profiling mode (including dependencies as needed)
+
+Global flags:
+ -V, --version # Print version information
+ --full-version # Print full version information with git
+ revision (if available) and compiler
+ --numeric-version # Print just the version number
+ --config-file=FILE # Set an alternate location for the config file
+ --ignore-expiry # Ignore expiry dates on signed metadata (use
+ only in exceptional circumstances)
+ --http-transport=HttpTransport
+ # Set a transport for http(s) requests. Accepts
+ 'curl', 'wget', 'powershell', and
+ 'plain-http'. (default: 'curl')
+ --store-dir=DIR # The location of the build store
+ --active-repositories=REPOS # The active package repositories (set to
+ ':none' to disable all repositories)
+
+Global flags must be given before the command, e.g.
+`cabal --store-dir=DIR build`.
```
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/bench.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/clean.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/configure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/copy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/freeze.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/gen-bounds.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/haddock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/reconfigure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
19 changes: 19 additions & 0 deletions doc/cmd-v1-help/register.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,22 @@ case with the nix-style commands.

For more information, see:
https://cabal.readthedocs.io/en/latest/nix-local-build-overview.html

Global flags:
-V, --version # Print version information
--full-version # Print full version information with git
revision (if available) and compiler
--numeric-version # Print just the version number
--config-file=FILE # Set an alternate location for the config file
--ignore-expiry # Ignore expiry dates on signed metadata (use
only in exceptional circumstances)
--http-transport=HttpTransport
# Set a transport for http(s) requests. Accepts
'curl', 'wget', 'powershell', and
'plain-http'. (default: 'curl')
--store-dir=DIR # The location of the build store
--active-repositories=REPOS # The active package repositories (set to
':none' to disable all repositories)

Global flags must be given before the command, e.g.
`cabal --store-dir=DIR build`.
Loading
Loading