@@ -3,6 +3,8 @@ title: Basics
3
3
sidebar_position : 3
4
4
---
5
5
6
+ import {ChainedSnippets} from "../../src/components/MarkdownComponents.js";
7
+
6
8
Scala CLI is a command line tool that executes a given sub-command on the inputs it’s provided with, using a
7
9
given [ configuration] ( ../guides/configuration.md ) to produce a result.
8
10
@@ -22,7 +24,7 @@ in which case it defaults to one of the sub-commands based on context:
22
24
- if any inputs were passed, it defaults to the ` run ` sub-command
23
25
- and so, ` scala-cli a.scala ` runs your ` a.scala ` file
24
26
- additionally, when no inputs were passed, it defaults to the ` run ` sub-command in the following scenarios:
25
- - if a snippet was passed with ` -- execute-script ` , ` --execute-scala ` or ` --execute-java `
27
+ - if a snippet was passed with ` -e ` , ` -- execute-script` , ` --execute-scala ` , ` --execute-java ` or ` --execute-markdown `
26
28
- if a main class was passed with the ` --main-class ` option alongside an extra ` --classpath `
27
29
- otherwise if no inputs were passed, it defaults to the ` repl ` sub-command
28
30
@@ -60,11 +62,18 @@ object Hello {
60
62
61
63
Then run it by passing it to ` scala-cli ` :
62
64
65
+ <ChainedSnippets >
66
+
63
67
``` bash
64
68
scala-cli Hello.scala
65
- # Hello from Scala
66
69
```
67
70
71
+ ``` text
72
+ Hello from Scala
73
+ ```
74
+
75
+ </ChainedSnippets >
76
+
68
77
You can also split your code into multiple files:
69
78
70
79
``` scala title=Messages.scala
@@ -82,11 +91,18 @@ object Hello {
82
91
83
92
and the run them with ` scala-cli ` :
84
93
94
+ <ChainedSnippets >
95
+
85
96
``` bash
86
97
scala-cli Hello.scala Messages.scala
87
- # Hello from Scala
88
98
```
89
99
100
+ ``` text
101
+ Hello from Scala
102
+ ```
103
+
104
+ </ChainedSnippets >
105
+
90
106
::: note
91
107
Scala CLI compiles only the provided inputs.
92
108
For example, if we provide only one of the files above:
@@ -124,11 +140,18 @@ object Hello {
124
140
125
141
In this case, you can run all the source code files in ` my-app ` by supplying the directory name:
126
142
143
+ <ChainedSnippets >
144
+
127
145
``` bash
128
146
scala-cli my-app
129
- # Hello from Scala
130
147
```
131
148
149
+ ``` text
150
+ Hello from Scala
151
+ ```
152
+
153
+ </ChainedSnippets >
154
+
132
155
In our experience, ` scala-cli . ` is the most used command; it compiles and runs all sources in the current directory.
133
156
134
157
::: note
@@ -160,11 +183,20 @@ scala-cli https://gist.github.com/alexarchambault/f972d941bc4a502d70267cfbbc4d63
160
183
` scala-cli ` accepts input via Github Gist’s urls.
161
184
It downloads the gist zip archive and runs it:
162
185
186
+ <ChainedSnippets >
187
+
163
188
``` bash
164
189
scala-cli https://gist.github.com/alexarchambault/7b4ec20c4033690dd750ffd601e540ec
165
- # Hello
166
190
```
167
191
192
+ ``` text
193
+ Hello
194
+ ```
195
+
196
+ </ChainedSnippets >
197
+
198
+ More details in the [ GitHub gists cookbook] ( ../cookbooks/gists.md ) .
199
+
168
200
### Zip archive
169
201
170
202
` scala-cli ` accepts inputs via a ` zip ` archive path.
@@ -176,38 +208,94 @@ object Hello extends App {
176
208
}
177
209
```
178
210
211
+ <ChainedSnippets >
212
+
179
213
``` bash ignore
180
214
unzip -l hello.zip
181
- # Archive: hello.zip
182
- # Length Date Time Name
183
- # --------- ---------- ----- ----
184
- # 49 12-07-2021 00:06 Hello.scala
185
- # --------- -------
186
- # 49 1 file
215
+ ```
216
+
217
+ ``` text
218
+ Archive: hello.zip
219
+ Length Date Time Name
220
+ --------- ---------- ----- ----
221
+ 49 12-07-2021 00:06 Hello.scala
222
+ --------- -------
223
+ 49 1 file
224
+ ```
225
+
226
+ ``` bash ignore
187
227
scala-cli hello.zip
188
- # Hello
189
228
```
190
229
230
+ ``` text
231
+ Hello
232
+ ```
233
+
234
+ </ChainedSnippets >
235
+
191
236
## Piping
192
237
193
238
You can also pipe code to ` scala-cli ` for execution:
194
239
195
240
- scripts
241
+
242
+ <ChainedSnippets >
243
+
196
244
``` bash
197
245
echo ' println("Hello")' | scala-cli _.sc
198
- # Hello
199
246
```
247
+
248
+ ``` text
249
+ Hello
250
+ ```
251
+
252
+ </ChainedSnippets >
253
+
200
254
- Scala code
255
+
256
+ <ChainedSnippets >
257
+
201
258
``` bash
202
259
echo ' @main def hello() = println("Hello")' | scala-cli _.scala
203
- # Hello
204
260
```
261
+
262
+ ``` text
263
+ Hello
264
+ ```
265
+
266
+ </ChainedSnippets >
267
+
205
268
- Java code
269
+
270
+ <ChainedSnippets >
271
+
206
272
``` bash
207
273
echo ' class Hello { public static void main(String args[]) { System.out.println("Hello"); } }' | scala-cli _.java
208
- # Hello
274
+ ```
275
+
276
+ ``` text
277
+ Hello
278
+ ```
279
+
280
+ </ChainedSnippets >
281
+
282
+ - Markdown code (experimental)
283
+
284
+ <ChainedSnippets >
285
+
286
+ ``` bash
287
+ echo ' # Example Snippet
288
+ ```scala
289
+ println("Hello")
290
+ ```' | scala-cli _.md
291
+ ```
292
+
293
+ ``` text
294
+ Hello
209
295
```
210
296
297
+ </ChainedSnippets >
298
+
211
299
More details in the [ Piping guide] ( ../guides/piping.md ) .
212
300
213
301
## Scala CLI version
@@ -219,31 +307,56 @@ Running another Scala CLI version might be slower because it uses JVM-based Scal
219
307
220
308
To run another Scala CLI version, specify it with ` --cli-version ` before any other argument:
221
309
310
+ <ChainedSnippets >
311
+
222
312
``` bash
223
- scala-cli --cli-version 0.1.3-51-g4d314eee-SNAPSHOT about
224
- # Scala CLI version 0.1.3-51-g4d314eee-SNAPSHOT
313
+ scala-cli --cli-version 0.1.17-62-g21e1cf44-SNAPSHOT about
225
314
```
226
315
316
+ ``` text
317
+ Scala CLI version: 0.1.17-62-g21e1cf44-SNAPSHOT
318
+ Scala version (default): 3.2.1
319
+ ```
320
+
321
+ </ChainedSnippets >
322
+
227
323
<!-- Expected:
228
- Scala CLI version 0.1.3-51-g4d314eee-SNAPSHOT
324
+ Scala CLI version: 0.1.17-62-g21e1cf44-SNAPSHOT
325
+ Scala version (default): 3.2.1
229
326
-->
230
327
231
328
To use the latest Scala CLI nightly build, pass ` nightly ` to ` --cli-version ` parameter:
232
329
330
+ <ChainedSnippets >
331
+
233
332
``` bash
234
333
scala-cli --cli-version nightly about
235
- # Scala CLI version 0.1.3-8-g431cc15f-SNAPSHOT
236
334
```
237
335
336
+ ``` text
337
+ Fetching Scala CLI 0.1.17-62-g21e1cf44-SNAPSHOT
338
+ Scala CLI version: 0.1.17-62-g21e1cf44-SNAPSHOT
339
+ Scala version (default): 3.2.1
340
+ ```
341
+
342
+ </ChainedSnippets >
343
+
238
344
## Process substitution
239
345
240
346
Lastly, ` scala-cli ` also accepts input via shell process substitution:
241
347
348
+ <ChainedSnippets >
349
+
242
350
``` bash
243
351
scala-cli <( echo ' println("Hello")' )
244
- # Hello
245
352
```
246
353
354
+ ``` text
355
+ Hello
356
+ ```
357
+
358
+ </ChainedSnippets >
359
+
247
360
<!-- Expected:
248
361
Hello
249
362
-->
0 commit comments