nim-lang

Get version from Nimble Package?


Is there some way I can read the version variable from my project's .nimble file? Some languages include built in functions that will give you this value, does Nim have one of these functions?


Solution

  • The .nimble file can be parsed with parsecfg. This parsing can be as complex as shown in the documentation with a while loop and considering all possible cases, or you can trust that the .nimble file follows the standard and includes a line like:

    version = 0.1.2
    

    In this case you can just search for that case like this:

    import parsecfg
    
    var p: Config = loadConfig("./yourPackageName.nimble")
    echo p.getSectionValue("", "version") 
    

    The empty string passed to getSectionValue points to the root of the config file, and then you extract the version value from there.