I'm attaching a patch that resolves ticket #7843. It adds new command line 
option --show-options 
that displays all GHC command line flags that are longer than two characters. 
This allows to have 
auto completion of command line options in bash.

Janek
From 7957ab015dc750fd66f2a34aa30f52544362e0f5 Mon Sep 17 00:00:00 2001
From: Jan Stolarek <[email protected]>
Date: Thu, 25 Apr 2013 16:07:35 +0200
Subject: [PATCH 1/3] Add --show-options to list all flags (Fixes #7843)

---
 compiler/main/StaticFlags.hs | 11 ++++++++---
 ghc/Main.hs                  | 20 +++++++++++++++++++-
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/compiler/main/StaticFlags.hs b/compiler/main/StaticFlags.hs
index c982d14..6e40f71 100644
--- a/compiler/main/StaticFlags.hs
+++ b/compiler/main/StaticFlags.hs
@@ -43,7 +43,10 @@ module StaticFlags (
         addOpt, removeOpt, v_opt_C_ready,
 
         -- Saving/restoring globals
-        saveStaticFlagGlobals, restoreStaticFlagGlobals
+        saveStaticFlagGlobals, restoreStaticFlagGlobals,
+
+        -- For options autocompletion
+        flagsStatic, flagsStaticNames
   ) where
 
 #include "HsVersions.h"
@@ -148,8 +151,10 @@ flagsStatic = [
 
 
 isStaticFlag :: String -> Bool
-isStaticFlag f =
-  f `elem` [
+isStaticFlag f = f `elem` flagsStaticNames
+
+flagsStaticNames :: [String]
+flagsStaticNames = [
     "fdicts-strict",
     "fspec-inline-join-points",
     "fno-hi-version-check",
diff --git a/ghc/Main.hs b/ghc/Main.hs
index 35dbf5b..d581b17 100644
--- a/ghc/Main.hs
+++ b/ghc/Main.hs
@@ -109,6 +109,7 @@ main = do
                    ShowSupportedExtensions -> showSupportedExtensions
                    ShowVersion             -> showVersion
                    ShowNumVersion          -> putStrLn cProjectVersion
+                   ShowOptions             -> showOptions
         Right postStartupMode ->
             -- start our GHC session
             GHC.runGhc mbMinusB $ do
@@ -372,11 +373,13 @@ data PreStartupMode
   = ShowVersion             -- ghc -V/--version
   | ShowNumVersion          -- ghc --numeric-version
   | ShowSupportedExtensions -- ghc --supported-extensions
+  | ShowOptions             -- ghc --show-options
 
-showVersionMode, showNumVersionMode, showSupportedExtensionsMode :: Mode
+showVersionMode, showNumVersionMode, showSupportedExtensionsMode, showOptionsMode :: Mode
 showVersionMode             = mkPreStartupMode ShowVersion
 showNumVersionMode          = mkPreStartupMode ShowNumVersion
 showSupportedExtensionsMode = mkPreStartupMode ShowSupportedExtensions
+showOptionsMode             = mkPreStartupMode ShowOptions
 
 mkPreStartupMode :: PreStartupMode -> Mode
 mkPreStartupMode = Left
@@ -520,6 +523,7 @@ mode_flags =
   , Flag "-version"              (PassFlag (setMode showVersionMode))
   , Flag "-numeric-version"      (PassFlag (setMode showNumVersionMode))
   , Flag "-info"                 (PassFlag (setMode showInfoMode))
+  , Flag "-show-options"         (PassFlag (setMode showOptionsMode))
   , Flag "-supported-languages"  (PassFlag (setMode showSupportedExtensionsMode))
   , Flag "-supported-extensions" (PassFlag (setMode showSupportedExtensionsMode))
   ] ++
@@ -693,6 +697,20 @@ showSupportedExtensions = mapM_ putStrLn supportedLanguagesAndExtensions
 showVersion :: IO ()
 showVersion = putStrLn (cProjectName ++ ", version " ++ cProjectVersion)
 
+showOptions :: IO ()
+showOptions = putStr (unlines availableOptions)
+    where
+      availableOptions     = getFlagNames mode_flags   ++ 
+                             getFlagNames flagsDynamic ++
+                             (filterUnwantedStatic . getFlagNames $ flagsStatic) ++ 
+                             flagsStaticNames
+      getFlagNames opts         = map getFlagName opts
+      getFlagName (Flag name _) = name
+      -- this is a hack to get rid of two unwanted entries that get listed
+      -- as static flags. Hopefully this hack will disappear one day together
+      -- with static flags
+      filterUnwantedStatic      = filter (\x -> not (x `elem` ["f", "fno-"]))
+
 showGhcUsage :: DynFlags -> IO ()
 showGhcUsage = showUsage False
 
-- 
1.8.0


From a152cd5bc6cdb17389c0418abd466b5c6b632ca0 Mon Sep 17 00:00:00 2001
From: Jan Stolarek <[email protected]>
Date: Thu, 25 Apr 2013 16:29:47 +0200
Subject: [PATCH 2/3] List only option longer than 2 characters (#7843)

---
 ghc/Main.hs | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/ghc/Main.hs b/ghc/Main.hs
index d581b17..895c95e 100644
--- a/ghc/Main.hs
+++ b/ghc/Main.hs
@@ -700,7 +700,8 @@ showVersion = putStrLn (cProjectName ++ ", version " ++ cProjectVersion)
 showOptions :: IO ()
 showOptions = putStr (unlines availableOptions)
     where
-      availableOptions     = getFlagNames mode_flags   ++ 
+      availableOptions     = filter ((>2) . length) $
+                             getFlagNames mode_flags   ++ 
                              getFlagNames flagsDynamic ++
                              (filterUnwantedStatic . getFlagNames $ flagsStatic) ++ 
                              flagsStaticNames
-- 
1.8.0


From 36d2d1c1ee858a46a0d04e9422746327421e180a Mon Sep 17 00:00:00 2001
From: Jan Stolarek <[email protected]>
Date: Fri, 26 Apr 2013 08:26:15 +0200
Subject: [PATCH 3/3] Add leading "-" when listing options

---
 ghc/Main.hs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ghc/Main.hs b/ghc/Main.hs
index 895c95e..4d44c69 100644
--- a/ghc/Main.hs
+++ b/ghc/Main.hs
@@ -700,7 +700,7 @@ showVersion = putStrLn (cProjectName ++ ", version " ++ cProjectVersion)
 showOptions :: IO ()
 showOptions = putStr (unlines availableOptions)
     where
-      availableOptions     = filter ((>2) . length) $
+      availableOptions     = map ((:) '-') . filter ((>2) . length) $
                              getFlagNames mode_flags   ++ 
                              getFlagNames flagsDynamic ++
                              (filterUnwantedStatic . getFlagNames $ flagsStatic) ++ 
-- 
1.8.0

_______________________________________________
ghc-devs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/ghc-devs

Reply via email to