Skip to content

Commit 6962fc8

Browse files
Add more docs for piping sources (#1053)
* Add more docs for piping sources * Apply suggestions from code review Co-authored-by: Alexandre Archambault <[email protected]> Co-authored-by: Alexandre Archambault <[email protected]>
1 parent 036ccfc commit 6962fc8

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

website/docs/commands/basics.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,23 @@ scala-cli hello.zip
174174

175175
## Piping
176176

177-
You can also pipe Scala code to `scala-cli` for execution:
178-
179-
```bash
180-
echo 'println("Hello")' | scala-cli -
181-
# Hello
182-
```
177+
You can also pipe code to `scala-cli` for execution:
178+
- scripts
179+
```bash
180+
echo 'println("Hello")' | scala-cli _.sc
181+
# Hello
182+
```
183+
- Scala code
184+
```bash
185+
echo '@main def hello() = println("Hello")' | scala-cli _.scala
186+
# Hello
187+
```
188+
- Java code
189+
```bash
190+
echo 'class Hello { public static void main(String args[]) { System.out.println("Hello"); } }' | scala-cli _.java
191+
# Hello
192+
```
193+
More details in the [Piping guide](../guides/piping.md).
183194

184195
## Scala CLI version
185196

website/docs/guides/piping.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Piping
3+
sidebar_position: 23
4+
---
5+
6+
# Piping
7+
8+
Instead of passing paths to your sources, you can also pipe your code via standard input:
9+
```bash
10+
echo '@main def hello() = println("Hello")' | scala-cli _
11+
# Hello
12+
```
13+
14+
## Wildcards
15+
The `_` wildcard implies that the piped code is a standard Scala app.
16+
It is also possible to pass a script or Java code, when using the appropriate wildcard.
17+
The available options are as follows:
18+
- for standard Scala code use `_`, `_.scala` or `-.scala`;
19+
- for Scala scripts use `-`, `_.sc` or `-.sc`;
20+
- for Java code use `_.java` or `-.java`.
21+
22+
## Examples
23+
- scripts
24+
```bash
25+
echo 'println("Hello")' | scala-cli _.sc
26+
# Hello
27+
```
28+
- Scala code
29+
```bash
30+
echo '@main def hello() = println("Hello")' | scala-cli _.scala
31+
# Hello
32+
```
33+
- Java code
34+
```bash
35+
echo 'class Hello { public static void main(String args[]) { System.out.println("Hello"); } }' | scala-cli _.java
36+
# Hello
37+
```
38+
39+
## Mixing piped sources with on-disk ones
40+
It is also possible to pipe some code via standard input, while the rest of your code is on-disk.
41+
```bash
42+
echo 'case class HelloMessage(msg: String)' > HelloMessage.scala
43+
echo '@main def hello() = println(HelloMessage(msg = "Hello").msg)' | scala-cli _ HelloMessage.scala
44+
# Hello
45+
```

0 commit comments

Comments
 (0)