Skip to content

Commit d00fc04

Browse files
Implement CLI
Use CLI (new 'rsc') instead of Runner (old 'RustScript')
1 parent 2971ac5 commit d00fc04

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

Cli.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.util.List;
2+
import java.util.Arrays;
3+
4+
/**
5+
* @author William Rågstad <william.ragstad@gmail.com>
6+
* @version 0.1.0
7+
*
8+
* The CLI is a simple command line interface for the RustScript
9+
* language. It provides a simple way to run RustScript script files
10+
* and interact in a playful way using the REPL mode. Sometime in the
11+
* future, this might be extended to include a compiler to other
12+
* languages like JavaScript, C++ or Python.
13+
*/
14+
public class Cli {
15+
private static final String VERSION = "0.1.0";
16+
private static final String DESCRIPTION = "A command line interface tool for the RustScript language.";
17+
private static final String COPYRIGHT = "Copyright (c) 2021 William Rågstad";
18+
private static final String HELP = String.format("""
19+
RustScript CLI version %s.
20+
%s
21+
22+
Usage: rsc (options) (files)
23+
24+
Options:
25+
-h, --help
26+
Prints this help message.
27+
-v, --version
28+
Prints the version of the program.
29+
-r, --repl
30+
Starts the REPL mode.
31+
-c, --compile [files] (Not implemented)
32+
Compiles the given files.
33+
-l, --lint [files] (Not implemented)
34+
Lints the given files.
35+
36+
Execute scripts: rsc [files]
37+
Interprets the given script files one at a time.
38+
39+
%s""", VERSION, DESCRIPTION, COPYRIGHT);
40+
41+
public static void main(String[] args) {
42+
List<String> argsList = Arrays.asList(args).stream()
43+
.map((String option) -> option.startsWith("-") ? option.toLowerCase() : option).toList();
44+
if (argsList.contains("--help") || argsList.contains("-h")) {
45+
System.out.println(HELP);
46+
} else if (argsList.contains("--version") || argsList.contains("-v")) {
47+
System.out.println(VERSION);
48+
} else if (argsList.contains("--repl") || argsList.contains("-r")) {
49+
Repl.run();
50+
} else {
51+
if (argsList.size() > 0) {
52+
if (argsList.stream().anyMatch((String option) -> option.startsWith("-"))) {
53+
// Unknown option found
54+
System.out.println("Unknown option");
55+
} else {
56+
// All args are files
57+
Runner.run(args);
58+
}
59+
} else {
60+
System.out.println(HELP);
61+
}
62+
}
63+
// TODO: Add linting and compilation
64+
}
65+
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ There are global variables though.
3333
Run a script using the following command.
3434

3535
```shell
36-
java -jar RustScript.jar <script>
36+
java -jar rsc.jar <script>
3737
```
3838

3939
### 🔨 Build
4040

4141
If you want to build RustScript on your own, run the command below to build the `Runner.java` class into a standalone executable.
4242

4343
```shell
44-
jar -cvmf manifest.txt RustScript.jar *.class core/*.class
44+
jar -cvmf manifest.txt rsc.jar *.class core/*.class
4545
```
4646

4747

@@ -230,10 +230,10 @@ fold(lcm, 1, [1..20])
230230

231231
> The sum of the squares of the first ten natural numbers is,
232232
> 1^2 + 2^2 + ... + 10^2 = 385$$
233-
>
233+
>
234234
> The square of the sum of the first ten natural numbers is,
235235
> (1 + 2 + ... + 10)^2 = 55^2 = 3025$$
236-
>
236+
>
237237
>Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is $3025 - 385 = 2640$.
238238
>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
239239

manifest.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Main-Class: Runner
1+
Main-Class: Cli

0 commit comments

Comments
 (0)