generate-version.py: Add verion and help options

Replace basic sys.argv argument parsing with argparse module to provide:
- Change the Zephyr version argument from a positional argument to an
  explicit -v/--version option, making the intent clearer at the command
  line.
- Proper -h/--help option with usage information.
- Better error messages for invalid version format.
- Usage examples in help text.
- Improved user experience.
- Update README.md with usage.

The script now displays helpful information when called with --help:
  $ generate-version.py --help
  usage: generate-version.py [-h] -v VERSION

  Generate Zephyr kernel source include file for a specific version

  options:
    -h, --help            show this help message and exit
    -v VERSION, --version VERSION
                        Zephyr version in the form x.y.z (e.g., 3.7.0, 4.3.0)

  Example:
    generate-version.py -v 3.7.0
    generate-version.py --version 4.3.0

AI-Generated: GitHub Copilot (Claude Sonnet 4.6)

Signed-off-by: Sandeep Gundlupet Raju <sandeep.gundlupet-raju@amd.com>
Signed-off-by: Lee Chee Yang <chee.yang.lee@intel.com>
This commit is contained in:
Sandeep Gundlupet Raju
2026-03-06 21:43:48 -07:00
committed by Lee Chee Yang
parent 32dbada167
commit 8861e85b10
2 changed files with 22 additions and 5 deletions

View File

@@ -216,7 +216,7 @@ Yocto configuration for a Zephyr version from the West configuration in the
Zephyr repository. It requires the west and jinja2 Python packages to be
installed on the host. Run it as follows:
```
$ ./meta-zephyr-core/scripts/generate-version.py x.x.x
$ ./meta-zephyr-core/scripts/generate-version.py -v x.x.x
```
where x.x.x is the Zephyr version.

View File

@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import argparse
import pathlib
import re
import subprocess
@@ -11,10 +12,26 @@ import urllib.request
import jinja2
import west.manifest
# This script takes one argument - the Zephyr version in the form x.y.z
version = sys.argv[1]
if not re.match(r'\d+.\d+.\d+', version):
raise ValueError("Please provide a valid Zephyr version")
# Set up argument parser
parser = argparse.ArgumentParser(
description='Generate Zephyr kernel source include file for a specific version',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog='''
Example:
%(prog)s -v 3.7.0
%(prog)s --version 4.3.0
''')
parser.add_argument('-v', '--version',
required=True,
metavar='VERSION',
help='Zephyr version in the form x.y.z (e.g., 3.7.0, 4.3.0)')
args = parser.parse_args()
version = args.version
# Validate version format
if not re.match(r'\d+\.\d+\.\d+', version):
parser.error(f"Invalid version format: '{version}'. Please provide a valid Zephyr version (e.g., 3.7.0)")
# Obtain the West manifest and decode using west as a library
manifest_url = f'https://raw.githubusercontent.com/zephyrproject-rtos/zephyr/v{version}/west.yml'