Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ markdown_extensions:
nav:
- Home: index.md
- contributing.md
- Language Features:
- howto/main_function.md
- API Reference:
- api_reference/python_builtins.md
- Unsorted notes:
Expand Down
53 changes: 53 additions & 0 deletions docs/src/howto/main_function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
title: The Main Function
---

All SPy programs must have a `main` function. The main function is the entry point of the program, and it is where the execution of the program begins.

```py
def main() -> None:
print("Hello world")
```

Not every `.spy` module needs a main function, but the module invoked by, e.g. `spy execute foo.spy` must contain a main function.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Not every `.spy` module needs a main function, but the module invoked by, e.g. `spy execute foo.spy` must contain a main function.
Not every `.spy` module needs a main function, but the module entry point invoked by, e.g. `spy foo.spy` or `spy build foo.spy` must contain a main function.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a big more correct, but it become more nuanced, because e.g. spy build --target lib and spy build --target py-cffi do not require a main.

Maybe we can reword the section by starting this way:

The execution of a SPy program starts from a main function; you need a main if you try to run the interpreter (spy foo.spy) or to build an executable (spy build foo.spy). You don't need a main if you build a library (e.g. spy build --target lib foo.spy).

However, I don't fully like the wording. What do you think?


## Return Codes

The `main` function may be typed to return an `int` (`i32`). If so, the return value of the main function will be the return value of the program:

```py
#retcode.spy
def main() -> int
return 123
```
```
$ uv run spy retcode.spy
$ echo $?
123
```


## Passed Arguments (Interpretter Only)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Passed Arguments (Interpretter Only)
## Passed Arguments (Interpreter Only)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as @kanin-kearpimy already pointed out, the arguments works also on compiled mode now 🎉.

Maybe instead of "passed arguments" we can call this section "Accessing command line arguments"?


If the `main` function accepts a list of strings as an argument, the SPy program will accept arguments from the command line, both when running in interpretted
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the `main` function accepts a list of strings as an argument, the SPy program will accept arguments from the command line, both when running in interpretted
If the `main` function accepts a list of strings as an argument, the SPy program will accept arguments from the command line, both when running in interpreted

Seems the sentence is not finished?


```py
#args.spy
def main(args: list[str]) -> None:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use argv as the canonical name, for consistency to CPython

print(args[1])
```
```
$ uv run spy args.spy 999
999
```

As with CPython, args[0] is the name of the string passed to the uv runtime:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably we should say that this is the equivalent of CPython's sys.argv


```py
#argname.spy
def main(args: list[str]) -> None:
print(args[0])
```
```
$ uv run spy argname.spy
foo.spy
```
Loading