11{-# LANGUAGE DuplicateRecordFields #-}
22{-# LANGUAGE LambdaCase #-}
3- {-# LANGUAGE MultiWayIf #-}
43{-# LANGUAGE NamedFieldPuns #-}
54{-# LANGUAGE RecordWildCards #-}
65{-# LANGUAGE TypeApplications #-}
@@ -12,6 +11,7 @@ import Control.Exception (Exception (displayException))
1211import Control.Monad
1312import Data.Aeson (eitherDecodeFileStrict' )
1413import Data.Default (Default (.. ))
14+ import Data.List (find )
1515import Data.Maybe (fromMaybe )
1616import qualified ExamplesRelay
1717import qualified ExamplesRelayP2P
@@ -23,6 +23,8 @@ import qualified LeiosProtocol.Short.VizSimP2P as VizShortLeiosP2P
2323import qualified LeiosProtocol.VizSimTestRelay as VizSimTestRelay
2424import Options.Applicative (
2525 Alternative ((<|>) ),
26+ HasValue ,
27+ Mod ,
2628 Parser ,
2729 ParserInfo ,
2830 ParserPrefs ,
@@ -44,6 +46,8 @@ import Options.Applicative (
4446 progDesc ,
4547 readerError ,
4648 short ,
49+ showDefault ,
50+ showDefaultWith ,
4751 showHelpOnEmpty ,
4852 str ,
4953 strOption ,
@@ -73,6 +77,9 @@ main = do
7377 SimCommand opt -> runSimOptions opt
7478 CliCommand opt -> runCliOptions opt
7579
80+ shownDefValue :: (Show a , HasValue f ) => a -> Mod f a
81+ shownDefValue a = value a <> showDefault
82+
7683parserPrefs :: ParserPrefs
7784parserPrefs =
7885 prefs . mconcat $
@@ -252,14 +259,17 @@ parserPraosP2P1 =
252259 ( long " seed"
253260 <> metavar " NUMBER"
254261 <> help " The seed for the random number generator."
255- <> value 0
262+ <> shownDefValue 0
256263 )
257- <*> option
258- (fmap (fromIntegral @ Int ) auto)
259- ( long " block-interval"
260- <> metavar " NUMBER"
261- <> help " The interval at which blocks are generated."
262- <> value 5
264+ <*> fmap
265+ (fromIntegral @ Int )
266+ ( option
267+ auto
268+ ( long " block-interval"
269+ <> metavar " NUMBER"
270+ <> help " The interval at which blocks are generated."
271+ <> shownDefValue 5
272+ )
263273 )
264274 <*> parserTopographyOptions
265275
@@ -271,33 +281,36 @@ parserShortLeiosP2P1 =
271281 ( long " seed"
272282 <> metavar " NUMBER"
273283 <> help " The seed for the random number generator."
274- <> value 0
284+ <> shownDefValue 0
275285 )
276286 <*> option
277287 (fmap (fromIntegral @ Int ) auto)
278288 ( long " slice-length"
279289 <> metavar " NUMBER"
280290 <> help " The interval at which ranking blocks are generated."
281- <> value 5
291+ <> shownDefValue 5
282292 )
283293 <*> parserTopographyOptions
284294 <*> option
285295 readCores
286296 ( short ' N'
287297 <> metavar " NUMBER"
288298 <> value Infinite
289- <> help " number of simulated cores for node parallesim, or 'unbounded' (the default)."
299+ <> showDefaultWith showCores
300+ <> help " number of simulated cores for node parallesim, or 'unbounded'."
290301 )
291302 where
303+ unbounded_str = " unbounded"
292304 readCores = unbounded <|> finite
293305 where
294306 unbounded = do
295307 s <- str
296- if s == " unbounded " then pure Infinite else readerError " unrecognized"
308+ if s == unbounded_str then pure Infinite else readerError " unrecognized"
297309 finite = do
298310 n <- auto
299311 if n > 0 then pure (Finite n) else readerError " number of cores should be greater than 0"
300-
312+ showCores Infinite = unbounded_str
313+ showCores (Finite n) = show n
301314vizOptionsToViz :: VizCommand -> IO Visualization
302315vizOptionsToViz VizCommandWithOptions {.. } = case vizSubCommand of
303316 VizTCP1 -> pure ExamplesTCP. example1
@@ -395,12 +408,15 @@ parserSimOptions :: Parser SimOptions
395408parserSimOptions =
396409 SimOptions
397410 <$> parserSimCommand
398- <*> option
399- (Time . fromIntegral @ Int <$> auto)
400- ( long " output-seconds"
401- <> metavar " SECONDS"
402- <> help " Output N seconds of simulation."
403- <> value (Time $ fromIntegral @ Int 40 )
411+ <*> fmap
412+ (Time . fromIntegral @ Int )
413+ ( option
414+ auto
415+ ( long " output-seconds"
416+ <> metavar " SECONDS"
417+ <> help " Output N seconds of simulation."
418+ <> shownDefValue 40
419+ )
404420 )
405421 <*> strOption
406422 ( long " output-file"
@@ -417,11 +433,11 @@ parserSimCommand :: Parser SimCommand
417433parserSimCommand =
418434 subparser . mconcat $
419435 [ commandGroup " Available simulations:"
420- , command " praos-diffusion-10" . info parserSimPraosDiffusion10 $
436+ , command " praos-diffusion-10" . info ( parserSimPraosDiffusion10 <**> helper) $
421437 progDesc " "
422- , command " praos-diffusion-20" . info parserSimPraosDiffusion20 $
438+ , command " praos-diffusion-20" . info ( parserSimPraosDiffusion20 <**> helper) $
423439 progDesc " "
424- , command " short-leios" . info parserShortLeios $
440+ , command " short-leios" . info ( parserShortLeios <**> helper) $
425441 progDesc " "
426442 ]
427443
@@ -433,14 +449,14 @@ parserSimPraosDiffusion10 =
433449 ( long " num-close-links"
434450 <> metavar " NUMBER"
435451 <> help " The number of close-distance links."
436- <> value 5
452+ <> shownDefValue 5
437453 )
438454 <*> option
439455 auto
440456 ( long " num-random-links"
441457 <> metavar " NUMBER"
442458 <> help " The number of random links."
443- <> value 5
459+ <> shownDefValue 5
444460 )
445461
446462parserSimPraosDiffusion20 :: Parser SimCommand
@@ -451,14 +467,14 @@ parserSimPraosDiffusion20 =
451467 ( long " num-close-links"
452468 <> metavar " NUMBER"
453469 <> help " The number of close-distance links."
454- <> value 10
470+ <> shownDefValue 10
455471 )
456472 <*> option
457473 auto
458474 ( long " num-random-links"
459475 <> metavar " NUMBER"
460476 <> help " The number of random links."
461- <> value 10
477+ <> shownDefValue 10
462478 )
463479
464480parserShortLeios :: Parser SimCommand
@@ -593,21 +609,21 @@ parserTopographyCharacteristics =
593609 ( long " tc-num-nodes"
594610 <> metavar " NUMBER"
595611 <> help " The number of nodes."
596- <> value (p2pNumNodes def)
612+ <> shownDefValue (p2pNumNodes def)
597613 )
598614 <*> option
599615 auto
600616 ( long " tc-num-links-close"
601617 <> metavar " NUMBER"
602618 <> help " The number of links to close peers for each node."
603- <> value (p2pNodeLinksClose def)
619+ <> shownDefValue (p2pNodeLinksClose def)
604620 )
605621 <*> option
606622 auto
607623 ( long " tc-num-links-random"
608624 <> metavar " NUMBER"
609625 <> help " The number of links to random peers for each node."
610- <> value (p2pNodeLinksRandom def)
626+ <> shownDefValue (p2pNodeLinksRandom def)
611627 )
612628
613629parserWorld :: Parser World
@@ -624,14 +640,22 @@ parserWorldShape =
624640 <> metavar " SHAPE"
625641 <> help " The shape of the generated world. Supported shapes are rectangle and cylinder."
626642 <> value def
643+ <> showDefaultWith showWorldShape
627644 )
628645
629646readWorldShape :: ReadM WorldShape
630647readWorldShape = eitherReader $ \ txt ->
631- if
632- | txt == " rectangle" -> Right Rectangle
633- | txt == " cylinder" -> Right Cylinder
634- | otherwise -> Left (" Could not parse WorldShape '" <> txt <> " '" )
648+ case lookup txt worldShapeLabels of
649+ Just s -> Right s
650+ Nothing -> Left (" Could not parse WorldShape '" <> txt <> " '" )
651+
652+ showWorldShape :: WorldShape -> String
653+ showWorldShape s = case find ((== s) . snd ) worldShapeLabels of
654+ Just (txt, _) -> txt
655+ Nothing -> " Error, Unknown worldshape: " ++ show s
656+
657+ worldShapeLabels :: [(String , WorldShape )]
658+ worldShapeLabels = [(" rectangle" , Rectangle ), (" cylinder" , Cylinder )]
635659
636660parserWorldDimensions :: Parser WorldDimensions
637661parserWorldDimensions =
@@ -641,12 +665,12 @@ parserWorldDimensions =
641665 ( long " tc-world-width"
642666 <> metavar " SECONDS"
643667 <> help " The east-west size of the generated world."
644- <> value (fst $ worldDimensions def)
668+ <> shownDefValue (fst $ worldDimensions def)
645669 )
646670 <*> option
647671 auto
648672 ( long " tc-world-height"
649673 <> metavar " SECONDS"
650674 <> help " The north-south length of the generated world."
651- <> value (snd $ worldDimensions def)
675+ <> shownDefValue (snd $ worldDimensions def)
652676 )
0 commit comments