Repository: servo/servo
File: ports/servoshell/prefs.rs (Rust)

Code before:
        output_image_path: cmd_args.output.map(|p| p.to_string_lossy().into_owned()),
        exit_after_stable_image: cmd_args.exit,
        userscripts_directory: cmd_args.userscripts,
        experimental_prefs_enabled: cmd_args.enable_experimental_web_platform_features,
        #[cfg(target_env = "ohos")]
        log_filter: cmd_args.log_filter.or_else(|| {
            (!preferences.log_filter.is_empty()).then(|| preferences.log_filter.clone())
        }),
        #[cfg(target_env = "ohos")]
        log_to_file: cmd_args.log_to_file,
        ..Default::default()
    };

    let mut debug_options = DiagnosticsLogging::default();
    for debug_string in cmd_args.debug {
        if let Err(error) = debug_options.extend(debug_string) {
            println!("error: {}", error);
            return ArgumentParsingResult::ErrorParsing;
        }
    }

    // Check if help was requested via env var or -Z flag
    if debug_options.help {
        DiagnosticsLogging::print_debug_options_usage("servo");
        return ArgumentParsingResult::Exit;
    }

    let opts = Opts {
        debug: debug_options,
        time_profiling: cmd_args.profile,
        time_profiler_trace_path: cmd_args
            .profiler_trace_path
            .map(|p| p.to_string_lossy().into_owned()),
        nonincremental_layout: cmd_args.nonincremental_layout,
        user_stylesheets: cmd_args.user_stylesheet,
        hard_fail: cmd_args.hard_fail,
        multiprocess: cmd_args.multiprocess,
        background_hang_monitor: cmd_args.background_hang_monitor,
        sandbox: cmd_args.sandbox,
        random_pipeline_closure_probability: cmd_args.random_pipeline_closure_probability,
        random_pipeline_closure_seed: cmd_args.random_pipeline_closure_seed,
        config_dir: config_dir.clone(),
        shaders_path: cmd_args.shaders,
        certificate_path: cmd_args
            .certificate_path
            .map(|p| p.to_string_lossy().into_owned()),
        ignore_certificate_errors: cmd_args.ignore_certificate_errors,
        unminify_js: cmd_args.unminify_js,
        local_script_source: cmd_args
            .local_script_source
            .map(|p| p.to_string_lossy().into_owned()),

Review comment (refactor):
I would move this exit to `extend_from_string` which can just call https://doc.rust-lang.org/std/process/fn.exit.html.

Code after:
        output_image_path: cmd_args.output.map(|p| p.to_string_lossy().into_owned()),
        exit_after_stable_image: cmd_args.exit,
        userscripts_directory: cmd_args.userscripts,
        experimental_prefs_enabled: cmd_args.enable_experimental_web_platform_features,
        #[cfg(target_env = "ohos")]
        log_filter: cmd_args.log_filter.or_else(|| {
            (!preferences.log_filter.is_empty()).then(|| preferences.log_filter.clone())
        }),
        #[cfg(target_env = "ohos")]
        log_to_file: cmd_args.log_to_file,
        ..Default::default()
    };

    let mut debug_options = DiagnosticsLogging::new();

    // Parse -Z command-line flags.
    for debug_string in cmd_args.debug {
        if let Err(error) = debug_options.extend_from_string(&debug_string) {
            eprintln!("Could not parse debug logging option: {error}");
            return ArgumentParsingResult::ErrorParsing;
        }
    }

    let opts = Opts {
        debug: debug_options,
        time_profiling: cmd_args.profile,
        time_profiler_trace_path: cmd_args
            .profiler_trace_path
            .map(|p| p.to_string_lossy().into_owned()),
        nonincremental_layout: cmd_args.nonincremental_layout,
        user_stylesheets: cmd_args.user_stylesheet,
        hard_fail: cmd_args.hard_fail,
        multiprocess: cmd_args.multiprocess,
        background_hang_monitor: cmd_args.background_hang_monitor,
        sandbox: cmd_args.sandbox,
        random_pipeline_closure_probability: cmd_args.random_pipeline_closure_probability,
        random_pipeline_closure_seed: cmd_args.random_pipeline_closure_seed,
        config_dir: config_dir.clone(),
        shaders_path: cmd_args.shaders,
        certificate_path: cmd_args
            .certificate_path
            .map(|p| p.to_string_lossy().into_owned()),
        ignore_certificate_errors: cmd_args.ignore_certificate_errors,
        unminify_js: cmd_args.unminify_js,
        local_script_source: cmd_args
            .local_script_source
            .map(|p| p.to_string_lossy().into_owned()),
        unminify_css: cmd_args.unminify_css,
        print_pwm: cmd_args.print_pwm,
        force_ipc: cmd_args.force_ipc,
    };