diff --git a/doc/setversions.py b/doc/setversions.py
index dc0bb11e75e..8e934667f93 100755
--- a/doc/setversions.py
+++ b/doc/setversions.py
@@ -24,15 +24,6 @@ import textwrap
 
 from urllib.request import urlopen, URLError
 
-parser = argparse.ArgumentParser(
-    description="Parse https://dashboard.yoctoproject.org/releases.json to get current releases information"
-)
-parser.add_argument("--get-latest-branch",
-                    help="Print current latest branch and exit",
-                    action="store_true",
-                    default=False)
-args = parser.parse_args()
-
 # NOTE: the following variables contain default values in case we are not able to fetch
 # the releases.json file from https://dashboard.yoctoproject.org/releases.json
 DEVBRANCH = "2.18"
@@ -95,16 +86,26 @@ if RELEASES_FROM_JSON:
     # current releases is also an LTS
     ACTIVERELEASES = list(dict.fromkeys([current_branch] + LTSSERIES))
 
-if args.get_latest_branch:
-    print(ACTIVERELEASES[0])
-    sys.exit(0)
-
 print(f"ACTIVERELEASES calculated to be {ACTIVERELEASES}", file=sys.stderr)
 print(f"DEVBRANCH calculated to be {DEVBRANCH}", file=sys.stderr)
 print(f"LTSSERIES calculated to be {LTSSERIES}", file=sys.stderr)
 
 BB_RELEASE_TAG_RE = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+$")
 
+def main():
+    parser = argparse.ArgumentParser(
+        description="Parse https://dashboard.yoctoproject.org/releases.json to get current releases information"
+    )
+    parser.add_argument("--get-latest-branch",
+                        help="Print current latest branch and exit",
+                        action="store_true",
+                        default=False)
+    args = parser.parse_args()
+
+    if args.get_latest_branch:
+        print(ACTIVERELEASES[0])
+        sys.exit(0)
+
 def get_current_version():
     # Test tags exist and inform the user to fetch if not
     try:
@@ -368,3 +369,6 @@ def write_releases_rst(releases_rst_out: str):
             - :yocto_docs:`1.6.2 BitBake User Manual </1.6.2/bitbake-user-manual/bitbake-user-manual.html>`
             - :yocto_docs:`1.6.3 BitBake User Manual </1.6.3/bitbake-user-manual/bitbake-user-manual.html>`
             """))
+
+if __name__ == "__main__":
+    main()
