@@ -29,15 +29,12 @@ Scala CLI is available as the `scala` command alongside the Scala distribution i
29
29
Yes, even though its usage has been deprecated, it is still available under the ` scala_legacy ` command.
30
30
However, it is likely to be dropped in a future version.
31
31
32
- ``` bash ignore
33
- scala_legacy
32
+ ``` bash
33
+ scala_legacy -version
34
34
# [warning] MainGenericRunner class is deprecated since Scala 3.5.0, and Scala CLI features will not work.
35
35
# [warning] Please be sure to update to the Scala CLI launcher to use the new features.
36
36
# [warning] Check the Scala 3.5.0 release notes to troubleshoot your installation.
37
- # Welcome to Scala 3.5.0 (17, Java OpenJDK 64-Bit Server VM).
38
- # Type in expressions for evaluation. Or try :help.
39
- #
40
- # scala>
37
+ # Scala code runner version 3.5.0 -- Copyright 2002-2024, LAMP/EPFL
41
38
```
42
39
43
40
## How has the passing of arguments been changed from the old ` scala ` runner to Scala CLI?
@@ -46,55 +43,63 @@ Let us take a closer look on how the old runner handled arguments when compared
46
43
47
44
### The old ways
48
45
49
- In the old ` scala ` runner, the first argument was treated as the input source, while the second and following arguments
46
+ In the old runner, the first argument was treated as the input source, while the second and following arguments
50
47
were considered program arguments.
51
48
52
- ``` bash ignore
53
- scala Source.scala programArg1 programArg2
49
+ ``` scala title=Source.scala
50
+ @ main def main (args : String * ): Unit = println(args.mkString(" " ))
51
+ ```
52
+
53
+ ``` bash
54
+ scala_legacy Source.scala programArg1 programArg2
54
55
```
55
56
56
57
Since everything after the first argument had to be arbitrarily read as a program argument, regardless of format, all
57
58
runner options had to be passed before the source input.
58
59
59
- ``` bash ignore
60
- scala -save script.sc programArg1 programArg2
60
+ ``` bash
61
+ scala_legacy -save Source.scala programArg1 programArg2
61
62
```
62
63
63
64
### The ways of Scala CLI
64
65
65
66
With Scala CLI's default way of handling arguments, inputs and program arguments have to be
66
67
divided by ` -- ` . There is no limit for the number of either.
67
68
69
+ ``` scala title=Source2.scala
70
+ def placeholder = println(" Example extra source" )
71
+ ```
72
+
68
73
``` bash ignore
69
- scala-cli Source1 .scala Source2.scala -- programArg1 programArg2
74
+ scala Source .scala Source2.scala -- programArg1 programArg2
70
75
```
71
76
72
77
Additionally, a Scala CLI sub-command can be passed before the inputs section.
73
78
For example, to call the above example specifying the ` run ` sub-command explicitly, pass it like this:
74
79
75
- ``` bash ignore
76
- scala-cli run Source1 .scala Source2.scala -- programArg1 programArg2
80
+ ``` bash
81
+ scala run Source .scala Source2.scala -- programArg1 programArg2
77
82
```
78
83
79
84
More on sub-commands can be found [ here] ( ../../commands/basics.md ) .
80
85
81
86
Runner options can be passed on whatever position in the inputs section (before ` -- ` ).
82
87
For example, all the following examples are correct ways to specify the Scala version explicitly as ` 3.2 `
83
88
84
- ``` bash ignore
85
- scala-cli -S 3.2 Source1 .scala Source2.scala -- programArg1 programArg2
86
- scala-cli Source1 .scala -S 3.2 Source2.scala -- programArg1 programArg2
87
- scala-cli Source1 .scala Source2.scala -S 3.2 -- programArg1 programArg2
89
+ ``` bash
90
+ scala -S 3.2 Source .scala Source2.scala -- programArg1 programArg2
91
+ scala Source .scala -S 3.2 Source2.scala -- programArg1 programArg2
92
+ scala Source .scala Source2.scala -S 3.2 -- programArg1 programArg2
88
93
```
89
94
90
95
::: note
91
96
The exception to this rule are the launcher options, like ` --cli-version ` or ` --cli-scala-version ` .
92
97
Those have to be passed before the inputs section (before any source inputs).
93
98
94
- For example, to explicitly specify the launcher should run Scala CLI ` v0.1.20 ` , pass it like this:
99
+ For example, to explicitly specify the launcher should run Scala CLI ` v1.5.0 ` , pass it like this:
95
100
96
- ``` bash ignore
97
- scala-cli --cli-version 0.1.20 Source1 .scala Source2.scala -- programArg1 programArg2
101
+ ``` bash
102
+ scala --cli-version 1.5.0 Source .scala Source2.scala -- programArg1 programArg2
98
103
```
99
104
100
105
Also, if a Scala CLI sub-command is being passed explicitly, all launcher options have to be passed before the
@@ -103,7 +108,7 @@ sub-command.
103
108
For example, to call [ the ` package ` sub-command] ( ../../commands/package.md ) using the nightly CLI version, do it like this:
104
109
105
110
``` bash ignore
106
- scala-cli --cli-version nightly package --help
111
+ scala --cli-version nightly package --help
107
112
```
108
113
109
114
:::
@@ -114,8 +119,8 @@ To provide better support for shebang scripts, Scala CLI
114
119
has [ a dedicated ` shebang ` sub-command] ( ../../commands/shebang.md ) , which handles arguments similarly to the old ` scala `
115
120
script.
116
121
117
- ``` bash ignore
118
- scala-cli shebang Source.scala programArg1 programArg2
122
+ ``` bash
123
+ scala shebang Source.scala programArg1 programArg2
119
124
```
120
125
121
126
The purpose of the ` shebang ` sub-command is essentially to only be used in a shebang header (more
@@ -231,12 +236,12 @@ object Main {
231
236
232
237
<ChainedSnippets >
233
238
234
- ``` bash ignore
235
- scala Main.scala Hello world
239
+ ``` bash
240
+ scala_legacy Main.scala Hello world
236
241
```
237
242
238
243
``` bash
239
- scala-cli Main.scala -- Hello world
244
+ scala Main.scala -- Hello world
240
245
```
241
246
242
247
``` text
@@ -259,7 +264,7 @@ In other words, when explicitly declaring a main class when working with Scala C
259
264
file.
260
265
261
266
``` bash
262
- scala-cli main-in-script.sc -- Hello world
267
+ scala main-in-script.sc -- Hello world
263
268
# no output will be printed
264
269
```
265
270
@@ -280,7 +285,7 @@ However, it is supported by Scala CLI.
280
285
<ChainedSnippets >
281
286
282
287
``` bash
283
- scala-cli script.sc -- Hello world
288
+ scala script.sc -- Hello world
284
289
```
285
290
286
291
``` text
@@ -303,7 +308,7 @@ However, both the old Scala `3.x` runner as well as Scala CLI do not support it.
303
308
<ChainedSnippets >
304
309
305
310
``` bash fail
306
- scala-cli script.scala -- Hello world
311
+ scala script.scala -- Hello world
307
312
```
308
313
309
314
``` text
@@ -347,15 +352,15 @@ That is, all arguments starting with the second were treated as program args, ra
347
352
This is in contrast with the Scala CLI default way of handling arguments, where inputs and program arguments have to be
348
353
divided by ` -- ` .
349
354
350
- ``` bash ignore
351
- scala-cli Source1 .scala Source2.scala -- programArg1 programArg2
355
+ ``` bash
356
+ scala Source .scala Source2.scala -- programArg1 programArg2
352
357
```
353
358
354
359
To better support shebang scripts, Scala CLI has a dedicated ` shebang ` sub-command, which handles arguments similarly to
355
360
the old ` scala ` script.
356
361
357
- ``` bash ignore
358
- scala-cli shebang Source.scala programArg1 programArg2
362
+ ``` bash
363
+ scala shebang Source.scala programArg1 programArg2
359
364
```
360
365
361
366
For more concrete examples on how to change the shebang header in your existing scripts, look below.
@@ -409,17 +414,18 @@ implicitly run any compiled class files it would find.
409
414
```
410
415
411
416
This syntax has been dropped and is no longer supported with the new ` scala ` runner.
412
- ``` bash ignore
417
+ ``` bash
413
418
scalac hello.scala
414
- scala hello # NOTE: this syntax is not supported by Scala CLI
419
+ scala_legacy hello # NOTE: this syntax is not supported by Scala CLI
415
420
# Hello
416
421
```
417
422
418
423
With Scala CLI, all inputs have to be passed explicitly, so any compiled classes in the current working directory
419
424
would be ignored unless passed explicitly.
420
425
``` bash ignore
426
+ scala clean .
421
427
scalac hello.scala
422
- scala-cli run -cp .
428
+ scala run -cp .
423
429
# Hello
424
430
```
425
431
@@ -428,8 +434,9 @@ If only the classpath is passed with `-cp`, then the `run` sub-command can't be
428
434
will default to the REPL (as there are no explicit source file inputs present).
429
435
430
436
``` bash ignore
437
+ scala clean .
431
438
scalac hello.scala
432
- scala-cli -cp .
439
+ scala -cp .
433
440
# Welcome to Scala 3.5.0 (17, Java OpenJDK 64-Bit Server VM).
434
441
# Type in expressions for evaluation. Or try :help.
435
442
#
@@ -440,8 +447,9 @@ scala-cli -cp .
440
447
It is possible to explicitly specify the main class to be run (for example, if there are multiple main classes
441
448
in the build). The ` run ` sub-command becomes optional then, as passing ` -M ` indicates the intention to run something.
442
449
``` bash
450
+ scala clean .
443
451
scalac hello.scala
444
- scala-cli -cp . -M hello
452
+ scala -cp . -M hello
445
453
# Hello
446
454
```
447
455
@@ -451,16 +459,17 @@ with the `compile` sub-command, rather than the `scalac` script.
451
459
452
460
You don't have to specify the class files location, Scala CLI won't recompile them if they are up to date.
453
461
``` bash ignore
454
- scala-cli compile hello.scala
455
- scala-cli hello.scala
462
+ scala clean hello.scala
463
+ scala compile hello.scala
464
+ scala hello.scala
456
465
# Hello
457
466
```
458
467
459
468
Alternatively, you can also specify the location for the compiled classes explicitly, and then add them
460
469
to the classpath, as you would with ` scalac ` .
461
- ``` bash ignore
462
- scala-cli compile hello.scala -d compiled_classes
463
- scala-cli hello.scala
470
+ ``` bash
471
+ scala compile hello.scala -d compiled_classes
472
+ scala run -cp compiled_classes
464
473
# Hello
465
474
```
466
475
:::
0 commit comments