optparse-simple (empty) → 0.0.0
raw patch · 4 files changed
+154/−0 lines, 4 filesdep +basedep +eitherdep +gitrevsetup-changed
Dependencies added: base, either, gitrev, optparse-applicative, template-haskell, transformers
Files
- LICENSE +24/−0
- Setup.hs +2/−0
- optparse-simple.cabal +23/−0
- src/Options/Applicative/Simple.hs +105/−0
+ LICENSE view
@@ -0,0 +1,24 @@+Copyright (c) 2015, optparse-simple+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:+ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.+ * Neither the name of optparse-simple nor the+ names of its contributors may be used to endorse or promote products+ derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ optparse-simple.cabal view
@@ -0,0 +1,23 @@+name: optparse-simple+version: 0.0.0+synopsis: Simple interface to optparse-applicative+description: Simple interface to optparse-applicative+license: BSD3+license-file: LICENSE+author: FP Complete+maintainer: chrisdone@fpcomplete.com+copyright: 2015 FP Complete+category: Options+build-type: Simple+cabal-version: >=1.8++library+ hs-source-dirs: src/+ ghc-options: -Wall -O2+ exposed-modules: Options.Applicative.Simple+ build-depends: base >= 4 && <5+ , template-haskell+ , transformers+ , optparse-applicative+ , gitrev+ , either
+ src/Options/Applicative/Simple.hs view
@@ -0,0 +1,105 @@+{-# LANGUAGE TemplateHaskell #-}++-- | Simple interface to program arguments.+--+-- Typical usage with no commands:+--+-- @+-- do (opts,()) <-+-- simpleOptions "ver"+-- "header"+-- "desc"+-- (flag () () (long "some-flag"))+-- empty+-- doThings opts+-- @+--+-- Typical usage with commands:+--+-- @+-- do (opts,runCmd) <-+-- simpleOptions "ver"+-- "header"+-- "desc"+-- (pure ()) $+-- do addCommand "delete"+-- "Delete the thing"+-- (const deleteTheThing)+-- (pure ())+-- addCommand "create"+-- "Create a thing"+-- createAThing+-- (strOption (long "hello"))+-- runCmd+-- @++module Options.Applicative.Simple+ ( module Options.Applicative.Simple+ , module Options.Applicative+ ) where++import Control.Monad.Trans.Class (lift)+import Control.Monad.Trans.Either+import Control.Monad.Trans.Writer+import Data.Monoid+import Data.Version+import Development.GitRev (gitDirty, gitHash)+import Language.Haskell.TH (Q,Exp)+import qualified Language.Haskell.TH.Syntax as TH+import Options.Applicative++-- | Generate a simple options parser.+simpleOptions+ :: String+ -- ^ version string+ -> String+ -- ^ header+ -> String+ -- ^ program description+ -> Parser a+ -- ^ global settings+ -> EitherT b (Writer (Mod CommandFields b)) ()+ -- ^ commands (use 'addCommand')+ -> IO (a,b)+simpleOptions versionString h pd globalParser commandParser =+ execParser $+ info (helpOption <*> versionOption <*> config) desc+ where desc = fullDesc <> header h <> progDesc pd+ helpOption =+ abortOption ShowHelpText $+ long "help" <>+ help "Show this help text"+ versionOption =+ infoOption+ versionString+ (long "version" <>+ help "Show version")+ config =+ (,) <$> globalParser <*>+ case (runWriter (runEitherT commandParser)) of+ (Right (),d) -> subparser d+ (Left b,_) -> pure b++-- | Generate a string like @Version 1.2, Git revision 1234@.+--+-- @$(simpleVersion …)@ @::@ 'String'+simpleVersion :: Version -> Q Exp+simpleVersion version =+ [|concat ["Version "+ ,$(TH.lift $ showVersion version)+ ,", Git revision "+ ,$gitHash+ ,if $gitDirty+ then " (dirty)"+ else ""]|]++-- | Add a command to the options dispatcher.+addCommand :: String -- ^ command string+ -> String -- ^ title of command+ -> (a -> b) -- ^ constructor to wrap up command in common data type+ -> Parser a -- ^ command parser+ -> EitherT b (Writer (Mod CommandFields b)) ()+addCommand cmd title constr inner =+ lift (tell (command cmd+ (info (constr <$> inner)+ (progDesc title))))