optparse-simple-0.1.1.2/example/0000755000000000000000000000000013410336365014622 5ustar0000000000000000optparse-simple-0.1.1.2/src/0000755000000000000000000000000013175625251013761 5ustar0000000000000000optparse-simple-0.1.1.2/src/Options/0000755000000000000000000000000013175625251015414 5ustar0000000000000000optparse-simple-0.1.1.2/src/Options/Applicative/0000755000000000000000000000000013457550725017663 5ustar0000000000000000optparse-simple-0.1.1.2/test/0000755000000000000000000000000013176047744014157 5ustar0000000000000000optparse-simple-0.1.1.2/src/Options/Applicative/Simple.hs0000644000000000000000000001326213457550705021452 0ustar0000000000000000-- Try to ensure that https://github.com/fpco/optparse-simple/issues/12 doesn't recur. {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE CPP #-} -- | 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.Except import Control.Monad.Trans.Writer #if !MIN_VERSION_base(4,11,0) import Data.Semigroup #endif import Data.Version import GitHash (GitInfo, giDirty, giHash, tGitInfoCwdTry) import Language.Haskell.TH (Q,Exp) import qualified Language.Haskell.TH.Syntax as TH import Options.Applicative import System.Environment -- | Generate and execute a simple options parser. simpleOptions :: String -- ^ version string -> String -- ^ header -> String -- ^ program description -> Parser a -- ^ global settings -> ExceptT b (Writer (Mod CommandFields b)) () -- ^ commands (use 'addCommand') -> IO (a,b) simpleOptions versionString h pd globalParser commandParser = do args <- getArgs case execParserPure (prefs idm) parser args of Failure _ | null args -> withArgs ["--help"] (execParser parser) parseResult -> handleParseResult parseResult where parser = info (versionOption <*> simpleParser globalParser commandParser) desc desc = fullDesc <> header h <> progDesc pd versionOption = infoOption versionString (long "version" <> help "Show version") -- | Generate a string like @Version 1.2, Git revision 1234@. -- -- @$(simpleVersion …)@ @::@ 'String' simpleVersion :: Version -> Q Exp simpleVersion version = [|concat (["Version " ,$(TH.lift $ showVersion version) ] ++ case $(TH.unTypeQ tGitInfoCwdTry) :: Either String GitInfo of Left _ -> [] Right gi -> [ ", Git revision " , giHash gi , if giDirty gi 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 -> ExceptT b (Writer (Mod CommandFields b)) () addCommand cmd title constr inner = lift (tell (command cmd (info (constr <$> (helper <*> inner)) (progDesc title)))) -- | Add a command that takes sub-commands to the options dispatcher. -- -- Example: -- -- @ -- addSubCommands "thing" -- "Subcommands that operate on things" -- (do addCommand "delete" -- "Delete the thing" -- (const deleteTheThing) -- (pure ()) -- addCommand "create" -- "Create a thing" -- createAThing -- (strOption (long "hello"))) -- @ -- -- If there are common options between all the sub-commands, use 'addCommand' -- in combination with 'simpleParser' instead of 'addSubCommands'. addSubCommands :: String -- ^ command string -> String -- ^ title of command -> ExceptT b (Writer (Mod CommandFields b)) () -- ^ sub-commands (use 'addCommand') -> ExceptT b (Writer (Mod CommandFields b)) () addSubCommands cmd title commandParser = addCommand cmd title (\((), a) -> a) (simpleParser (pure ()) commandParser) -- | Generate a simple options parser. -- -- Most of the time you should use 'simpleOptions' instead, but 'simpleParser' -- can be used for sub-commands that need common options. For example: -- -- @ -- addCommand "thing" -- "Subcommands that operate on things" -- (\\(opts,runSubCmd) -> runSubCmd opts) -- (simpleParser (flag () () (long "some-flag")) $ -- do addCommand "delete" -- "Delete the thing" -- (const deleteTheThing) -- (pure ()) -- addCommand "create" -- "Create a thing" -- createAThing -- (strOption (long "hello"))) -- @ -- simpleParser :: Parser a -- ^ common settings -> ExceptT b (Writer (Mod CommandFields b)) () -- ^ commands (use 'addCommand') -> Parser (a,b) simpleParser commonParser commandParser = helpOption <*> config where helpOption = abortOption ShowHelpText $ long "help" <> help "Show this help text" config = (,) <$> commonParser <*> case runWriter (runExceptT commandParser) of (Right (),d) -> subparser d (Left b,_) -> pure b optparse-simple-0.1.1.2/example/Simple.hs0000644000000000000000000000034213410336365016406 0ustar0000000000000000{-# LANGUAGE TemplateHaskell #-} module Main (main) where import Options.Applicative.Simple (simpleVersion) import qualified Paths_optparse_simple as Meta main :: IO () main = putStrLn $(simpleVersion Meta.version)optparse-simple-0.1.1.2/test/Main.hs0000644000000000000000000000623713176047744015407 0ustar0000000000000000{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE OverloadedStrings #-} import Options.Applicative.Simple hiding(action) import GHC.IO.Handle import System.IO import System.Environment import Control.Exception import Control.Monad import System.Directory import System.Exit import Data.ByteString (ByteString) import qualified Data.ByteString as BS import Data.Monoid ((<>)) shouldBe :: (Show a, Eq a) => a -> a -> IO () shouldBe actual expected | expected == actual = return () | otherwise = do putStrLn $ "expected: " ++ show expected putStrLn $ "actual : " ++ show actual exitFailure catchReturn :: Exception e => IO e -> IO e catchReturn io = io `catch` return catchExitCode :: IO () -> IO ExitCode catchExitCode action = catchReturn $ do action return ExitSuccess data FakeHandles = FakeHandles { fakeIn :: Handle , fakeOut :: Handle , fakeErr :: Handle , realIn :: Handle , realOut :: Handle , realErr :: Handle } openFile' :: FilePath -> IO Handle openFile' path = do removeIfExists path openFile path ReadWriteMode removeIfExists :: FilePath -> IO () removeIfExists path = do exists <- doesFileExist path when exists $ do removeFile path stdinFile :: FilePath stdinFile = ".tmp.stdin" stdoutFile :: FilePath stdoutFile = ".tmp.stdout" stderrFile :: FilePath stderrFile = ".tmp.stderr" beforeFH :: IO FakeHandles beforeFH = do realIn <- hDuplicate stdin realOut <- hDuplicate stdout realErr <- hDuplicate stderr fakeIn <- openFile stdinFile ReadWriteMode fakeOut <- openFile' stdoutFile fakeErr <- openFile' stderrFile hDuplicateTo fakeIn stdin hDuplicateTo fakeOut stdout hDuplicateTo fakeErr stderr return FakeHandles{..} afterFH :: FakeHandles -> IO () afterFH FakeHandles{..} = do hDuplicateTo realIn stdin hDuplicateTo realOut stdout hDuplicateTo realErr stderr hClose fakeIn hClose fakeOut hClose fakeErr withFakeHandles :: IO a -> IO a withFakeHandles = bracket beforeFH afterFH . const withStdIn :: ByteString -> IO () -> IO (ByteString, ByteString, ExitCode) withStdIn inBS action = do BS.writeFile stdinFile inBS withFakeHandles $ do _ <- catchExitCode action hFlush stdout hFlush stderr out <- BS.readFile stdoutFile err <- BS.readFile stderrFile removeIfExists stdinFile removeIfExists stdoutFile removeIfExists stderrFile return (out, err, ExitSuccess) main :: IO () main = do (out, err, exitCode) <- withStdIn "" $ withArgs ["--version"] $ simpleProg exitCode `shouldBe` ExitSuccess err `shouldBe` "" out `shouldBe` "version\n" (out', err', exitCode') <- withStdIn "" $ withArgs ["--summary"] $ summaryProg exitCode' `shouldBe` ExitSuccess err' `shouldBe` "" out' `shouldBe` "A program summary\n" return () simpleProg :: IO () simpleProg = do ((), ()) <- simpleOptions "version" "header" "desc" (pure ()) empty return () parserWithSummary :: Parser () parserWithSummary = summaryOption <*> pure () where summaryOption = infoOption "A program summary" $ long "summary" <> help "Show program summary" summaryProg :: IO () summaryProg = do ((), ()) <- simpleOptions "version" "header" "desc" parserWithSummary empty return () optparse-simple-0.1.1.2/LICENSE0000644000000000000000000000273313175625251014204 0ustar0000000000000000Copyright (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 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. optparse-simple-0.1.1.2/Setup.hs0000644000000000000000000000005613175625251014627 0ustar0000000000000000import Distribution.Simple main = defaultMain optparse-simple-0.1.1.2/optparse-simple.cabal0000644000000000000000000000357013457551231017306 0ustar0000000000000000cabal-version: 1.12 -- This file has been generated from package.yaml by hpack version 0.31.1. -- -- see: https://github.com/sol/hpack -- -- hash: fa55f93c89cb6aea5dd08d557e397a862c381f89f99d22f74b8699ceb6361534 name: optparse-simple version: 0.1.1.2 synopsis: Simple interface to optparse-applicative description: Please see the README at category: Options homepage: https://github.com/fpco/optparse-simple#readme bug-reports: https://github.com/fpco/optparse-simple/issues author: FP Complete maintainer: chrisdone@fpcomplete.com copyright: 2015-2017 FP Complete license: BSD3 license-file: LICENSE build-type: Simple extra-source-files: README.md ChangeLog.md source-repository head type: git location: https://github.com/fpco/optparse-simple flag build-example description: Build the example executable manual: True default: False library hs-source-dirs: src/ ghc-options: -Wall build-depends: base >=4.9.1 && <5 , githash >=0.1.3.0 , optparse-applicative , template-haskell , transformers >=0.4 if impl (ghc < 8.0) build-depends: semigroups ==0.18.* exposed-modules: Options.Applicative.Simple other-modules: Paths_optparse_simple default-language: Haskell2010 executable simple main-is: example/Simple.hs other-modules: Paths_optparse_simple build-depends: base >=4.9.1 && <5 , optparse-simple if flag(build-example) buildable: True else buildable: False default-language: Haskell2010 test-suite test type: exitcode-stdio-1.0 main-is: Main.hs hs-source-dirs: test ghc-options: -Wall build-depends: base , bytestring , directory , optparse-simple other-modules: Paths_optparse_simple default-language: Haskell2010 optparse-simple-0.1.1.2/README.md0000644000000000000000000000141213175625251014447 0ustar0000000000000000optparse-simple ===== Simple interface to optparse-applicative ## Usage Typical usage with no commands: ``` haskell do (opts,()) <- simpleOptions "ver" "header" "desc" (flag () () (long "some-flag")) empty doThings opts ``` Typical usage with commands: ``` haskell 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 ``` optparse-simple-0.1.1.2/ChangeLog.md0000644000000000000000000000073013457551030015337 0ustar0000000000000000# ChangeLog for optparse-simple ## 0.1.1.2 * Run TH slice at the right time to get proper Git info [#13](https://github.com/fpco/optparse-simple/issues/13) ## 0.1.1.1 * Add explicit signature to work around [#12](https://github.com/fpco/optparse-simple/issues/12) ## 0.1.1 * Switch dependency `gitrev` to `githash` ## 0.1.0 * Migrate from `EitherT` to `ExceptT` [#8](https://github.com/fpco/optparse-simple/issues/8) ## 0.0.4 * Support `--help` on subcommands