From 85df08a91312098173c3450ee0250ab88beb39d3 Mon Sep 17 00:00:00 2001 From: Alan Zimmerman Date: Mon, 15 Feb 2016 22:04:47 +0200 Subject: [PATCH] Add a argument parser and some haddocks I am not sure if I have set the ParserInfo up correctly, I have never used it before. --- Language/Haskell/GhcMod/Options/Options.hs | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Language/Haskell/GhcMod/Options/Options.hs b/Language/Haskell/GhcMod/Options/Options.hs index b0349f0..8d10d9d 100644 --- a/Language/Haskell/GhcMod/Options/Options.hs +++ b/Language/Haskell/GhcMod/Options/Options.hs @@ -17,10 +17,12 @@ {-# OPTIONS_GHC -fno-warn-unused-do-bind #-} module Language.Haskell.GhcMod.Options.Options ( - globalArgSpec + globalArgSpec + , parseCmdLineOptions ) where import Options.Applicative +import Options.Applicative.Help.Chunk import Options.Applicative.Types import Language.Haskell.GhcMod.Types import Control.Arrow @@ -30,6 +32,22 @@ import Language.Haskell.GhcMod.Read import Language.Haskell.GhcMod.Options.DocUtils import Language.Haskell.GhcMod.Options.Help +-- | Parse a set of arguments according to the ghc-mod CLI flag spec, producing +-- @Options@ set accordingly. +parseCmdLineOptions :: [String] -> Maybe Options +parseCmdLineOptions args = execParserMaybe parserInfo args + where + parserInfo + = ParserInfo + { infoParser = globalArgSpec + , infoFullDesc = True + , infoProgDesc = Chunk Nothing + , infoHeader = Chunk Nothing + , infoFooter = Chunk Nothing + , infoFailureCode = -1 + , infoIntersperse = True + } + splitOn :: Eq a => a -> [a] -> ([a], [a]) splitOn c = second (drop 1) . break (==c) @@ -119,6 +137,9 @@ programsArgSpec = Programs <=> showDefault <=> help "stack executable to use" +-- | An optparse-applicative @Parser@ sepcification for @Options@ so that +-- applications making use of the ghc-mod API can have a consistent way of +-- parsing global options. globalArgSpec :: Parser Options globalArgSpec = Options <$> outputOptsSpec