Skip to content

Commit

Permalink
Switch CLI to clap for a better experience (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirdaki authored Nov 22, 2021
1 parent 21a2ccb commit 922756c
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
name = "theforce"
version = "0.1.0"
edition = "2018"
description = "A Star Wars inspired programming language."
homepage = "https://github.com/mirdaki/theforce"
repository = "https://github.com/mirdaki/theforce"
license = "MIT"
keywords = ["star-wars", "programming-language"]
categories = ["compilers", "command-line-utilities"]

[features]
llvm = ["inkwell"]
Expand All @@ -10,3 +16,4 @@ llvm = ["inkwell"]
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "llvm10-0", optional = true }
pest = "2.1.3"
pest_derive = "2.1.0"
clap = "2.33.3"
36 changes: 21 additions & 15 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
use std::fs;

pub struct Args {
pub path: std::path::PathBuf,
}
extern crate clap;
use clap::{App, Arg, ArgMatches};

pub fn parse_arguments() -> Result<Args, String> {
let path = match std::env::args().nth(1) {
Some(path) => path,
None => return Err("Args could not be parsed".to_string()),
};
use std::fs;

Ok(Args {
path: std::path::PathBuf::from(path),
})
pub fn parse_arguments() -> ArgMatches<'static> {
App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.after_help(&*format!(
"For more information, please visit {}",
env!("CARGO_PKG_HOMEPAGE")
))
.arg(
Arg::with_name("PATH")
.help("The path to a `.force` file to run.")
.required(true)
.index(1),
)
.get_matches()
}

pub fn read_source(args: Args) -> Result<String, String> {
match fs::read_to_string(args.path) {
pub fn read_source(args: ArgMatches) -> Result<String, String> {
match fs::read_to_string(args.value_of("PATH").unwrap()) {
Ok(content) => Ok(content),
Err(_) => Err("File could not be read".to_string()),
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ mod parser;
mod compiler;

fn main() -> Result<(), String> {
let config = cli::parse_arguments()?;
let args = cli::parse_arguments();

let source = cli::read_source(config)?;
let source = cli::read_source(args)?;

let ast = parser::parse(source.as_str());
interpreter::evaluate(ast.unwrap(), io::stdin().lock(), io::stdout())
Expand Down

0 comments on commit 922756c

Please sign in to comment.