mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-05 20:40:19 -08:00
Cleanup code for EarlyExit during help triggers
This commit is contained in:
@@ -525,21 +525,15 @@ pub trait DynamicSubCommand: Sized {
|
|||||||
pub struct EarlyExit {
|
pub struct EarlyExit {
|
||||||
/// The output to display to the user of the commandline tool.
|
/// The output to display to the user of the commandline tool.
|
||||||
pub output: String,
|
pub output: String,
|
||||||
/// Status of argument parsing.
|
/// If the early exit is caused by help triggers.
|
||||||
///
|
pub is_help: bool,
|
||||||
/// `Ok` if the command was parsed successfully and the early exit is due
|
|
||||||
/// to a flag like `--help` causing early exit with output.
|
|
||||||
///
|
|
||||||
/// `Err` if the arguments were not successfully parsed.
|
|
||||||
// TODO replace with std::process::ExitCode when stable.
|
|
||||||
pub status: Result<(), ()>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for EarlyExit {
|
impl From<String> for EarlyExit {
|
||||||
fn from(err_msg: String) -> Self {
|
fn from(err_msg: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
output: err_msg,
|
output: err_msg,
|
||||||
status: Err(()),
|
is_help: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -695,7 +689,6 @@ pub fn parse_struct_args(
|
|||||||
mut parse_options: ParseStructOptions<'_>,
|
mut parse_options: ParseStructOptions<'_>,
|
||||||
mut parse_positionals: ParseStructPositionals<'_>,
|
mut parse_positionals: ParseStructPositionals<'_>,
|
||||||
mut parse_subcommand: Option<ParseStructSubCommand<'_>>,
|
mut parse_subcommand: Option<ParseStructSubCommand<'_>>,
|
||||||
help_func: &dyn Fn() -> String,
|
|
||||||
) -> Result<(), EarlyExit> {
|
) -> Result<(), EarlyExit> {
|
||||||
let mut help = false;
|
let mut help = false;
|
||||||
let mut remaining_args = args;
|
let mut remaining_args = args;
|
||||||
@@ -738,8 +731,8 @@ pub fn parse_struct_args(
|
|||||||
|
|
||||||
if help {
|
if help {
|
||||||
Err(EarlyExit {
|
Err(EarlyExit {
|
||||||
output: help_func(),
|
output: String::new(),
|
||||||
status: Ok(()),
|
is_help: true,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -869,7 +862,7 @@ impl ParseStructPositionals<'_> {
|
|||||||
} else {
|
} else {
|
||||||
Err(EarlyExit {
|
Err(EarlyExit {
|
||||||
output: unrecognized_arg(arg),
|
output: unrecognized_arg(arg),
|
||||||
status: Err(()),
|
is_help: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -374,8 +374,6 @@ fn impl_from_args_struct_from_args<'a>(
|
|||||||
|
|
||||||
let help_triggers = get_help_triggers(type_attrs);
|
let help_triggers = get_help_triggers(type_attrs);
|
||||||
|
|
||||||
let help = quote! { String::new() };
|
|
||||||
|
|
||||||
let method_impl = quote_spanned! { impl_span =>
|
let method_impl = quote_spanned! { impl_span =>
|
||||||
fn from_args(__cmd_name: &[&str], __args: &[&str])
|
fn from_args(__cmd_name: &[&str], __args: &[&str])
|
||||||
-> std::result::Result<Self, argh::EarlyExit>
|
-> std::result::Result<Self, argh::EarlyExit>
|
||||||
@@ -405,7 +403,6 @@ fn impl_from_args_struct_from_args<'a>(
|
|||||||
last_is_greedy: #last_positional_is_greedy,
|
last_is_greedy: #last_positional_is_greedy,
|
||||||
},
|
},
|
||||||
#parse_subcommands,
|
#parse_subcommands,
|
||||||
&|| #help,
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let mut #missing_requirements_ident = argh::MissingRequirements::default();
|
let mut #missing_requirements_ident = argh::MissingRequirements::default();
|
||||||
|
|||||||
@@ -84,17 +84,16 @@ impl<T> EarlyExitExt<T> for Result<T, EarlyExit> {
|
|||||||
fn on_early_exit<F: FnOnce()>(self, print_help_msg: F) -> T {
|
fn on_early_exit<F: FnOnce()>(self, print_help_msg: F) -> T {
|
||||||
match self {
|
match self {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(EarlyExit { output, status }) => match status {
|
Err(EarlyExit { output, is_help }) => {
|
||||||
Ok(_) => {
|
if is_help {
|
||||||
print_help_msg();
|
print_help_msg();
|
||||||
exit(0)
|
exit(0)
|
||||||
}
|
} else {
|
||||||
Err(_) => {
|
|
||||||
eprintln!("{output}");
|
eprintln!("{output}");
|
||||||
print_help_msg();
|
print_help_msg();
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user