never executed always true always false
1 {-# LANGUAGE DeriveGeneric #-}
2 {-# LANGUAGE FlexibleContexts #-}
3
4 {-# OPTIONS_HADDOCK hide #-}
5
6 module Options.Applicative.MnemonicSize
7 (
8 -- * Type
9 MnemonicSize(..)
10 , mnemonicSizeToString
11 , mnemonicSizeFromString
12
13 -- * Applicative Parser
14 , mnemonicSizeOpt
15 ) where
16
17 import Prelude
18
19 import Data.List
20 ( intercalate )
21 import Data.List.Extra
22 ( enumerate )
23 import GHC.Generics
24 ( Generic )
25 import Options.Applicative
26 ( Parser
27 , completer
28 , eitherReader
29 , help
30 , listCompleter
31 , long
32 , metavar
33 , option
34 , showDefaultWith
35 , value
36 )
37
38
39 --
40 -- Type
41 --
42
43 data MnemonicSize
44 = MS_9 | MS_12 | MS_15 | MS_18 | MS_21 | MS_24
45 deriving (Generic, Show, Bounded, Enum, Eq)
46
47 mnemonicSizeToString :: MnemonicSize -> String
48 mnemonicSizeToString = drop 3 . show
49
50 mnemonicSizeFromString :: String -> Either String MnemonicSize
51 mnemonicSizeFromString str =
52 case lookup str sizeMap of
53 Just ms -> Right ms
54 Nothing -> Left $ mempty
55 <> "Invalid mnemonic size. Expected one of: "
56 <> intercalate ", " sizeStrs
57 <> "."
58 where
59 sizeMap = sizeStrs `zip` enumerate
60
61 sizeStrs :: [String]
62 sizeStrs = mnemonicSizeToString <$> enumerate
63
64 --
65 -- Applicative Parser
66 --
67
68 mnemonicSizeOpt :: Parser MnemonicSize
69 mnemonicSizeOpt = option (eitherReader mnemonicSizeFromString) $ mempty
70 <> long "size"
71 <> metavar "INT"
72 <> help "Number of mnemonic words to generate. Must be a multiple of 3."
73 <> value MS_24
74 <> showDefaultWith mnemonicSizeToString
75 <> completer (listCompleter sizeStrs)