Skip to content

Commit

Permalink
color print for help message.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joinhack committed Jan 17, 2025
1 parent 6335096 commit e568879
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ cap-tempfile = "3.4.1"
cap-rand = { version = "3.4.1", features = ["small_rng"] }
test-log = { version = "0.2", default-features = false, features = ["trace"] }
tracing-subscriber = { version = "0.3.1", default-features = false, features = ['fmt', 'env-filter', 'ansi', 'tracing-log'] }
clap = { version = "4.5.17", default-features = false, features = ["std", "derive"] }
clap = { version = "4.5.26", default-features = false, features = ["std", "derive", "wrap_help", "env", "string", "wrap_help", "error-context"] }

is-terminal = "0.4.10"
bitflags = "2.0"
Expand Down
1 change: 1 addition & 0 deletions bls-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dlopen = { workspace = true }
once_cell.workspace = true
dotenvy = "0.15.7"
env_logger = { workspace = true }
anstyle = "1.0.10"

[dev-dependencies]
tempfile = { workspace = true }
49 changes: 43 additions & 6 deletions bls-runtime/src/cli_clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ pub enum RuntimeType {

#[derive(Parser, Debug)]
pub struct PermissionFlags {
#[clap(long = "allow-read", num_args=(0..), hide=true, action=clap::ArgAction::Append, value_name = "[FILE[,]]", help = ALLOW_READ_HELP, value_parser = parser_allow)]
#[clap(long = "allow-read", id="allow-read", num_args=(0..), action=clap::ArgAction::Append, value_name = "[PATH[,]]", help = ALLOW_READ_HELP, value_parser = parser_allow)]
pub allow_read: Option<PermissionAllow>,

#[clap(long = "allow-write", num_args=(0..) , value_name = "[FILE[,]]", help = ALLOW_WRITE_HELP, value_parser = parser_allow)]
#[clap(long = "allow-write", id="allow-write", num_args=(0..) , value_name = "[PATH[,]]", help = ALLOW_WRITE_HELP, value_parser = parser_allow)]
pub allow_write: Option<PermissionAllow>,

#[clap(long = "allow-all", help = "Allow all permissions.")]
#[clap(long = "allow-all", id="allow-all", help = "Allow all permissions.")]
pub allow_all: bool,
}

Expand Down Expand Up @@ -261,8 +261,46 @@ pub struct StdioFlags {
/// The latest version from Cargo.toml
pub(crate) const SHORT_VERSION: &str = concat!("v", env!("CARGO_PKG_VERSION"));

pub fn get_styles() -> clap::builder::Styles {
clap::builder::Styles::styled()
.usage(
anstyle::Style::new()
.bold()
.underline()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow))),
)
.header(
anstyle::Style::new()
.bold()
.underline()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow))),
)
.literal(
anstyle::Style::new().fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Green))),
)
.invalid(
anstyle::Style::new()
.bold()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red))),
)
.error(
anstyle::Style::new()
.bold()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red))),
)
.valid(
anstyle::Style::new()
.bold()
.underline()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Green))),
)
.placeholder(
anstyle::Style::new().fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::White))),
)
}

#[derive(Parser, Debug)]
#[command(author, version = SHORT_VERSION, long_version = SHORT_VERSION, about = "Blockless WebAssembly Runtime", long_about = None)]
#[command(author, version = SHORT_VERSION, styles=get_styles(), arg_required_else_help = true, long_version = SHORT_VERSION, about = "Blockless WebAssembly Runtime")]
pub(crate) struct CliCommandOpts {
#[clap(long = "v86", value_name = "V86", required = false, help = V86_HELP )]
pub v86: bool,
Expand Down Expand Up @@ -348,11 +386,10 @@ pub(crate) struct CliCommandOpts {
#[clap(long = "nn-graph", value_name = "NN_GRAPH", value_parser = parse_nn_graph, help = NN_GRAPH_HELP)]
pub nn_graph: Vec<BlsNnGraph>,

#[arg(long)]
pub help: bool,
}

impl CliCommandOpts {

#[inline(always)]
pub fn fs_root_path(&self) -> Option<&String> {
self.fs_root_path.as_ref()
Expand Down
24 changes: 13 additions & 11 deletions bls-runtime/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,20 @@ fn parse_args() -> CliCommandOpts {
let mut cli_command = CliCommandOpts::command();
let clap_match = cli_command.get_matches_mut();
let cli_command_opts = CliCommandOpts::from_arg_matches(&clap_match);
if let Ok(mut o) = cli_command_opts {
if o.permission_flags.allow_read.is_none() && clap_match.contains_id("allow_read") {
o.permission_flags.allow_read = Some(blockless::PermissionAllow::AllowAll);
}
if o.permission_flags.allow_write.is_none() && clap_match.contains_id("allow_write") {
o.permission_flags.allow_write = Some(blockless::PermissionAllow::AllowAll);
}
o
} else {
exit(255);
match cli_command_opts {
Ok(mut o) => {
if o.permission_flags.allow_read.is_none() && clap_match.get_flag("allow-read") {
o.permission_flags.allow_read = Some(blockless::PermissionAllow::AllowAll);
}
if o.permission_flags.allow_write.is_none() && clap_match.contains_id("allow-write") {
o.permission_flags.allow_write = Some(blockless::PermissionAllow::AllowAll);
}
o
},
Err(_) => {
exit(255);
},
}

}

#[tokio::main]
Expand Down

0 comments on commit e568879

Please sign in to comment.