Skip to content

Commit b9aa5f2

Browse files
committed
feat(main): shell completions, clap
1 parent 012f75f commit b9aa5f2

File tree

3 files changed

+140
-21
lines changed

3 files changed

+140
-21
lines changed

Cargo.lock

Lines changed: 77 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ rust-version = "1.69.0"
1313
[dependencies]
1414
ureq = { version = "2.9.6", default-features = false, optional = true }
1515
oorandom = { version = "11.1.3", optional = true }
16+
clap = { version = "4.5.4", features = ["cargo", "help"] }
17+
clap_complete = "4.5.2"
1618

1719
[dev-dependencies]
1820
criterion = "0.5.1"

src/main.rs

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,74 @@
11
#![deny(unsafe_code)]
22

3+
use std::io;
4+
5+
use clap::{
6+
builder::{styling::AnsiColor, Styles},
7+
crate_authors, crate_description, crate_name, crate_version, value_parser, Arg, ArgAction,
8+
Command,
9+
};
10+
use clap_complete::{generate, Generator, Shell};
11+
fn build_cli() -> Command {
12+
Command::new(crate_name!())
13+
.arg_required_else_help(true)
14+
.author(crate_authors!())
15+
.about(crate_description!())
16+
.version(crate_version!())
17+
.long_about(HELP)
18+
.subcommand(Command::new("uaf").about("Run the use-after-free bug"))
19+
.subcommand(
20+
Command::new("bo").about("Run the buffer overflow exploit. Optionally take a shower"),
21+
)
22+
.subcommand(Command::new("transition").about("Safely transmute a Boy to a Girl"))
23+
.subcommand(Command::new("segfault").about("Segfault yourself"))
24+
.subcommand(
25+
Command::new("completions")
26+
.about("Return shell completions")
27+
.arg(
28+
Arg::new("shell")
29+
.action(ArgAction::Set)
30+
.value_parser(value_parser!(Shell))
31+
.required(true),
32+
),
33+
)
34+
.styles(STYLE)
35+
.help_template(TEMPLATE)
36+
}
337
fn main() {
4-
let mut args = std::env::args();
5-
let _program = args.next();
6-
let Some(subcommand) = args.next() else {
7-
println!("{HELP}");
8-
return;
9-
};
10-
11-
match subcommand.as_str() {
38+
let mut command = build_cli();
39+
let matches = build_cli().clone().get_matches();
40+
let subcommand = matches.subcommand().unwrap();
41+
match subcommand.0 {
1242
"uaf" => cve_rs::use_after_free(),
1343
"segfault" => cve_rs::segfault(),
1444
"bo" => cve_rs::buffer_overflow().unwrap(),
1545
"transition" => transmute_demo().unwrap(),
16-
"help" | "--help" | "h" | "-h" | "?" | "-?" => println!("{HELP}"),
17-
other => println!("Error: Unknown command `{other}`.\n{HELP}"),
46+
"completions" => print_completions(
47+
subcommand.1.get_one::<Shell>("shell").copied().unwrap(),
48+
&mut command,
49+
),
50+
_ => unreachable!(),
1851
}
1952
}
2053

54+
fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
55+
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
56+
}
57+
58+
const STYLE: Styles = Styles::styled()
59+
.header(AnsiColor::Yellow.on_default())
60+
.usage(AnsiColor::Green.on_default())
61+
.literal(AnsiColor::Green.on_default())
62+
.placeholder(AnsiColor::Green.on_default());
63+
64+
const TEMPLATE: &str = "\
65+
{before-help}{name} {version}
66+
{author-with-newline}{about-with-newline}
67+
{usage-heading} {usage}
68+
69+
{all-args}{after-help}
70+
";
71+
2172
const HELP: &str = r"
2273
cve-rs: Blazingly fast memory vulnerabilities, written in 100% safe rust.
2374
@@ -27,13 +78,6 @@ cve-rs exploits a soundness hole in lifetimes that lets us cast any lifetime to
2778
See: https://github.com/rust-lang/rust/issues/25860
2879
2980
This program is open-source! View the source for all these exploits here: https://github.com/Speykious/cve-rs
30-
31-
Commands:
32-
help Show this help message.
33-
uaf Run the use-after-free bug.
34-
bo Run the buffer overflow exploit. Optionally take a shower.
35-
transition Safely transmute a Boy to a Girl.
36-
segfault Segfault yourself.
3781
";
3882

3983
#[repr(C)]

0 commit comments

Comments
 (0)