Skip to content

Commit 768af8f

Browse files
authored
Merge pull request #1 from ajdawson/executable-names
Allow automatic discovery of a main program's name
2 parents cb9c967 + b78302c commit 768af8f

7 files changed

Lines changed: 33 additions & 28 deletions

File tree

app/Bin32.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import Truncate (makeBin32)
1717
import Truncate.Main (btMain)
1818
import Truncate.Main.Readers (binary)
1919

20-
header :: String
21-
header = "trbin32 - Truncate 32-bit floating-point numbers represented in binary"
20+
description :: String
21+
description = "Truncate 32-bit floating-point numbers represented in binary"
2222

2323
main :: IO ()
24-
main = btMain binary makeBin32 header
24+
main = btMain binary makeBin32 description

app/Bin64.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import Truncate (makeBin64)
1717
import Truncate.Main (btMain)
1818
import Truncate.Main.Readers (binary)
1919

20-
header :: String
21-
header = "trbin64 - Truncate 64-bit floating-point numbers represented in binary"
20+
description :: String
21+
description = "Truncate 64-bit floating-point numbers represented in binary"
2222

2323
main :: IO ()
24-
main = btMain binary makeBin64 header
24+
main = btMain binary makeBin64 description

app/Dec32.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import Truncate (makeDec32)
1717
import Truncate.Main (btMain)
1818
import Truncate.Main.Readers (decimal)
1919

20-
header :: String
21-
header = "trdec32 - Truncate 32-bit floating-point numbers represented in decimal"
20+
description :: String
21+
description = "Truncate 32-bit floating-point numbers represented in decimal"
2222

2323
main :: IO ()
24-
main = btMain decimal makeDec32 header
24+
main = btMain decimal makeDec32 description

app/Dec64.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import Truncate (makeDec64)
1717
import Truncate.Main (btMain)
1818
import Truncate.Main.Readers (decimal)
1919

20-
header :: String
21-
header = "trdec64 - truncate 64-bit floating-point numbers in decimal"
20+
description :: String
21+
description = "truncate 64-bit floating-point numbers in decimal"
2222

2323
main :: IO ()
24-
main = btMain decimal makeDec64 header
24+
main = btMain decimal makeDec64 description

app/Hex32.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import Truncate (makeHex32)
1717
import Truncate.Main (btMain)
1818
import Truncate.Main.Readers (hexadecimal)
1919

20-
header :: String
21-
header = "trhex32 - Truncate 32-bit floating-point numbers represented in hexadecimal"
20+
description :: String
21+
description = "Truncate 32-bit floating-point numbers represented in hexadecimal"
2222

2323
main :: IO ()
24-
main = btMain hexadecimal makeHex32 header
24+
main = btMain hexadecimal makeHex32 description

app/Hex64.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import Truncate (makeHex64)
1717
import Truncate.Main (btMain)
1818
import Truncate.Main.Readers (hexadecimal)
1919

20-
header :: String
21-
header = "trhex64 - Truncate 64-bit floating-point numbers represented in hexadecimal"
20+
description :: String
21+
description = "Truncate 64-bit floating-point numbers represented in hexadecimal"
2222

2323
main :: IO ()
24-
main = btMain hexadecimal makeHex64 header
24+
main = btMain hexadecimal makeHex64 description

src/Truncate/Main.hs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Truncate.Main
2727

2828

2929
------------------------------------------------------------------------
30+
import System.Environment (getProgName)
3031
import 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

6466
btMainWithExitCode :: (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

7176
runProgram :: (Show a, Truncatable a) => Options a -> IO ExitCode
7277
runProgram (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

Comments
 (0)