optparse-simple-0.0.4/src/0000755000000000000000000000000013175625251013623 5ustar0000000000000000optparse-simple-0.0.4/src/Options/0000755000000000000000000000000013175625251015256 5ustar0000000000000000optparse-simple-0.0.4/src/Options/Applicative/0000755000000000000000000000000013175626152017520 5ustar0000000000000000optparse-simple-0.0.4/test/0000755000000000000000000000000013176047744014021 5ustar0000000000000000optparse-simple-0.0.4/src/Options/Applicative/Simple.hs0000644000000000000000000001271713175626152021315 0ustar0000000000000000{-# 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 import System.Environment -- | Generate and execute 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 = 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) ] ++ if $gitHash == ("UNKNOWN" :: String) then [] else [", 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 <$> (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 -> EitherT b (Writer (Mod CommandFields b)) () -- ^ sub-commands (use 'addCommand') -> EitherT 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 -> EitherT 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 (runEitherT commandParser) of (Right (),d) -> subparser d (Left b,_) -> pure b optparse-simple-0.0.4/test/Main.hs0000644000000000000000000000623713176047744015251 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.0.4/LICENSE0000644000000000000000000000273313175625251014046 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.0.4/Setup.hs0000644000000000000000000000005613175625251014471 0ustar0000000000000000import Distribution.Simple main = defaultMain optparse-simple-0.0.4/optparse-simple.cabal0000644000000000000000000000227413176047744017157 0ustar0000000000000000name: optparse-simple version: 0.0.4 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 extra-source-files: README.md ChangeLog.md library hs-source-dirs: src/ ghc-options: -Wall exposed-modules: Options.Applicative.Simple build-depends: base >= 4 && <5 , template-haskell , transformers , optparse-applicative , gitrev , either test-suite test type: exitcode-stdio-1.0 main-is: Main.hs build-depends: base -any , optparse-simple , directory -any , bytestring -any hs-source-dirs: test ghc-options: -Wall source-repository head type: git location: https://github.com/fpco/optparse-simple optparse-simple-0.0.4/README.md0000644000000000000000000000141213175625251014311 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.0.4/ChangeLog.md0000644000000000000000000000005413175626105015203 0ustar0000000000000000## 0.0.4 * Support `--help` on subcommands