Accept numeric values for boolean env flags

This commit is contained in:
ldm0
2026-08-12 12:27:59 +08:00
parent 46d2b93836
commit 3bfff8449f
3 changed files with 34 additions and 16 deletions
+5 -4
View File
@@ -179,10 +179,11 @@ Moli keeps expensive browser work explicit rather than silently enabling it:
| `--image`, `--font`, `--audio`, `--video`, `--media`, `--text-track` | Enable one specific optional resource family |
| `--profile-dir`, `--http-cache-dir`, `--cookie-file` | Opt into whatever persistence the workload needs |
`MOLI_LAYOUT=true` and `MOLI_RESOURCE=true` provide environment-variable
fallbacks for `--layout` and `--resource`. Explicit command-line flags take
priority over their environment-variable values. The environment values must
be `true` or `false`.
`MOLI_LAYOUT`, `MOLI_RESOURCE`, and `MOLI_BLOCK_PRIVATE_NETWORKS` provide
environment-variable fallbacks for their corresponding command-line flags.
They accept `true`/`false`, `1`/`0`, `yes`/`no`, and `on`/`off`
(case-insensitive). Explicit command-line flags take priority over their
environment-variable values.
Layout is sampled, not continuously retained: a cold geometry request builds
one full pass from the current DOM/style and keeps only the latest
+15 -3
View File
@@ -315,7 +315,11 @@ pub struct CommonArgs {
/// Fetch every optional image, font, audio, video, media, and text-track
/// resource family.
#[arg(long, env = "MOLI_RESOURCE")]
#[arg(
long,
env = "MOLI_RESOURCE",
value_parser = clap::builder::BoolishValueParser::new()
)]
pub resource: bool,
#[arg(long)]
@@ -325,7 +329,11 @@ pub struct CommonArgs {
///
/// Without this flag Moli keeps deterministic compatibility
/// geometry and does not construct layout or paint output.
#[arg(long, env = "MOLI_LAYOUT")]
#[arg(
long,
env = "MOLI_LAYOUT",
value_parser = clap::builder::BoolishValueParser::new()
)]
pub layout: bool,
#[arg(long = "cookie-file")]
@@ -337,7 +345,11 @@ pub struct CommonArgs {
#[arg(long)]
pub document_start_script_file: Vec<String>,
#[arg(long, env = "MOLI_BLOCK_PRIVATE_NETWORKS")]
#[arg(
long,
env = "MOLI_BLOCK_PRIVATE_NETWORKS",
value_parser = clap::builder::BoolishValueParser::new()
)]
pub block_private_networks: bool,
#[arg(long)]
+14 -9
View File
@@ -987,17 +987,20 @@ fn layout_selects_on_demand_policy_for_fetch_and_serve() {
#[test]
fn env_flags_are_fallbacks_and_cli_flags_take_priority() {
for (case, layout, resource) in [
("env-enables", "true", "true"),
("env-disables", "false", "false"),
("cli-overrides-env", "false", "false"),
for (case, env_value, expected) in [
("env-enables", "true", true),
("env-disables", "false", false),
("env-one-enables", "1", true),
("env-zero-disables", "0", false),
("cli-overrides-env", "0", true),
] {
let output = Command::new(std::env::current_exe().unwrap())
.args(["--exact", "parse_env_flags_in_child_process", "--nocapture"])
.env("MOLI_TEST_ENV_FLAG_CASE", case)
.env("MOLI_LAYOUT", layout)
.env("MOLI_RESOURCE", resource)
.env("MOLI_BLOCK_PRIVATE_NETWORKS", resource)
.env("MOLI_TEST_ENV_FLAG_EXPECTED", expected.to_string())
.env("MOLI_LAYOUT", env_value)
.env("MOLI_RESOURCE", env_value)
.env("MOLI_BLOCK_PRIVATE_NETWORKS", env_value)
.output()
.unwrap();
@@ -1015,6 +1018,10 @@ fn parse_env_flags_in_child_process() {
let Ok(case) = std::env::var("MOLI_TEST_ENV_FLAG_CASE") else {
return;
};
let expected = std::env::var("MOLI_TEST_ENV_FLAG_EXPECTED")
.unwrap()
.parse::<bool>()
.unwrap();
let args = if case == "cli-overrides-env" {
vec![
@@ -1031,8 +1038,6 @@ fn parse_env_flags_in_child_process() {
let Commands::Serve(args) = cli.command else {
panic!("expected serve command");
};
let expected = case != "env-disables";
assert_eq!(args.common.layout, expected);
assert_eq!(args.common.resource, expected);
assert_eq!(args.common.block_private_networks, expected);