Repository: linkml/linkml

Issue:
Add `--version` flag
As per https://clig.dev/ we should have a `--version` flag on generator and other commands. This should display the linkml version - perhaps also runtime.

not sure how to implement this:

```python
>>> import linkml
RDFLib Version: 5.0.0
>>> linkml.__version__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'linkml' has no attribute '__version__'
```

Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/linkml/linkml/pull/1048?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=linkml) Report
> Merging [#1048](https://codecov.io/gh/linkml/linkml/pull/1048?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=linkml) (617b825) into [main](https://codecov.io/gh/linkml/linkml/commit/eb29c53f4c85c46100fc312d96ed1c3ede4a253a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=linkml) (eb29c53) will **decrease** coverage by `0.00%`.
> The diff coverage is `71.42%`.

```diff
@@            Coverage Diff             @@
##             main    #1048      +/-   ##
==========================================
- Coverage   78.06%   78.06%   -0.01%     
==========================================
  Files          73       74       +1     
  Lines        8390     8398       +8     
  Branches     2063     2063              
==========================================
+ Hits         6550     6556       +6     
- Misses       1446     1448       +2     
  Partials      394      394              
```


| [Impacted Files](https://codecov.io/gh/linkml/linkml/pull/1048?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=linkml) | Coverage Δ | |
|---|---|---|
| [linkml/\_version.py](https://codecov.io/gh/linkml/linkml/pull/1048/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=linkml#diff-bGlua21sL192ZXJzaW9uLnB5) | `60.00% <60.00%> (ø)` | |
| [linkml/utils/converter.py](https://codecov.io/gh/linkml/linkml/pull/1048/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=linkml#diff-bGlua21sL3V0aWxzL2NvbnZlcnRlci5weQ==) | `51.96% <100.00%> (+1.45%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=linkml). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=linkml)

cmungall: Thanks so much @dalito 🙏 ! @hsolbrig is trying this out and will write a test for it - I think he has some opinions about `-V` vs `--version` too!
cmungall: Good idea @hsolbrig! I made an issue #1051 

Patch:
diff --git a/linkml/_version.py b/linkml/_version.py
new file mode 100644
index 00000000..666640ee
--- /dev/null
+++ b/linkml/_version.py
@@ -0,0 +1,7 @@
+from importlib.metadata import version, PackageNotFoundError
+
+try:
+    __version__ = version("linkml")
+except PackageNotFoundError:
+    # package is not installed
+    __version__ = "0.0.0"
diff --git a/linkml/utils/converter.py b/linkml/utils/converter.py
index b1acd164..75b0a3f6 100644
--- a/linkml/utils/converter.py
+++ b/linkml/utils/converter.py
@@ -1,5 +1,6 @@
 import logging
 import os
+import sys
 from pathlib import Path
 from typing import List
 
@@ -16,6 +17,8 @@ from linkml.utils.datautils import (_get_context, _get_format, _is_xsv,
                                     dumpers_loaders, get_dumper, get_loader,
                                     infer_index_slot, infer_root_class)
 
+from linkml._version import __version__
+
 
 @click.command()
 @click.option("--module", "-m", help="Path to python datamodel module")
@@ -61,6 +64,7 @@ from linkml.utils.datautils import (_get_context, _get_format, _is_xsv,
     help="Infer missing slot values",
 )
 @click.option("--context", "-c", multiple=True, help="path to JSON-LD context file")
+@click.version_option(__version__, "-V", "--version")
 @click.argument("input")
 def cli(
     input,
diff --git a/pyproject.toml b/pyproject.toml
index ff308548..d22658ae 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -42,6 +42,11 @@ packages = [
     { include = "linkml" }
 ]
 
+[tool.poetry-dynamic-versioning]
+enable = true
+vcs = "git"
+style = "pep440"
+
 [tool.poetry.scripts]    
 gen-jsonld-context = "linkml.generators.jsonldcontextgen:cli"
 gen-prefix-map = "linkml.generators.prefixmapgen:cli"
@@ -130,5 +135,5 @@ coverage = "^6.4.1"
 docs = ["Sphinx", "sphinx-rtd-theme", "sphinxcontrib-mermaid", "furo"]
 
 [build-system]
-requires = ["poetry-core>=1.0.0"]
-build-backend = "poetry.core.masonry.api"
+requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning"]
+build-backend = "poetry_dynamic_versioning.backend"


Test patch:
diff --git a/tests/test_utils/test_converter.py b/tests/test_utils/test_converter.py
index 5666dcce..3b1d1cb8 100644
--- a/tests/test_utils/test_converter.py
+++ b/tests/test_utils/test_converter.py
@@ -58,3 +58,9 @@ class TestCommandLineInterface(unittest.TestCase):
             self.assertEqual(p2["age_in_months"], 240)
             self.assertEqual(p2["age_category"], "adult")
             self.assertEqual(p2["full_name"], "first2 last2")
+
+    def test_version(self):
+        runner = CliRunner(mix_stderr=False)
+        result = runner.invoke(cli, ["--version"])
+        # self.assertEqual(0, result.exit_code)  # fails! unclear why result.exit_code is 1 not 0
+        self.assertIn("version", result.stdout)