argparser 0.3.2 → 0.3.3
raw patch · 7 files changed
+62/−25 lines, 7 files
Files
- argparser.cabal +9/−9
- changelog.md +2/−0
- src/System/Console/ArgParser.hs +6/−6
- src/System/Console/ArgParser/Format.hs +2/−2
- src/System/Console/ArgParser/Params.hs +2/−2
- src/System/Console/ArgParser/Run.hs +14/−2
- tests/System/Console/ArgParser/FormatTest.hs +27/−4
argparser.cabal view
@@ -1,5 +1,5 @@ name: argparser -version: 0.3.2 +version: 0.3.3 cabal-version: >=1.8 build-type: Simple author: Simon Bergot <simon.bergot@gmail.com> @@ -10,14 +10,15 @@ synopsis: Command line parsing framework for console applications description: Provides a combinator library for defining a command line parser. license-file: LICENSE +extra-source-files: changelog.md -library - build-depends: +library + build-depends: base >= 4.0 && < 5, containers hs-source-dirs: src ghc-options: -Wall - exposed-modules: + exposed-modules: System.Console.ArgParser, System.Console.ArgParser.ArgsProcess, System.Console.ArgParser.BaseType, @@ -30,26 +31,25 @@ source-repository head type: git - location: https://github.com/sbergot/ArgParser + location: https://github.com/sbergot/ArgParser test-suite TestsHTF type: exitcode-stdio-1.0 main-is: TestsHTF.hs ghc-options: -Wall -rtsopts - build-depends: + build-depends: base >= 4, HTF > 0.9, containers, HUnit, argparser - other-modules: + other-modules: System.Console.ArgParser.ArgsProcessTest, System.Console.ArgParser.FormatTest, System.Console.ArgParser.ParserTest, System.Console.ArgParser.QuickParamsTest, System.Console.ArgParser.SubParserTest, System.Console.ArgParser.TestHelpers - hs-source-dirs: + hs-source-dirs: tests, src -
+ changelog.md view
@@ -0,0 +1,2 @@+- add withParseResult +- fix version display bug
src/System/Console/ArgParser.hs view
@@ -12,22 +12,20 @@ consume and convert command line arguments. Default special action such as help/usage are automatically built from the parser specification. -Here is a quick example. +Here is a quick example. @ data MyTest = -- First, we need a datatype MyTest Int Int deriving (Show) -- we will print the values -myTestParser -- Then, we define a parser +myTestParser -- Then, we define a parser :: ParserSpec MyTest myTestParser = MyTest \`parsedBy\` reqPos \"pos1\" \`andBy\` optPos 0 \"pos2\" -main = do -- We proceed to build an interface and run it: - interface <- mkApp myTestParser - runApp interface print +main = withParseResult myTestParser print @ Building this app will produce an executable `foo` which will behave like this: @@ -68,6 +66,7 @@ -- $subparser , mkSubParser -- * Running a parser + , withParseResult , runApp , parseArgs -- * Creating parameters @@ -97,7 +96,8 @@ import System.Console.ArgParser.Run (mkApp, mkDefaultApp, parseArgs, runApp, setAppDescr, - setAppEpilog) + setAppEpilog, + withParseResult) import System.Console.ArgParser.SubParser (mkSubParser)
src/System/Console/ArgParser/Format.hs view
@@ -42,7 +42,7 @@ showCmdLineVersion :: CmdLnInterface a -> String showCmdLineVersion app = appName ++ appVersion where appName = getAppName app - appVersion = fromMaybe "" $ getAppVersion app + appVersion = maybe "" (" " ++) $ getAppVersion app -- | Prints a long usage such as -- @@ -112,4 +112,4 @@ newWidth = currWidth + 1 + length word in if newWidth > width then Just (accum, rest) - else loop newWidth (accum ++ ' ':word) _words+ else loop newWidth (accum ++ ' ':word) _words
src/System/Console/ArgParser/Params.hs view
@@ -39,7 +39,7 @@ -- | Specify the format of a flag data FlagFormat = - -- | Possible short format ie @-f@ or @--foo@ + -- | Possible short format ie @-f@ or @--foo@ Short | -- | Only long format ie @--foo@ Long @@ -62,7 +62,7 @@ takeLongFlag key flags = (args, rest) where args = M.lookup key flags rest = M.delete key flags - + takeValidFlag :: FlagFormat -> FlagParser takeValidFlag fmt = case fmt of Short -> takeFlag
src/System/Console/ArgParser/Run.hs view
@@ -12,7 +12,8 @@ module System.Console.ArgParser.Run ( -- * Running a parser - runApp + withParseResult + , runApp , parseArgs , parseNiceArgs -- * Building a parser @@ -47,6 +48,17 @@ args <- getArgs either putStrLn appfun $ parseArgs args appspec +-- | Runs an apllication with the user provided arguments. +-- It is a shorter way of calling `mkApp` and `runApp` +withParseResult + :: ParserSpec a + -> (a -> IO ()) + -> IO () +withParseResult parser app = do + interface <- mkApp parser + runApp interface app + + -- | Parse the arguments with the parser -- provided to the function. parseArgs @@ -117,4 +129,4 @@ -- | Set the name of an interface setAppName :: CmdLnInterface a -> String -> CmdLnInterface a -setAppName app descr = app {getAppName = descr }+setAppName app descr = app {getAppName = descr }
tests/System/Console/ArgParser/FormatTest.hs view
@@ -18,12 +18,12 @@ single xs = case xs of [x] -> x _ -> error "single on non-single list" - + paramDescr :: ParamSpec spec => spec a -> ParamDescr -paramDescr = single . getParamDescr +paramDescr = single . getParamDescr showUsage :: ParamSpec spec @@ -113,6 +113,29 @@ ]) $ showCmdLineAppUsage defaultFormat myTestParser +myTestVersionParser :: CmdLnInterface MyTest +myTestVersionParser = (mkDefaultApp (MyTest + `parsedBy` reqPos "foo" + `andBy` reqPos "bar") + "test") { getAppVersion = Just "version" } + +test_versionFormat :: H.Assertion +test_versionFormat = assertEqual + ( unlines + [ "test version" + , "usage : test foo bar [-h] [--version]" + , "" + , "mandatory arguments:" + , " foo" + , " bar" + , "" + , "optional arguments:" + , " -h, --help show this help message and exit" + , " --version print the program version and exit" + , "" + ]) + $ showCmdLineAppUsage defaultFormat myTestVersionParser + data MyDescrTest = MyDescrTest Int Int Int Int deriving (Eq, Show) @@ -166,7 +189,7 @@ "A" `setAppDescr` "A sub description") , ("B", mkDefaultApp (MyCons2 `parsedBy` reqPos "pos1") - "B" `setAppDescr` "B sub description") + "B" `setAppDescr` "B sub description") ] test_subparserFormat :: H.Assertion @@ -185,4 +208,4 @@ , " --version print the program version and exit" , "" ]) - $ showCmdLineAppUsage defaultFormat mySubTestParser+ $ showCmdLineAppUsage defaultFormat mySubTestParser