Skip to content

Commit

Permalink
Move cli command related stuff to cli module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivorforce committed Apr 28, 2024
1 parent c55b292 commit 7c56d91
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
30 changes: 29 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
use clap::Command;
use std::process::ExitCode;
use crate::cli::logging::dump_failure;

pub mod run;
pub mod check;
pub mod transpile;
pub mod logging;
pub mod logging;

pub fn make_command() -> Command {
Command::new("monoteny")
.about("A cli implementation for the monoteny language.")
.subcommand_required(true)
.arg_required_else_help(true)
.allow_external_subcommands(true)
.subcommand(run::make_command())
.subcommand(check::make_command())
.subcommand(transpile::make_command())
}

pub fn run_command() -> ExitCode {
let matches = make_command().get_matches();

let result = match matches.subcommand() {
Some(("run", sub_matches)) => run::run(sub_matches),
Some(("check", sub_matches)) => check::run(sub_matches),
Some(("transpile", sub_matches)) => transpile::run(sub_matches),
_ => panic!("Unsupported action."),
};

result.unwrap_or_else(|e| dump_failure(e))
}
5 changes: 3 additions & 2 deletions src/cli/logging.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::time::Instant;
use std::process::ExitCode;
use std::time::Instant;

use colored::Colorize;
use crate::error;

use crate::error::{print_errors, RResult, RuntimeError};

pub fn dump_start(name: &str) -> Instant {
Expand Down
28 changes: 1 addition & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ extern crate lalrpop_util;
use std::env;
use std::process::ExitCode;

use clap::Command;
use itertools::Itertools;

use cli::logging::dump_failure;

lalrpop_mod!(pub monoteny_grammar);
pub mod interpreter;
pub mod resolver;
Expand All @@ -25,30 +22,7 @@ pub mod cli;
pub mod static_analysis;
pub mod ast;

fn cli() -> Command {
Command::new("monoteny")
.about("A cli implementation for the monoteny language.")
.subcommand_required(true)
.arg_required_else_help(true)
.allow_external_subcommands(true)
.subcommand(cli::run::make_command())
.subcommand(cli::check::make_command())
.subcommand(cli::transpile::make_command())
}

fn main() -> ExitCode {
println!("{}", env::args().join(" "));
let matches = cli().get_matches();

let result = match matches.subcommand() {
Some(("run", sub_matches)) => cli::run::run(sub_matches),
Some(("check", sub_matches)) => cli::check::run(sub_matches),
Some(("transpile", sub_matches)) => cli::transpile::run(sub_matches),
_ => panic!("Unsupported action."),
};

match result {
Ok(c) => c,
Err(e) => dump_failure(e),
}
cli::run_command()
}

0 comments on commit 7c56d91

Please sign in to comment.