feat(cli): create proper cli
made a proper cli using clap
This commit is contained in:
5 files changed
+188
No files matched your search
@@ -0,0 +1,4 @@
|
||||
mod structure;
|
||||
|
||||
pub use structure::Action;
|
||||
pub use structure::Cli;
|
||||
@@ -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
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user