packages feed

flags-applicative 0.0.4.4 → 0.0.4.5

raw patch · 3 files changed

+29/−2 lines, 3 filesdep +networkPVP: minor bump suggested

API additions: PVP suggests at least a minor version bump

Dependencies added: network

API changes (from Hackage documentation)

+ Flags.Applicative: hostFlag :: Name -> Description -> FlagParser (HostName, Maybe PortNumber)

Files

flags-applicative.cabal view
@@ -1,5 +1,5 @@ name:                flags-applicative-version:             0.0.4.4+version:             0.0.4.5 synopsis:            Applicative flag parsing description:         https://github.com/mtth/flags-applicative homepage:            https://github.com/mtth/flags-applicative@@ -19,6 +19,7 @@   build-depends:       base >= 4.8 && < 5                      , containers >= 0.6 && < 0.7                      , mtl >= 2.2 && < 2.3+                     , network >= 2.8 && < 2.9                      , text >= 1.2 && < 1.3   default-language:    Haskell2010   ghc-options:         -Wall
src/Flags/Applicative.hs view
@@ -36,7 +36,7 @@   -- * Running parsers   , parseFlags, parseSystemFlagsOrDie   -- * Defining flags-  , switch, boolFlag, flag, textFlag, autoFlag, textListFlag, autoListFlag+  , switch, boolFlag, flag, textFlag, hostFlag, autoFlag, textListFlag, autoListFlag   ) where  import Control.Applicative ((<|>), Alternative, empty)@@ -53,6 +53,7 @@ import Data.Set (Set) import Data.Semigroup ((<>)) import Data.Text (Text)+import Network.Socket (HostName, PortNumber) import System.Exit (die) import System.Environment (getArgs) import qualified Data.Map.Strict as Map@@ -282,6 +283,14 @@ -- | Returns a parser for a single text value. textFlag :: Name -> Description -> FlagParser Text textFlag = flag Right++hostFlag :: Name -> Description -> FlagParser (HostName, Maybe PortNumber)+hostFlag = flag $ \txt -> do+  let (hostname, suffix) = T.breakOn ":" txt+  mbPort <- case T.stripPrefix ":" suffix of+      Nothing -> Right Nothing+      Just portStr -> Just <$> readEither (T.unpack portStr)+  pure (T.unpack hostname, mbPort)  -- | Returns a parser for any value with a 'Read' instance. Prefer 'textFlag' for textual values -- since 'flag'  will expect its values to be double-quoted and might not work as expected.
test/Spec.hs view
@@ -81,3 +81,20 @@         parser = boolFlag "foo" ""         res = parseFlags parser ["--foo", "--bar=1", "--swallowed_switches=bar"]       res `shouldBe` Left (UnexpectedFlagValue "bar")+    it "should parse a hostname" $ do+      let+        parser = hostFlag "host" ""+        res = parseFlags parser ["--host=foo.com"]+      res `shouldBe` Right (("foo.com", Nothing), [])+    it "should parse a hostname and a port" $ do+      let+        parser = hostFlag "host" ""+        res = parseFlags parser ["--host=localhost:1234"]+      res `shouldBe` Right (("localhost", Just 1234), [])+    it "should fail when given an invalid port" $ do+      let+        parser = hostFlag "host" ""+        res = parseFlags parser ["--host=localhost:1a2"]+      case res of+        Left (InvalidFlagValue "host" _ _) -> pure ()+        _ -> expectationFailure $ show res