cargo_nextest/dispatch/
imp.rs

1// Copyright (c) The nextest Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Main entry point implementation.
5
6use super::{
7    app::CargoNextestApp,
8    clap_error::{EarlySetup, handle_clap_error},
9};
10use clap::{CommandFactory, Parser};
11
12/// Main entry point for cargo-nextest.
13///
14/// This function handles CLI parsing, early setup (for paged help), and
15/// dispatches to the appropriate command. Both the main binary and the
16/// integration test duplicate use this.
17pub fn main_impl() -> ! {
18    let cli_args: Vec<_> = std::env::args_os()
19        .map(|arg| arg.to_string_lossy().into_owned())
20        .collect();
21
22    let app = CargoNextestApp::command();
23    let early_setup = EarlySetup::new(&cli_args, &app);
24
25    match CargoNextestApp::try_parse() {
26        Ok(opts) => {
27            let output = opts.init_output();
28            match opts.exec(cli_args, output, &mut crate::OutputWriter::default()) {
29                Ok(code) => std::process::exit(code),
30                Err(error) => {
31                    error.display_to_stderr(&output.stderr_styles());
32                    std::process::exit(error.process_exit_code())
33                }
34            }
35        }
36        Err(err) => {
37            let code = handle_clap_error(err, &early_setup);
38            std::process::exit(code);
39        }
40    }
41}