Skip to content
Snippets Groups Projects
Commit 06fabe0a authored by Pascal Palenda's avatar Pascal Palenda
Browse files

Feat: open the examples instead of running them

parent da861e7b
No related branches found
No related tags found
1 merge request!23Feature: improve examples
import importlib
import os
import sys
from pathlib import Path
def main():
"""
Main function to list and execute example scripts.
Main function to list and open example scripts.
This function performs the following steps:
1. Lists all Python files in the current directory that contain "example" in their name.
2. If no command-line arguments are provided, it prints the list of example files and prompts the user to select one by index.
3. If a command-line argument is provided, it uses that as the index of the example file to run.
4. Validates the selected index and either exits, prints an error message, or imports and runs the selected example's main function.
3. If a command-line argument is provided, it uses that as the index of the example file to open.
4. Validates the selected index and either exits, prints an error message, or opens the file.
Returns:
None
"""
......@@ -25,7 +25,7 @@ def main():
print(f"{len(example_files)}: Exit")
example_index = int(input("Enter the index of the example you want to run: "))
example_index = int(input("Enter the index of the example you want to open: "))
else:
example_index = int(sys.argv[1])
......@@ -37,14 +37,7 @@ def main():
return
example_file = example_files[example_index]
module_name = f"vapython.examples.{example_file.stem}"
module = importlib.import_module(module_name)
example_main = getattr(module, "main", None)
if example_main is not None:
example_main()
else:
print(f"Could not find 'main' function in module '{module_name}'")
os.startfile(example_file) # noqa: S606
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment