packages feed

argparser 0.3.3 → 0.3.4

raw patch · 7 files changed

+20/−15 lines, 7 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ System.Console.ArgParser.BaseType: emptyArgs :: NiceArgs

Files

argparser.cabal view
@@ -1,5 +1,5 @@ name:           argparser
-version:        0.3.3
+version:        0.3.4
 cabal-version:  >=1.8
 build-type:     Simple
 author:         Simon Bergot <simon.bergot@gmail.com>
@@ -10,7 +10,6 @@ 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:
− changelog.md
@@ -1,2 +0,0 @@-- add withParseResult
-- fix version display bug
src/System/Console/ArgParser/ArgsProcess.hs view
@@ -36,7 +36,7 @@ getWord (Token _ word) = word
 
 tokenize :: Args -> [Token]
-tokenize = concatMap arg2token where
+tokenize = (concatMap arg2token) . (takeWhile (/= "--")) where
   arg2token :: Arg -> [Token]
   arg2token arg = case arg of
     '-':'-':word -> [Token Flag word]
src/System/Console/ArgParser/BaseType.hs view
@@ -13,7 +13,7 @@ module System.Console.ArgParser.BaseType where
 
 import           Control.Applicative     ((<*>), Applicative, pure)
-import qualified Data.Map as M (Map)
+import qualified Data.Map as M (Map, empty)
 
 -- | Simple command line arg
 type Arg   = String
@@ -37,6 +37,9 @@   , argDescr    :: String -- ^ Description of the parameter
   , argMetaVar  :: String -- ^ Description of the parameter in the usage
   }
+
+emptyArgs :: NiceArgs
+emptyArgs = ([], M.empty)
 
 -- | Returns a short description of the input format
 --   of a parameter.
src/System/Console/ArgParser/Params.hs view
@@ -98,12 +98,12 @@ 
 instance ParamSpec FlagParam where
   getParser (FlagParam fmt key parse) = Parser rawparse where
-    rawparse (pos, flags) = case args of
-      Just [] -> (Right $ parse True, (pos, rest))
-      Just _  -> (Left "unexpected parameter(s)", (pos, rest))
-      Nothing -> (Right $ parse False, (pos, rest))
+    rawparse (pos, flags) = case margs of
+      Just []   -> (Right $ parse True,  (pos, rest))
+      Just args' -> (Right $ parse True,  (pos ++ args', rest))
+      Nothing   -> (Right $ parse False, (pos, rest))
      where
-      (args, rest) = takeValidFlag fmt key flags
+      (margs, rest) = takeValidFlag fmt key flags
   getParamDescr (FlagParam fmt key _) = [ParamDescr
     (const $ "[" ++ shortestFlagFmt fmt key ++ "]")
     "optional arguments"
src/System/Console/ArgParser/Run.hs view
@@ -34,8 +34,10 @@ import           System.Console.ArgParser.Parser
 import           System.Environment
 
-runParser :: Parser a -> NiceArgs -> ParseResult a
-runParser (Parser parse) args = fst $ parse args
+runParser :: Parser a -> NiceArgs -> (Bool, ParseResult a)
+runParser (Parser parse) args = let
+  (res, rest) = parse args
+  in (rest == emptyArgs, res)
 
 -- | Runs a command line application with the
 --   user provided arguments. If the parsing succeeds,
@@ -78,7 +80,9 @@ parseNiceArgs niceargs appspec = fromMaybe normalprocess specialprocess
  where
   parser = getParserFun $ cmdArgParser appspec
-  normalprocess = runParser parser niceargs
+  normalprocess = case runParser parser niceargs of
+    (True, res) -> res
+    (False, _)  -> Left "too many arguments"
   specialprocess = runSpecialFlags appspec niceargs
 
 runSpecialFlags :: CmdLnInterface a -> NiceArgs -> Maybe (ParseResult a)
@@ -87,7 +91,7 @@     []                   -> Nothing
     (parse, action):rest -> runSpecialAction parse action rest
   runSpecialAction parse action other = case specialParseResult of
-    Right True -> Just $ action app args
+    (_, Right True) -> Just $ action app args
     _          -> loop other
    where
     specialParseResult = runParser (getParserFun parse) args
tests/TestsHTF.hs view
@@ -9,6 +9,7 @@ import {-@ HTF_TESTS @-} System.Console.ArgParser.QuickParamsTest
 import {-@ HTF_TESTS @-} System.Console.ArgParser.ParserTest
 import {-@ HTF_TESTS @-} System.Console.ArgParser.ArgsProcessTest
+import {-@ HTF_TESTS @-} System.Console.ArgParser.FullTest
 
 main :: IO()
 main = htfMain htf_importedTests