From b0cb559c503e791c2a2297a6316d59dab6c49202 Mon Sep 17 00:00:00 2001 From: joe Date: Sat, 31 Dec 2016 23:43:09 -0500 Subject: Fix build of examples. --- examples/Client.hs | 15 +++++++++++--- examples/MkTorrent.hs | 55 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 48 insertions(+), 22 deletions(-) (limited to 'examples') diff --git a/examples/Client.hs b/examples/Client.hs index abf62657..26711676 100644 --- a/examples/Client.hs +++ b/examples/Client.hs @@ -1,9 +1,11 @@ {-# LANGUAGE RankNTypes #-} +{-# LANGUAGE CPP #-} {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE RecordWildCards #-} module Main (main) where import Control.Concurrent import Control.Monad.Trans +import Data.Maybe import Options.Applicative import System.Environment import System.Exit @@ -12,6 +14,13 @@ import Text.Read import Network.BitTorrent +#if MIN_VERSION_optparse_applicative(0,13,0) +-- maybeReader imported from Options.Applicative.Builder +#elif MIN_VERSION_optparse_applicative(0,11,0) +maybeReader f = eitherReader (maybe (Left ":(") Right . f) +#else +maybeReader f = f +#endif {----------------------------------------------------------------------- -- Command line arguments @@ -29,17 +38,17 @@ argsParser = Args <$> (TorrentBox <$> infohashP <|> TorrentBox <$> torrentP) <*> destDirP where infohashP :: Parser InfoHash - infohashP = argument readMaybe + infohashP = argument (maybeReader readMaybe) (metavar "SHA1" <> help "infohash of torrent file") torrentP :: Parser FilePath - torrentP = argument Just + torrentP = argument (maybeReader Just) ( metavar "FILE" <> help "A .torrent file" ) destDirP :: Parser FilePath - destDirP = argument Just + destDirP = argument (maybeReader Just) ( metavar "DIR" <> help "Directory to put content" ) diff --git a/examples/MkTorrent.hs b/examples/MkTorrent.hs index 871343f9..1df4b42f 100644 --- a/examples/MkTorrent.hs +++ b/examples/MkTorrent.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE StandaloneDeriving #-} @@ -42,6 +43,14 @@ import Network.BitTorrent.Exchange.Message import Network.BitTorrent.Exchange.Session import System.Torrent.Storage +#if MIN_VERSION_optparse_applicative(0,13,0) +-- maybeReader imported from Options.Applicative.Builder +#elif MIN_VERSION_optparse_applicative(0,11,0) +maybeReader f = eitherReader (maybe (Left ":(") Right . f) +#else +maybeReader f = f +#endif + {----------------------------------------------------------------------- -- Dialogs @@ -116,7 +125,7 @@ askChoice kvs = do -----------------------------------------------------------------------} torrentFile :: Parser FilePath -torrentFile = argument Just +torrentFile = argument (maybeReader Just) ( metavar "TORRENT_FILE_PATH" <> help "A .torrent file" ) @@ -169,7 +178,7 @@ checkInfo = info (helper <*> parser) modifier <> header "append +RTS -N$NUMBER_OF_CORES -RTS for parallel execution" parser = CheckOpts <$> torrentFile - <*> argument Just + <*> argument (maybeReader Just) ( metavar "CONTENT_DIR_PATH" <> value "." <> help "Content directory or a single file" @@ -247,11 +256,11 @@ createFlags = CreateFlags createOpts :: Parser CreateOpts createOpts = CreateOpts - <$> argument Just + <$> argument (maybeReader Just) ( metavar "PATH" <> help "Content directory or a single file" ) - <*> optional (argument Just + <*> optional (argument (maybeReader Just) ( metavar "FILE" <> help "Place for the output .torrent file" )) @@ -328,24 +337,26 @@ data GetOpts = GetOpts , buckets :: Int } deriving Show +#if !MIN_VERSION_network(2,6,3) instance Read PortNumber where readsPrec i s = [ (toEnum a, t) | (a, t) <- readsPrec i s] +#endif paramsParser :: Parser GetOpts paramsParser = GetOpts - <$> argument readMaybe - (metavar "SHA1" <> help "infohash of torrent file") - <*> option (long "port" <> short 'p' - <> value 7000 <> showDefault - <> metavar "NUM" <> help "port number to bind" - ) - <*> option (long "boot" <> short 'b' - <> metavar "NODE" <> help "bootstrap node address" - ) - <*> option (long "bucket" <> short 'n' - <> value 2 <> showDefault - <> metavar "NUM" <> help "number of buckets to maintain" - ) + <$> argument (maybeReader readMaybe) + (metavar "SHA1" <> help "infohash of torrent file") + <*> option auto (long "port" <> short 'p' + <> value 7000 <> showDefault + <> metavar "NUM" <> help "port number to bind" + ) + <*> option auto (long "boot" <> short 'b' + <> metavar "NODE" <> help "bootstrap node address" + ) + <*> option auto (long "bucket" <> short 'n' + <> value 2 <> showDefault + <> metavar "NUM" <> help "number of buckets to maintain" + ) getInfo :: ParserInfo GetOpts getInfo = info (helper <*> paramsParser) @@ -397,8 +408,10 @@ data GlobalOpts = GlobalOpts { verbosity :: Priority } deriving Show +#if !MIN_VERSION_hslogger(1,2,9) deriving instance Enum Priority deriving instance Bounded Priority +#endif priorities :: [Priority] priorities = [minBound..maxBound] @@ -409,7 +422,7 @@ defaultPriority = WARNING verbosityOpts :: Parser Priority verbosityOpts = verbosityP <|> verboseP <|> quietP where - verbosityP = option + verbosityP = option auto ( long "verbosity" <> metavar "LEVEL" <> help ("Set verbosity level\n" @@ -441,13 +454,17 @@ options :: Parser Options options = Options <$> commandOpts <*> globalOpts versioner :: String -> Version -> Parser (a -> a) +#if MIN_VERSION_optparse_applicative(0,10,0) +versioner prog ver = nullOption disabled $ mconcat +#else versioner prog ver = nullOption $ mconcat +#endif [ long "version" , help "Show program version and exit" , value id , metavar "" , hidden - , reader $ const $ undefined -- Left $ ErrorMsg versionStr + , mempty -- reader $ const $ undefined -- Left $ ErrorMsg versionStr ] where versionStr = prog ++ " version " ++ showVersion ver -- cgit v1.2.3