|
| 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 | +} |
0 commit comments