Skip to content

Commit b79cdf1

Browse files
committed
move usage dumping for readme to mod dump_usage
1 parent 0c15f3e commit b79cdf1

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

crates/cargo-gpu/src/dump_usage.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//! Convenience function for internal use. Dumps all the CLI usage instructions. Useful for
2+
//! updating the README.
3+
4+
use crate::{user_output, Cli};
5+
6+
/// main dump usage function
7+
pub fn dump_full_usage_for_readme() -> anyhow::Result<()> {
8+
use clap::CommandFactory as _;
9+
let mut command = Cli::command();
10+
11+
let mut buffer: Vec<u8> = Vec::default();
12+
command.build();
13+
14+
write_help(&mut buffer, &mut command, 0)?;
15+
user_output!("{}", String::from_utf8(buffer)?);
16+
17+
Ok(())
18+
}
19+
20+
/// Recursive function to print the usage instructions for each subcommand.
21+
fn write_help(
22+
buffer: &mut impl std::io::Write,
23+
cmd: &mut clap::Command,
24+
depth: usize,
25+
) -> anyhow::Result<()> {
26+
if cmd.get_name() == "help" {
27+
return Ok(());
28+
}
29+
30+
let mut command = cmd.get_name().to_owned();
31+
let indent_depth = if depth == 0 || depth == 1 { 0 } else { depth };
32+
let indent = " ".repeat(indent_depth * 4);
33+
writeln!(
34+
buffer,
35+
"\n{}* {}{}",
36+
indent,
37+
command.remove(0).to_uppercase(),
38+
command
39+
)?;
40+
41+
for line in cmd.render_long_help().to_string().lines() {
42+
writeln!(buffer, "{indent} {line}")?;
43+
}
44+
45+
for sub in cmd.get_subcommands_mut() {
46+
writeln!(buffer)?;
47+
write_help(buffer, sub, depth + 1)?;
48+
}
49+
50+
Ok(())
51+
}

crates/cargo-gpu/src/main.rs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@
5050
5151
use anyhow::Context as _;
5252

53+
use crate::dump_usage::dump_full_usage_for_readme;
5354
use build::Build;
5455
use clap::Parser as _;
5556
use install::Install;
5657
use show::Show;
5758

5859
mod build;
5960
mod config;
61+
mod dump_usage;
6062
mod install;
6163
mod install_toolchain;
6264
mod legacy_target_specs;
@@ -209,54 +211,6 @@ fn cache_dir() -> anyhow::Result<std::path::PathBuf> {
209211
})
210212
}
211213

212-
/// Convenience function for internal use. Dumps all the CLI usage instructions. Useful for
213-
/// updating the README.
214-
fn dump_full_usage_for_readme() -> anyhow::Result<()> {
215-
use clap::CommandFactory as _;
216-
let mut command = Cli::command();
217-
218-
let mut buffer: Vec<u8> = Vec::default();
219-
command.build();
220-
221-
write_help(&mut buffer, &mut command, 0)?;
222-
user_output!("{}", String::from_utf8(buffer)?);
223-
224-
Ok(())
225-
}
226-
227-
/// Recursive function to print the usage instructions for each subcommand.
228-
fn write_help(
229-
buffer: &mut impl std::io::Write,
230-
cmd: &mut clap::Command,
231-
depth: usize,
232-
) -> anyhow::Result<()> {
233-
if cmd.get_name() == "help" {
234-
return Ok(());
235-
}
236-
237-
let mut command = cmd.get_name().to_owned();
238-
let indent_depth = if depth == 0 || depth == 1 { 0 } else { depth };
239-
let indent = " ".repeat(indent_depth * 4);
240-
writeln!(
241-
buffer,
242-
"\n{}* {}{}",
243-
indent,
244-
command.remove(0).to_uppercase(),
245-
command
246-
)?;
247-
248-
for line in cmd.render_long_help().to_string().lines() {
249-
writeln!(buffer, "{indent} {line}")?;
250-
}
251-
252-
for sub in cmd.get_subcommands_mut() {
253-
writeln!(buffer)?;
254-
write_help(buffer, sub, depth + 1)?;
255-
}
256-
257-
Ok(())
258-
}
259-
260214
/// Returns a string suitable to use as a directory.
261215
///
262216
/// Created from the spirv-builder source dep and the rustc channel.

0 commit comments

Comments
 (0)