uu_true/
true.rs

1// This file is part of the uutils coreutils package.
2//
3// For the full copyright and license information, please view the LICENSE
4// file that was distributed with this source code.
5use clap::{Arg, ArgAction, Command};
6use std::{ffi::OsString, io::Write};
7use uucore::error::{UResult, set_exit_code};
8
9use uucore::translate;
10
11#[uucore::main]
12pub fn uumain(args: impl uucore::Args) -> UResult<()> {
13    let mut command = uu_app();
14
15    let args: Vec<OsString> = args.collect();
16    if args.len() > 2 {
17        return Ok(());
18    }
19
20    if let Err(e) = command.try_get_matches_from_mut(args) {
21        let error = match e.kind() {
22            clap::error::ErrorKind::DisplayHelp => command.print_help(),
23            clap::error::ErrorKind::DisplayVersion => {
24                write!(std::io::stdout(), "{}", command.render_version())
25            }
26            _ => Ok(()),
27        };
28
29        if let Err(print_fail) = error {
30            // Try to display this error.
31            let _ = writeln!(std::io::stderr(), "{}: {print_fail}", uucore::util_name());
32            // Mirror GNU options. When failing to print warnings or version flags, then we exit
33            // with FAIL. This avoids allocation some error information which may result in yet
34            // other types of failure.
35            set_exit_code(1);
36        }
37    }
38
39    Ok(())
40}
41
42pub fn uu_app() -> Command {
43    Command::new(uucore::util_name())
44        .version(uucore::crate_version!())
45        .help_template(uucore::localized_help_template(uucore::util_name()))
46        .about(translate!("true-about"))
47        // We provide our own help and version options, to ensure maximum compatibility with GNU.
48        .disable_help_flag(true)
49        .disable_version_flag(true)
50        .arg(
51            Arg::new("help")
52                .long("help")
53                .help(translate!("true-help-text"))
54                .action(ArgAction::Help),
55        )
56        .arg(
57            Arg::new("version")
58                .long("version")
59                .help(translate!("true-version-text"))
60                .action(ArgAction::Version),
61        )
62}