This is well supported with Cabal. As follows (from xmonad):
Import Paths_$myprogram
– a file Cabal creates with lots of metadata from the .cabal file, along with a the module for handling version numbers:
import Paths_xmonad (version)
import Data.Version (showVersion)
Add a print statement to print the ‘version’ field provided by Paths_$myprogram
:
case args of
["--version"] -> putStrLn ("xmonad " ++ showVersion version)
In general, Cabal’s generated Paths file contains the following values, in dist/build/autogen/
version,
getBinDir, getLibDir, getDataDir, getLibexecDir,
getDataFileName