use clap::{Arg, Command, ArgMatches, ArgAction}; use crate::Path; use std::{io, process}; pub const PATH: Path = Path::Bin; pub fn cli() -> Command { Command::new("hostname") .version(env!("CARGO_PKG_VERSION")) .author("The JeanG3nie ") .about("Prints the name of the current host. The super-user can set the host name by supplying an argument.") .arg( Arg::new("NAME") .help("name to set") ) .arg( Arg::new("STRIP") .help("Removes any domain information from the printed name.") .short('s') .long("strip") .action(ArgAction::SetTrue) ) } pub fn run(matches: &ArgMatches) { if let Some(name) = matches.get_one::("NAME") { if let Err(e) = hostname::set(name) { eprintln!("{e}"); process::exit(1); } } else { match hostname::get() { Ok(hostname) => { if matches.get_flag("STRIP") { println!( "{}", match hostname.to_string_lossy().split('.').next() { Some(c) => c, None => { eprintln!("hostname: missing operand"); process::exit(1); } } ); } else { println!("{}", hostname.to_string_lossy()); } } Err(e) => { eprintln!("{e}"); process::exit(1); } } } }