@@ -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
@@ -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,45 +208,93 @@ 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
209
274
```
275
+
276
+ ``` text
277
+ Hello
278
+ ```
279
+
280
+ </ChainedSnippets >
281
+
210
282
- Markdown code (experimental)
283
+
284
+ <ChainedSnippets >
285
+
211
286
``` bash
212
287
echo ' # Example Snippet
213
288
```scala
214
289
println("Hello")
215
290
```' | scala-cli _.md
216
- # Hello
217
291
```
292
+
293
+ ``` text
294
+ Hello
295
+ ```
296
+
297
+ </ChainedSnippets >
218
298
219
299
More details in the [ Piping guide] ( ../guides/piping.md ) .
220
300
@@ -227,31 +307,56 @@ Running another Scala CLI version might be slower because it uses JVM-based Scal
227
307
228
308
To run another Scala CLI version, specify it with ` --cli-version ` before any other argument:
229
309
310
+ <ChainedSnippets >
311
+
230
312
``` bash
231
- scala-cli --cli-version 0.1.3-51-g4d314eee-SNAPSHOT about
232
- # Scala CLI version 0.1.3-51-g4d314eee-SNAPSHOT
313
+ scala-cli --cli-version 0.1.17-62-g21e1cf44-SNAPSHOT about
314
+ ```
315
+
316
+ ``` text
317
+ Scala CLI version: 0.1.17-62-g21e1cf44-SNAPSHOT
318
+ Scala version (default): 3.2.1
233
319
```
234
320
321
+ </ChainedSnippets >
322
+
235
323
<!-- Expected:
236
- 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
237
326
-->
238
327
239
328
To use the latest Scala CLI nightly build, pass ` nightly ` to ` --cli-version ` parameter:
240
329
330
+ <ChainedSnippets >
331
+
241
332
``` bash
242
333
scala-cli --cli-version nightly about
243
- # Scala CLI version 0.1.3-8-g431cc15f-SNAPSHOT
244
334
```
245
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
+
246
344
## Process substitution
247
345
248
346
Lastly, ` scala-cli ` also accepts input via shell process substitution:
249
347
348
+ <ChainedSnippets >
349
+
250
350
``` bash
251
351
scala-cli <( echo ' println("Hello")' )
252
- # Hello
253
352
```
254
353
354
+ ``` text
355
+ Hello
356
+ ```
357
+
358
+ </ChainedSnippets >
359
+
255
360
<!-- Expected:
256
361
Hello
257
362
-->
0 commit comments