summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bittorrent.cabal6
-rw-r--r--examples/Client.hs15
-rw-r--r--examples/MkTorrent.hs55
3 files changed, 53 insertions, 23 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index a09d4530..600b2587 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -313,12 +313,16 @@ executable mktorrent
313 , lifted-async 313 , lifted-async
314 , parallel-io 314 , parallel-io
315 315
316 , network
317 , bittorrent 316 , bittorrent
318 317
319 , filepath 318 , filepath
320 , optparse-applicative 319 , optparse-applicative
321 , hslogger 320 , hslogger
321 if flag(network-uri)
322 Build-depends: network >= 2.6
323 , network-uri >= 2.6
324 else
325 Build-depends: network >= 2.4 && < 2.6
322 ghc-options: -Wall -O2 -threaded 326 ghc-options: -Wall -O2 -threaded
323 327
324-- nonfunctioning example of very basic bittorrent client 328-- nonfunctioning example of very basic bittorrent client
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 @@
1{-# LANGUAGE RankNTypes #-} 1{-# LANGUAGE RankNTypes #-}
2{-# LANGUAGE CPP #-}
2{-# LANGUAGE ExistentialQuantification #-} 3{-# LANGUAGE ExistentialQuantification #-}
3{-# LANGUAGE RecordWildCards #-} 4{-# LANGUAGE RecordWildCards #-}
4module Main (main) where 5module Main (main) where
5import Control.Concurrent 6import Control.Concurrent
6import Control.Monad.Trans 7import Control.Monad.Trans
8import Data.Maybe
7import Options.Applicative 9import Options.Applicative
8import System.Environment 10import System.Environment
9import System.Exit 11import System.Exit
@@ -12,6 +14,13 @@ import Text.Read
12 14
13import Network.BitTorrent 15import Network.BitTorrent
14 16
17#if MIN_VERSION_optparse_applicative(0,13,0)
18-- maybeReader imported from Options.Applicative.Builder
19#elif MIN_VERSION_optparse_applicative(0,11,0)
20maybeReader f = eitherReader (maybe (Left ":(") Right . f)
21#else
22maybeReader f = f
23#endif
15 24
16{----------------------------------------------------------------------- 25{-----------------------------------------------------------------------
17-- Command line arguments 26-- Command line arguments
@@ -29,17 +38,17 @@ argsParser = Args <$> (TorrentBox <$> infohashP <|> TorrentBox <$> torrentP)
29 <*> destDirP 38 <*> destDirP
30 where 39 where
31 infohashP :: Parser InfoHash 40 infohashP :: Parser InfoHash
32 infohashP = argument readMaybe 41 infohashP = argument (maybeReader readMaybe)
33 (metavar "SHA1" <> help "infohash of torrent file") 42 (metavar "SHA1" <> help "infohash of torrent file")
34 43
35 torrentP :: Parser FilePath 44 torrentP :: Parser FilePath
36 torrentP = argument Just 45 torrentP = argument (maybeReader Just)
37 ( metavar "FILE" 46 ( metavar "FILE"
38 <> help "A .torrent file" 47 <> help "A .torrent file"
39 ) 48 )
40 49
41 destDirP :: Parser FilePath 50 destDirP :: Parser FilePath
42 destDirP = argument Just 51 destDirP = argument (maybeReader Just)
43 ( metavar "DIR" 52 ( metavar "DIR"
44 <> help "Directory to put content" 53 <> help "Directory to put content"
45 ) 54 )
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 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE OverloadedStrings #-} 2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE RecordWildCards #-} 3{-# LANGUAGE RecordWildCards #-}
3{-# LANGUAGE StandaloneDeriving #-} 4{-# LANGUAGE StandaloneDeriving #-}
@@ -42,6 +43,14 @@ import Network.BitTorrent.Exchange.Message
42import Network.BitTorrent.Exchange.Session 43import Network.BitTorrent.Exchange.Session
43import System.Torrent.Storage 44import System.Torrent.Storage
44 45
46#if MIN_VERSION_optparse_applicative(0,13,0)
47-- maybeReader imported from Options.Applicative.Builder
48#elif MIN_VERSION_optparse_applicative(0,11,0)
49maybeReader f = eitherReader (maybe (Left ":(") Right . f)
50#else
51maybeReader f = f
52#endif
53
45 54
46{----------------------------------------------------------------------- 55{-----------------------------------------------------------------------
47-- Dialogs 56-- Dialogs
@@ -116,7 +125,7 @@ askChoice kvs = do
116-----------------------------------------------------------------------} 125-----------------------------------------------------------------------}
117 126
118torrentFile :: Parser FilePath 127torrentFile :: Parser FilePath
119torrentFile = argument Just 128torrentFile = argument (maybeReader Just)
120 ( metavar "TORRENT_FILE_PATH" 129 ( metavar "TORRENT_FILE_PATH"
121 <> help "A .torrent file" 130 <> help "A .torrent file"
122 ) 131 )
@@ -169,7 +178,7 @@ checkInfo = info (helper <*> parser) modifier
169 <> header "append +RTS -N$NUMBER_OF_CORES -RTS for parallel execution" 178 <> header "append +RTS -N$NUMBER_OF_CORES -RTS for parallel execution"
170 parser = CheckOpts 179 parser = CheckOpts
171 <$> torrentFile 180 <$> torrentFile
172 <*> argument Just 181 <*> argument (maybeReader Just)
173 ( metavar "CONTENT_DIR_PATH" 182 ( metavar "CONTENT_DIR_PATH"
174 <> value "." 183 <> value "."
175 <> help "Content directory or a single file" 184 <> help "Content directory or a single file"
@@ -247,11 +256,11 @@ createFlags = CreateFlags
247 256
248createOpts :: Parser CreateOpts 257createOpts :: Parser CreateOpts
249createOpts = CreateOpts 258createOpts = CreateOpts
250 <$> argument Just 259 <$> argument (maybeReader Just)
251 ( metavar "PATH" 260 ( metavar "PATH"
252 <> help "Content directory or a single file" 261 <> help "Content directory or a single file"
253 ) 262 )
254 <*> optional (argument Just 263 <*> optional (argument (maybeReader Just)
255 ( metavar "FILE" 264 ( metavar "FILE"
256 <> help "Place for the output .torrent file" 265 <> help "Place for the output .torrent file"
257 )) 266 ))
@@ -328,24 +337,26 @@ data GetOpts = GetOpts
328 , buckets :: Int 337 , buckets :: Int
329 } deriving Show 338 } deriving Show
330 339
340#if !MIN_VERSION_network(2,6,3)
331instance Read PortNumber where 341instance Read PortNumber where
332 readsPrec i s = [ (toEnum a, t) | (a, t) <- readsPrec i s] 342 readsPrec i s = [ (toEnum a, t) | (a, t) <- readsPrec i s]
343#endif
333 344
334paramsParser :: Parser GetOpts 345paramsParser :: Parser GetOpts
335paramsParser = GetOpts 346paramsParser = GetOpts
336 <$> argument readMaybe 347 <$> argument (maybeReader readMaybe)
337 (metavar "SHA1" <> help "infohash of torrent file") 348 (metavar "SHA1" <> help "infohash of torrent file")
338 <*> option (long "port" <> short 'p' 349 <*> option auto (long "port" <> short 'p'
339 <> value 7000 <> showDefault 350 <> value 7000 <> showDefault
340 <> metavar "NUM" <> help "port number to bind" 351 <> metavar "NUM" <> help "port number to bind"
341 ) 352 )
342 <*> option (long "boot" <> short 'b' 353 <*> option auto (long "boot" <> short 'b'
343 <> metavar "NODE" <> help "bootstrap node address" 354 <> metavar "NODE" <> help "bootstrap node address"
344 ) 355 )
345 <*> option (long "bucket" <> short 'n' 356 <*> option auto (long "bucket" <> short 'n'
346 <> value 2 <> showDefault 357 <> value 2 <> showDefault
347 <> metavar "NUM" <> help "number of buckets to maintain" 358 <> metavar "NUM" <> help "number of buckets to maintain"
348 ) 359 )
349 360
350getInfo :: ParserInfo GetOpts 361getInfo :: ParserInfo GetOpts
351getInfo = info (helper <*> paramsParser) 362getInfo = info (helper <*> paramsParser)
@@ -397,8 +408,10 @@ data GlobalOpts = GlobalOpts
397 { verbosity :: Priority 408 { verbosity :: Priority
398 } deriving Show 409 } deriving Show
399 410
411#if !MIN_VERSION_hslogger(1,2,9)
400deriving instance Enum Priority 412deriving instance Enum Priority
401deriving instance Bounded Priority 413deriving instance Bounded Priority
414#endif
402 415
403priorities :: [Priority] 416priorities :: [Priority]
404priorities = [minBound..maxBound] 417priorities = [minBound..maxBound]
@@ -409,7 +422,7 @@ defaultPriority = WARNING
409verbosityOpts :: Parser Priority 422verbosityOpts :: Parser Priority
410verbosityOpts = verbosityP <|> verboseP <|> quietP 423verbosityOpts = verbosityP <|> verboseP <|> quietP
411 where 424 where
412 verbosityP = option 425 verbosityP = option auto
413 ( long "verbosity" 426 ( long "verbosity"
414 <> metavar "LEVEL" 427 <> metavar "LEVEL"
415 <> help ("Set verbosity level\n" 428 <> help ("Set verbosity level\n"
@@ -441,13 +454,17 @@ options :: Parser Options
441options = Options <$> commandOpts <*> globalOpts 454options = Options <$> commandOpts <*> globalOpts
442 455
443versioner :: String -> Version -> Parser (a -> a) 456versioner :: String -> Version -> Parser (a -> a)
457#if MIN_VERSION_optparse_applicative(0,10,0)
458versioner prog ver = nullOption disabled $ mconcat
459#else
444versioner prog ver = nullOption $ mconcat 460versioner prog ver = nullOption $ mconcat
461#endif
445 [ long "version" 462 [ long "version"
446 , help "Show program version and exit" 463 , help "Show program version and exit"
447 , value id 464 , value id
448 , metavar "" 465 , metavar ""
449 , hidden 466 , hidden
450 , reader $ const $ undefined -- Left $ ErrorMsg versionStr 467 , mempty -- reader $ const $ undefined -- Left $ ErrorMsg versionStr
451 ] 468 ]
452 where 469 where
453 versionStr = prog ++ " version " ++ showVersion ver 470 versionStr = prog ++ " version " ++ showVersion ver