feat(cli): add --help and --version (#105)

This commit is contained in:
Fathy Boundjadj 2023-02-10 07:36:24 +01:00 committed by GitHub
parent 191d45dd59
commit b330a25c97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 27 deletions

View File

@ -1,3 +1,3 @@
mod ffi;
mod browser;
pub use ffi::*;
pub use browser::*;

View File

@ -6,6 +6,7 @@ use std::{env, io};
use libc::{c_char, c_int, c_uchar, c_uint, c_void, size_t};
use crate::cli;
use crate::gfx::{Cast, Color, Point, Rect, Size};
use crate::output::Renderer;
use crate::ui::navigation::NavigationAction;
@ -65,28 +66,6 @@ pub struct BrowserDelegate {
post_task: extern "C" fn(extern "C" fn(*mut c_void), *mut c_void),
}
struct Args {
debug: bool,
chromium: Vec<String>,
}
fn parse_args() -> Args {
let mut args = Args {
debug: false,
chromium: Vec::new(),
};
for arg in env::args().skip(1) {
if arg == "--debug" {
args.debug = true
} else {
args.chromium.push(arg)
}
}
args
}
fn main() -> io::Result<Option<i32>> {
const CARBONYL_INSIDE_SHELL: &str = "CARBONYL_INSIDE_SHELL";
@ -94,10 +73,14 @@ fn main() -> io::Result<Option<i32>> {
return Ok(None);
}
let args = parse_args();
let cmd = match cli::main() {
None => return Ok(Some(0)),
Some(cmd) => cmd,
};
let mut terminal = input::Terminal::setup();
let output = Command::new(env::current_exe()?)
.args(args.chromium)
.args(cmd.args)
.arg("--disable-threaded-scrolling")
.arg("--disable-threaded-animation")
.env(CARBONYL_INSIDE_SHELL, "1")
@ -110,7 +93,7 @@ fn main() -> io::Result<Option<i32>> {
let code = output.status.code().unwrap_or(127);
if code != 0 || args.debug {
if code != 0 || cmd.debug {
io::stderr().write_all(&output.stderr)?;
}

5
src/cli.rs Normal file
View File

@ -0,0 +1,5 @@
mod main;
mod parser;
pub use main::*;
pub use parser::*;

25
src/cli/main.rs Normal file
View File

@ -0,0 +1,25 @@
use super::{CommandLine, CommandLineProgram};
pub fn main() -> Option<CommandLine> {
match CommandLineProgram::parse() {
CommandLineProgram::Main(cmd) => return Some(cmd),
CommandLineProgram::Help => {
println!("{}", help())
}
CommandLineProgram::Version => {
println!("Carbonyl {}", env!("CARGO_PKG_VERSION"))
}
}
None
}
pub fn help() -> &'static str {
"Usage: carbonyl [options] [url]
Options:
-h, --help display this help message
-d, --debug enable debug logs
-v, --version output the version number
"
}

30
src/cli/parser.rs Normal file
View File

@ -0,0 +1,30 @@
use std::env;
pub struct CommandLine {
pub args: Vec<String>,
pub debug: bool,
}
pub enum CommandLineProgram {
Help,
Version,
Main(CommandLine),
}
impl CommandLineProgram {
pub fn parse() -> CommandLineProgram {
let mut debug = false;
let mut args = Vec::new();
for arg in env::args() {
match arg.as_str() {
"-d" | "--debug" => debug = true,
"-h" | "--help" => return CommandLineProgram::Help,
"-v" | "--version" => return CommandLineProgram::Version,
_ => args.push(arg),
}
}
CommandLineProgram::Main(CommandLine { args, debug })
}
}

View File

@ -1,4 +1,5 @@
pub mod browser;
pub mod cli;
pub mod gfx;
pub mod input;
pub mod output;