From 3bfff8449fd0da2bf4ad71b703d68da7d5e91c10 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Wed, 12 Aug 2026 12:27:59 +0800 Subject: [PATCH] Accept numeric values for boolean env flags --- README.md | 9 +++++---- moli/src/cli.rs | 18 +++++++++++++++--- moli/tests/cli.rs | 23 ++++++++++++++--------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 052b33748a..3c9433fc56 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/moli/src/cli.rs b/moli/src/cli.rs index ed146f3d04..0e71d7c70a 100644 --- a/moli/src/cli.rs +++ b/moli/src/cli.rs @@ -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, - #[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)] diff --git a/moli/tests/cli.rs b/moli/tests/cli.rs index 2f97205c47..1f20a6a1e0 100644 --- a/moli/tests/cli.rs +++ b/moli/tests/cli.rs @@ -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::() + .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);