@@ -27,6 +27,7 @@ module Truncate.Main
2727
2828
2929------------------------------------------------------------------------
30+ import System.Environment (getProgName )
3031import System.Exit ( ExitCode (.. )
3132 , exitWith
3233 )
@@ -55,18 +56,22 @@ btMain :: (Show b, Truncatable b)
5556 -- program for validating (but not parsing) an input value.
5657 -- The 'Tuncate.Main.Readers' module provides readers for
5758 -- binary, decimal and hexadecimal inputs.
58- -> (String -> b ) -- ^ A function to generate a 'Truncatable' instance from
59+ -> (String -> b ) -- ^ A parser to generate a 'Truncatable' instance from
5960 -- a string (e.g., 'makeBin32').
60- -> String -- ^ A header printed as part of the program help .
61+ -> String -- ^ A short (one line) description of program's purpose .
6162 -> IO ()
62- btMain r f h = btMainWithExitCode r f h >>= exitWith
63+ btMain reader parser description =
64+ btMainWithExitCode reader parser description >>= exitWith
6365
6466btMainWithExitCode :: (Show b , Truncatable b ) =>
6567 ReadM String -> (String -> b ) -> String -> IO ExitCode
66- btMainWithExitCode r f h = runProgram =<< execParser programParser
67- where
68- optionParser = makeParser f r
69- programParser = info (helper <*> optionParser) (header h)
68+ btMainWithExitCode reader parser description = do
69+ prog <- getProgName
70+ let optionParser = makeParser reader parser
71+ programParser = info (helper <*> optionParser)
72+ (header $ prog ++ " - " ++ description)
73+ options <- execParser programParser
74+ runProgram options
7075
7176runProgram :: (Show a , Truncatable a ) => Options a -> IO ExitCode
7277runProgram (Options val bs b)
@@ -90,11 +95,11 @@ data Options a = Options { inputValue :: a
9095 }
9196 deriving (Show )
9297
93- makeParser :: ( a -> b ) -> ReadM a -> Parser (Options b )
94- makeParser f r = optionParser
98+ makeParser :: ReadM a -> ( a -> b ) -> Parser (Options b )
99+ makeParser reader parser = optionParser
95100 where
96101 optionParser = Options <$> valueParser <*> bitsParser <*> baseParser
97- valueParser = f <$> (argument r (metavar " value" ))
102+ valueParser = parser <$> (argument reader (metavar " value" ))
98103 bitsParser = or12 $ argument auto (metavar " bits" )
99104 baseParser = option auto ( short ' b'
100105 <> long " base"
0 commit comments