Last active
November 27, 2024 13:56
-
-
Save satler-git/b33fe0d7159bbbe5a678d9c18551eec0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(prelude_import)] | |
#[prelude_import] | |
use std::prelude::rust_2021::*; | |
#[macro_use] | |
extern crate std; | |
fn main() { | |
let flags = { | |
pub struct Flags { | |
pub num: u32, | |
pub decrease: bool, | |
} | |
#[automatically_derived] | |
impl ::core::fmt::Debug for Flags { | |
#[inline] | |
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { | |
::core::fmt::Formatter::debug_struct_field2_finish( | |
f, | |
"Flags", | |
"num", | |
&self.num, | |
"decrease", | |
&&self.decrease, | |
) | |
} | |
} | |
impl Flags { | |
#[allow(dead_code)] | |
pub fn from_env_or_exit() -> Self { | |
Self::from_env_or_exit_() | |
} | |
#[allow(dead_code)] | |
pub fn from_env() -> xflags::Result<Self> { | |
Self::from_env_() | |
} | |
#[allow(dead_code)] | |
pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> { | |
Self::from_vec_(args) | |
} | |
} | |
impl Flags { | |
fn from_env_or_exit_() -> Self { | |
Self::from_env_().unwrap_or_else(|err| err.exit()) | |
} | |
fn from_env_() -> xflags::Result<Self> { | |
let mut p = xflags::rt::Parser::new_from_env(); | |
Self::parse_(&mut p) | |
} | |
fn from_vec_(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> { | |
let mut p = xflags::rt::Parser::new(args); | |
Self::parse_(&mut p) | |
} | |
} | |
impl Flags { | |
fn parse_(p_: &mut xflags::rt::Parser) -> xflags::Result<Self> { | |
#![allow(non_snake_case, unused_mut)] | |
let mut decrease = Vec::new(); | |
let mut num = (false, Vec::new()); | |
let mut state_ = 0u8; | |
while let Some(arg_) = p_.pop_flag() { | |
match arg_ { | |
Ok(flag_) => { | |
match (state_, flag_.as_str()) { | |
(0, "--decrease" | "-d") => decrease.push(()), | |
(0, "--help" | "-h") => return Err(p_.help(Self::HELP_)), | |
_ => return Err(p_.unexpected_flag(&flag_)), | |
} | |
} | |
Err(arg_) => { | |
match (state_, arg_.to_str().unwrap_or("")) { | |
(0, _) => { | |
if let (done_ @ false, buf_) = &mut num { | |
buf_.push(p_.value_from_str::<u32>("num", arg_)?); | |
*done_ = true; | |
continue; | |
} | |
return Err(p_.unexpected_arg(arg_)); | |
} | |
_ => return Err(p_.unexpected_arg(arg_)), | |
} | |
} | |
} | |
} | |
Ok(Flags { | |
decrease: p_.optional("--decrease", decrease)?.is_some(), | |
num: p_.required("num", num.1)?, | |
}) | |
} | |
} | |
impl Flags { | |
const HELP_: &'static str = "\ | |
ARGS: | |
<num> | |
num to succ | |
OPTIONS: | |
-d, --decrease | |
Decrease the input number | |
-h, --help | |
Prints help information. | |
"; | |
} | |
Flags::from_env_or_exit() | |
}; | |
if flags.decrease { | |
{ | |
::std::io::_print( | |
format_args!("{0}\n", flags.num.checked_sub(1).unwrap_or_default()), | |
); | |
}; | |
} else { | |
{ | |
::std::io::_print(format_args!("{0}\n", flags.num + 1)); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment