Skip to content

Commit 5d19c8d

Browse files
committed
Update documentation of shebang command and scripts
1 parent 6e33db7 commit 5d19c8d

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

website/docs/commands/shebang.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ sidebar_position: 30
66
This command is equivalent to `run`, but it changes the way Scala CLI parses options (used to configure the tool) and
77
inputs (the sources of your project) in order to be compatible with `shebang` scripts.
88

9+
The command `shebang` also allows script files to be executed even if they have no file extension,
10+
provided they start with the [`shebang` header](../guides/shebang.md#shebang-script-headers).
11+
Note that those files are always run as scripts even though they may contain e.g. valid `.scala` program.
12+
913
Normally, inputs and `scala-cli` options can be mixed. Program arguments (to be passed to your app) have to be specified
1014
after `--` (double dash) separator.
1115

website/docs/guides/scripts.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ It is also possible to set `scala-cli` command-line options in the shebang line,
140140
#!/usr/bin/env -S scala-cli shebang --scala-version 2.13
141141
```
142142

143+
The command `shebang` also allows script files to be executed even if they have no file extension,
144+
provided they start with the [`shebang` header](../guides/shebang.md#shebang-script-headers).
145+
Note that those files are always run as scripts even though they may contain e.g. valid `.scala` program.
146+
143147
### Arguments
144148

145149
You may also pass arguments to your script, and they are referenced with the special `args` variable:

website/docs/guides/shebang.md

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ println(args.size)
1919
println(args.headOption)
2020
```
2121

22-
And it works almost correctly.
22+
And it works correctly:
2323

2424
<ChainedSnippets>
2525

@@ -40,7 +40,7 @@ None
4040

4141
</ChainedSnippets>
4242

43-
And it also works.
43+
And it also works:
4444

4545
<ChainedSnippets>
4646

@@ -173,4 +173,63 @@ world: not found
173173
world: not found
174174
-->
175175

176+
</ChainedSnippets>
177+
178+
### Script files' extensions
179+
180+
When running the `shebang` subcommand, script files don't need the `.sc` extension,
181+
but they are then REQUIRED to start with a shebang line:
182+
183+
```scala title=hello-with-shebang
184+
#!/usr/bin/env -S scala-cli shebang -S 3
185+
186+
println(args.size)
187+
println(args.headOption)
188+
```
189+
190+
<ChainedSnippets>
191+
192+
```bash
193+
chmod +x hello-with-shebang
194+
./hello-with-shebang Hello World
195+
```
196+
197+
```text
198+
2
199+
Some(Hello)
200+
```
201+
<!-- Expected:
202+
2
203+
Some(Hello)
204+
-->
205+
206+
</ChainedSnippets>
207+
208+
```scala title=hello-no-shebang
209+
println(args.size)
210+
println(args.headOption)
211+
```
212+
213+
<ChainedSnippets>
214+
215+
```bash run-fail
216+
chmod +x hello-no-shebang
217+
scala-cli shebang hello-no-shebang Hello World
218+
```
219+
220+
```text
221+
[error] hello-no-shebang does not contain shebang header
222+
possible fixes:
223+
Add '#!/usr/bin/env scala-cli shebang' to the top of the file
224+
Add extension to the file's name e.q. '.sc'
225+
```
226+
<!-- Expected:
227+
[error] hello-no-shebang does not contain shebang header
228+
possible fixes:
229+
Add '#!/usr/bin/env scala-cli shebang' to the top of the file
230+
Add extension to the file's name e.q. '.sc'
231+
-->
232+
233+
Note that files with no extensions are always run as scripts even though they may contain e.g. valid `.scala` program.
234+
176235
</ChainedSnippets>

0 commit comments

Comments
 (0)