cli/
lib.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
8
9use clap::{Parser, Subcommand};
10use commands::handle_command;
11use tokio::task::LocalSet;
12
13mod commands;
14mod evaluate;
15mod repl;
16
17#[derive(Parser)]
18#[command(name = "spiderfire", about = "JavaScript Runtime")]
19pub struct Cli {
20	#[command(subcommand)]
21	command: Option<Command>,
22}
23
24#[derive(Subcommand)]
25pub(crate) enum Command {
26	#[command(about = "Prints Cache Statistics")]
27	Cache {
28		#[arg(help = "Clears the Cache", short, long)]
29		clear: bool,
30	},
31
32	#[command(about = "Evaluates a line of JavaScript")]
33	Eval {
34		#[arg(help = "Line of JavaScript to be evaluated", required(true))]
35		source: String,
36	},
37
38	#[command(about = "Starts a JavaScript Shell")]
39	Repl,
40
41	#[command(about = "Runs a JavaScript file")]
42	Run {
43		#[arg(
44			help = "The JavaScript file to run, Default: 'main.js'",
45			required(false),
46			default_value = "main.js"
47		)]
48		path: String,
49
50		#[arg(
51			help = "Sets logging level, Default: ERROR",
52			short,
53			long,
54			required(false),
55			default_value = "ERROR"
56		)]
57		log_level: String,
58
59		#[arg(help = "Sets logging level to DEBUG", short, long)]
60		debug: bool,
61
62		#[arg(help = "Disables ES Modules Features", short, long)]
63		script: bool,
64	},
65}
66
67#[tokio::main(flavor = "current_thread")]
68pub async fn main() {
69	let cli = Cli::parse();
70
71	#[cfg(windows)]
72	{
73		colored::control::set_virtual_terminal(true).unwrap();
74	}
75
76	let local = LocalSet::new();
77	local.run_until(handle_command(cli)).await;
78}