Custom help message when using argh

Help messages generated from argh is nearly useless and very hard to
customize. Fork argh and disable all code for generating help messages.

Use a closure to print the help message when handling EarlyExit.
This commit is contained in:
topjohnwu
2023-07-05 17:05:39 -07:00
parent 5ee6daf126
commit d778b0b0a7
7 changed files with 79 additions and 77 deletions

View File

@ -23,7 +23,6 @@ use base::{
use crate::ramdisk::MagiskCpio;
#[derive(FromArgs)]
#[argh(description = "Manipulate cpio archives; <command> --help for more info.")]
struct CpioCli {
#[argh(subcommand)]
command: CpioCommands,
@ -47,66 +46,42 @@ enum CpioCommands {
}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "test",
description = "Test the cpio's status; return value is 0 or bitwise or-ed of following values: 0x1:Magisk; 0x2:unsupported; 0x4:Sony"
)]
#[argh(subcommand, name = "test")]
struct Test {}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "restore",
description = "Restore ramdisk from ramdisk backup stored within incpio"
)]
#[argh(subcommand, name = "restore")]
struct Restore {}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "patch",
description = "Apply ramdisk patches; configure with env variables: KEEPVERITY KEEPFORCEENCRYPT"
)]
#[argh(subcommand, name = "patch")]
struct Patch {}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "exists",
description = "Return 0 if <entry> exists, otherwise return 1"
)]
#[argh(subcommand, name = "exists")]
struct Exists {
#[argh(positional, arg_name = "entry")]
path: String,
}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "backup",
description = "Create ramdisk backups from <orig>"
)]
#[argh(subcommand, name = "backup")]
struct Backup {
#[argh(positional, arg_name = "orig")]
origin: String,
}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "rm",
description = "Remove <entry>; specify [-r] to remove recursively"
)]
#[argh(subcommand, name = "rm")]
struct Remove {
#[argh(positional, arg_name = "entry")]
path: String,
#[argh(switch, short = 'r', description = "recursive")]
#[argh(switch, short = 'r')]
recursive: bool,
}
#[derive(FromArgs)]
#[argh(subcommand, name = "mv", description = "Move <source> to <dest>")]
#[argh(subcommand, name = "mv")]
struct Move {
#[argh(positional, arg_name = "source")]
from: String,
@ -115,22 +90,14 @@ struct Move {
}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "extract",
description = "Extract <paths[0]> to <paths[1]>, or extract all entries to current directory if <paths> is not given"
)]
#[argh(subcommand, name = "extract")]
struct Extract {
#[argh(positional, greedy)]
paths: Vec<String>,
}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "mkdir",
description = "Create directory <entry> in permissions <mode> (in octal)"
)]
#[argh(subcommand, name = "mkdir")]
struct MakeDir {
#[argh(positional, from_str_fn(parse_mode))]
mode: mode_t,
@ -139,11 +106,7 @@ struct MakeDir {
}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "ln",
description = "Create a symlink to <target> with the name <entry>"
)]
#[argh(subcommand, name = "ln")]
struct Link {
#[argh(positional, arg_name = "entry")]
src: String,
@ -152,11 +115,7 @@ struct Link {
}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "add",
description = "Add <infile> as <entry> in permissions <mode> (in octal); replace <entry> if exists"
)]
#[argh(subcommand, name = "add")]
struct Add {
#[argh(positional, from_str_fn(parse_mode))]
mode: mode_t,
@ -167,18 +126,53 @@ struct Add {
}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "ls",
description = r#"List [<path>] ("/" by default); specifly [-r] to recursively list sub-directories"#
)]
#[argh(subcommand, name = "ls")]
struct List {
#[argh(positional, default = r#"String::from("/")"#)]
path: String,
#[argh(switch, short = 'r', description = "recursive")]
#[argh(switch, short = 'r')]
recursive: bool,
}
fn print_cpio_usage() {
eprintln!(
r#"Usage: magiskboot cpio <incpio> [commands...]
Do cpio commands to <incpio> (modifications are done in-place).
Each command is a single argument; add quotes for each command.
Supported commands:
exists ENTRY
Return 0 if ENTRY exists, else return 1
ls [-r] [PATH]
List PATH ("/" by default); specify [-r] to list recursively
rm [-r] ENTRY
Remove ENTRY, specify [-r] to remove recursively
mkdir MODE ENTRY
Create directory ENTRY with permissions MODE
ln TARGET ENTRY
Create a symlink to TARGET with the name ENTRY
mv SOURCE DEST
Move SOURCE to DEST
add MODE ENTRY INFILE
Add INFILE as ENTRY with permissions MODE; replaces ENTRY if exists
extract [ENTRY OUT]
Extract ENTRY to OUT, or extract all entries to current directory
test
Test the cpio's status
Return value is 0 or bitwise or-ed of following values:
0x1:Magisk 0x2:unsupported 0x4:Sony
patch
Apply ramdisk patches
Configure with env variables: KEEPVERITY KEEPFORCEENCRYPT
backup ORIG
Create ramdisk backups from ORIG
restore
Restore ramdisk from ramdisk backup stored within incpio
"#
)
}
#[repr(C, packed)]
struct CpioHeader {
magic: [u8; 6],
@ -523,11 +517,20 @@ impl Display for CpioEntry {
pub fn cpio_commands(argc: i32, argv: *const *const c_char) -> bool {
fn inner(argc: i32, argv: *const *const c_char) -> LoggedResult<()> {
if argc < 1 {
return Err(log_err!("no arguments"));
return Err(log_err!("No arguments"));
}
let cmds = map_args(argc, argv)?;
if cmds[0] == "--help" {
print_cpio_usage();
exit(0);
}
if argc < 2 {
return Err(log_err!("No commands"));
}
let file = cmds[0];
let mut cpio = if Path::new(file).exists() {
Cpio::load_from_file(file)?
@ -546,12 +549,12 @@ pub fn cpio_commands(argc: i32, argv: *const *const c_char) -> bool {
.collect::<Vec<_>>()
.as_slice(),
)
.early_exit();
.on_early_exit(print_cpio_usage);
match &mut cli.command {
CpioCommands::Test(Test {}) => exit(cpio.test()),
CpioCommands::Restore(Restore {}) => cpio.restore()?,
CpioCommands::Patch(Patch {}) => cpio.patch(),
CpioCommands::Test(_) => exit(cpio.test()),
CpioCommands::Restore(_) => cpio.restore()?,
CpioCommands::Patch(_) => cpio.patch(),
CpioCommands::Exists(Exists { path }) => {
if cpio.exists(path) {
exit(0);