-
Notifications
You must be signed in to change notification settings - Fork 51
Add docs on the main function, argv and return codes #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||||||
|
|
||||||
| ## 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) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Seems the sentence is not finished? |
||||||
|
|
||||||
| ```py | ||||||
| #args.spy | ||||||
| def main(args: list[str]) -> None: | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use |
||||||
| 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: | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably we should say that this is the equivalent of CPython's |
||||||
|
|
||||||
| ```py | ||||||
| #argname.spy | ||||||
| def main(args: list[str]) -> None: | ||||||
| print(args[0]) | ||||||
| ``` | ||||||
| ``` | ||||||
| $ uv run spy argname.spy | ||||||
| foo.spy | ||||||
| ``` | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 libandspy build --target py-cffido not require amain.Maybe we can reword the section by starting this way:
However, I don't fully like the wording. What do you think?