How can my Haskell program or library find its version number?

HaskellVersionPackageCabal

Haskell Problem Overview


I would like my cabalised program to have a --version switch.

I would like it to report the same version as is present in the .cabal file.

If I have to update the version number separately in my Haskell source code as well as in the .cabal file, I will eventually get them out of sync.

So, how can my program, while being compiled under cabal, get its version number from the .cabal file?

Haskell Solutions


Solution 1 - Haskell

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondave4420View Question on Stackoverflow
Solution 1 - HaskellDon StewartView Answer on Stackoverflow