simple-cmd-args (empty) → 0.1.0
raw patch · 6 files changed
+196/−0 lines, 6 filesdep +basedep +optparse-applicativedep +semigroupssetup-changed
Dependencies added: base, optparse-applicative, semigroups
Files
- CHANGELOG.md +9/−0
- LICENSE +21/−0
- README.md +29/−0
- Setup.hs +2/−0
- SimpleCmdArgs.hs +96/−0
- simple-cmd-args.cabal +39/−0
+ CHANGELOG.md view
@@ -0,0 +1,9 @@+# Changelog++`simple-cmd-args` uses [PVP Versioning][1].++0.1.0+=====+* Initial release with subcommands and option Mod functions++[1]: https://pvp.haskell.org
+ LICENSE view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2019 Jens Petersen++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ README.md view
@@ -0,0 +1,29 @@+# simple-cmd-args++[](https://hackage.haskell.org/package/simple-cmd-args)+[](LICENSE)+[](http://stackage.org/lts/package/simple-cmd-args)+[](http://stackage.org/nightly/package/simple-cmd-args)+[](https://travis-ci.org/juhp/simple-cmd-args)++A thin layer over optparse-applicative that avoids type plumbing for+the common use case of a commandline tool with subcommands,+by using `Parser (IO ())`++## Usage++```haskell+import SimpleCmdArgs+import Control.Applicative (some)+import SimpleCmd (cmd_)++main =+ simpleCmdArgs Nothing "my example tool" "Longer description..." $+ subcommands+ [ Subcommand "echo" (putStrLn <$> strArg "NAME") "Print name"+ , Subcommand "ls" (cmd_ "ls" <$> some (strArg "FILE...")) "Touch FILE"+ ]+```++See more [https://github.com/juhp/simple-cmd-args/tree/master/examples](examples/).+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ SimpleCmdArgs.hs view
@@ -0,0 +1,96 @@+module SimpleCmdArgs+ (optionMods,+ optionalMods,+ simpleCmdArgs,+ simpleCmdArgs',+ strArg,+ Subcommand(..),+ subcommands,+ switchMods+ )+where++import Control.Monad (join)+#if (defined(MIN_VERSION_base) && MIN_VERSION_base(4,8,0))+#else+import Data.Monoid (mconcat)+#endif+import Data.Semigroup ((<>))+import Data.Version+import Options.Applicative++-- | Parser executor (allows interspersed args and options)+simpleCmdArgs ::+ Maybe Version+ -- ^ version string+ -> String+ -- ^ header+ -> String+ -- ^ program description+ -> Parser (IO ())+ -- ^ commands+ -> IO ()+simpleCmdArgs mversion h pd =+ simpleCmdArgsWithMods mods mversion+ where+ mods = fullDesc <> header h <> progDesc pd++-- | Parser executor without interspersing options and args+simpleCmdArgs'+ :: Maybe Version+ -- ^ version string+ -> String+ -- ^ header+ -> String+ -- ^ program description+ -> Parser (IO ())+ -- ^ commands+ -> IO ()+simpleCmdArgs' mversion h pd =+ simpleCmdArgsWithMods mods mversion+ where+ mods = fullDesc <> header h <> progDesc pd <> noIntersperse++-- | Generic parser executor with explicit info modifiers+simpleCmdArgsWithMods ::+ InfoMod (IO ()) ->+ -- ^ modifiers+ Maybe Version+ -- ^ version string+ -> Parser (IO ())+ -- ^ commands+ -> IO ()+simpleCmdArgsWithMods mods mversion cmdsParser = join $+ customExecParser (prefs showHelpOnEmpty)+ (case mversion of+ (Just version) -> info (helper <*> versionOption version <*> cmdsParser) mods+ Nothing -> info (helper <*> cmdsParser) mods)+ where + versionOption ver =+ infoOption (showVersion ver) (long "version" <> help "Show version")++data Subcommand = Subcommand String String (Parser (IO ()))++subcommands :: [Subcommand] -> Parser (IO ())+subcommands = subparser . mconcat . map cmdToParse+ where+ cmdToParse (Subcommand name cmddesc cmdparse) =+ command name (info cmdparse (progDesc cmddesc))++strArg :: String -> Parser String+strArg var = strArgument (metavar var)++switchMods :: HasName f =>+ Char -> String -> String -> Mod f a+switchMods s l h =+ short s <> long l <> help h++optionMods :: (HasMetavar f, HasName f) =>+ Char -> String -> String -> String -> Mod f a+optionMods s l meta h =+ short s <> long l <> metavar meta <> help h++optionalMods :: (HasMetavar f, HasName f, HasValue f) =>+ Char -> String -> String -> String -> a -> Mod f a+optionalMods s l meta h d =+ short s <> long l <> metavar meta <> help h <> value d
+ simple-cmd-args.cabal view
@@ -0,0 +1,39 @@+cabal-version: 1.18+name: simple-cmd-args+version: 0.1.0+synopsis: Simple command args parsing and execution+description:+ This a small wrapper over optparse-applicative which+ allows combining args parsers directly with IO commands,+ avoiding command types boilerplate. It also provides+ a few option Mod functions.+homepage: https://github.com/juhp/simple-cmd-args+bug-reports: https://github.com/juhp/simple-cmd-args/issues+license: BSD3+license-file: LICENSE+author: Jens Petersen+maintainer: juhpetersen@gmail.com+copyright: 2019 Jens Petersen+category: System+build-type: Simple+extra-doc-files: README.md+ , CHANGELOG.md+tested-with: GHC == 7.10.3, GHC == 8.0.2, GHC == 8.2.2,+ GHC == 8.4.4, GHC == 8.6.3++source-repository head+ type: git+ location: https://github.com/juhp/simple-cmd-args.git++library+ exposed-modules: SimpleCmdArgs++ build-depends: base >= 4 && < 5+ , optparse-applicative+ if impl(ghc<8.0)+ build-depends: semigroups++ ghc-options: -Wall -fwarn-missing-signatures++ default-language: Haskell2010+ default-extensions: CPP