feat(cli): create proper cli

made a proper cli using clap
This commit is contained in:
ozzy committed 2026-09-27 15:09:57 -05:00
1 parent fdf4464edb
commit dbc14d0403
5 files changed
+188

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
mod structure;
pub use structure::Action;
pub use structure::Cli;
+21
View File
@@ -0,0 +1,21 @@
use clap::{Args, Parser, Subcommand};
#[derive(Parser)]
#[command(version, about = "a cli for math", long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub action: Option<Action>,
}
#[derive(Subcommand)]
pub enum Action {
/// evaluate an expression
#[command(aliases = ["eval", "e"])]
Evaluate(EvaluateArgs),
}
#[derive(Args)]
pub struct EvaluateArgs {
#[arg(trailing_var_arg = true)]
pub expression: Vec<String>,
}
+20
View File
@@ -1,8 +1,28 @@
use std::io::{self, IsTerminal, Write};
use clap::Parser;
mod cli;
mod math;
fn main() -> io::Result<()> {
let cli = cli::Cli::parse();
match cli.action {
Some(cli::Action::Evaluate(args)) => {
let expr = args.expression.join(" ");
let expr = math::parse(&expr);
println!("{}", math::eval(expr));
Ok(())
}
None => repl(),
}
}
fn repl() -> io::Result<()> {
if io::stdin().is_terminal() {
println!("mcl: math command line | ctrl + d to exit");
}
prompt()?;
for line in io::stdin().lines() {