Skip to content

Commit 791d025

Browse files
authored
Merge pull request #21 from tombh/tombh/full-usage-in-readme
Hidden command to dump usage for README
2 parents 4623ccb + 8dadc7e commit 791d025

2 files changed

Lines changed: 173 additions & 1 deletion

File tree

README.md

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Command line tool for building Rust shaders using rust-gpu.
33

44
## Getting Started
55

6-
### installation
6+
### Installation
77
To install the tool ensure you have `rustup`. Then run:
88

99
```
@@ -32,3 +32,133 @@ git clone https://github.com/rust-GPU/shader-crate-template
3232
cd shader-crate-template
3333
cargo gpu build
3434
```
35+
36+
## Usage
37+
38+
```
39+
Commands:
40+
install Install rust-gpu compiler artifacts
41+
build Compile a shader crate to SPIR-V
42+
toml Compile a shader crate according to the `cargo gpu build` parameters found in the given toml file
43+
help Print this message or the help of the given subcommand(s)
44+
45+
Options:
46+
-h, --help
47+
Print help
48+
49+
-V, --version
50+
Print version
51+
52+
53+
* Install
54+
55+
Install rust-gpu compiler artifacts
56+
57+
Usage: cargo-gpu install [OPTIONS]
58+
59+
Options:
60+
--spirv-builder <SPIRV_BUILDER>
61+
spirv-builder dependency, written just like in a Cargo.toml file
62+
63+
[default: "{ git = \"https://github.com/Rust-GPU/rust-gpu.git\" }"]
64+
65+
--rust-toolchain <RUST_TOOLCHAIN>
66+
Rust toolchain channel to use to build `spirv-builder`.
67+
68+
This must match the `spirv_builder` argument.
69+
70+
[default: nightly-2024-04-24]
71+
72+
--force-spirv-cli-rebuild
73+
Force `spirv-builder-cli` and `rustc_codegen_spirv` to be rebuilt
74+
75+
-h, --help
76+
Print help (see a summary with '-h')
77+
78+
79+
* Build
80+
81+
Compile a shader crate to SPIR-V
82+
83+
Usage: cargo-gpu build [OPTIONS]
84+
85+
Options:
86+
--spirv-builder <SPIRV_BUILDER>
87+
spirv-builder dependency, written just like in a Cargo.toml file
88+
89+
[default: "{ git = \"https://github.com/Rust-GPU/rust-gpu.git\" }"]
90+
91+
--rust-toolchain <RUST_TOOLCHAIN>
92+
Rust toolchain channel to use to build `spirv-builder`.
93+
94+
This must match the `spirv_builder` argument.
95+
96+
[default: nightly-2024-04-24]
97+
98+
--force-spirv-cli-rebuild
99+
Force `spirv-builder-cli` and `rustc_codegen_spirv` to be rebuilt
100+
101+
--shader-crate <SHADER_CRATE>
102+
Directory containing the shader crate to compile
103+
104+
[default: ./]
105+
106+
--shader-target <SHADER_TARGET>
107+
Shader target
108+
109+
[default: spirv-unknown-vulkan1.2]
110+
111+
--no-default-features
112+
Set cargo default-features
113+
114+
--features <FEATURES>
115+
Set cargo features
116+
117+
-o, --output-dir <OUTPUT_DIR>
118+
Path to the output directory for the compiled shaders
119+
120+
[default: ./]
121+
122+
-h, --help
123+
Print help (see a summary with '-h')
124+
125+
126+
* Toml
127+
128+
Compile a shader crate according to the `cargo gpu build` parameters found in the given toml file
129+
130+
Usage: cargo-gpu toml [PATH]
131+
132+
Arguments:
133+
[PATH]
134+
Path to a workspace or package Cargo.toml file.
135+
136+
Must include a [[workspace | package].metadata.rust-gpu.build] section where
137+
arguments to `cargo gpu build` are listed.
138+
139+
Path arguments like `output-dir` and `shader-manifest` must be relative to
140+
the location of the Cargo.toml file.
141+
142+
Example:
143+
144+
```toml
145+
[package.metadata.rust-gpu.build.spirv-builder]
146+
git = "https://github.com/Rust-GPU/rust-gpu.git"
147+
rev = "0da80f8"
148+
149+
[package.metadata.rust-gpu.build]
150+
output-dir = "shaders"
151+
shader-manifest = "shaders/manifest.json"
152+
```
153+
154+
Calling `cargo gpu toml {path/to/Cargo.toml}` with a Cargo.toml that
155+
contains the example above would compile the crate and place the compiled
156+
`.spv` files and manifest in a directory "shaders".
157+
158+
[default: ./Cargo.toml]
159+
160+
Options:
161+
-h, --help
162+
Print help (see a summary with '-h')
163+
164+
```

crates/cargo-gpu/src/main.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ impl Toml {
641641
log::debug!("build parameters: {parameters:#?}");
642642
if let Cli {
643643
command: Command::Build(mut build),
644+
..
644645
} = Cli::parse_from(parameters)
645646
{
646647
log::debug!("build: {build:?}");
@@ -663,6 +664,12 @@ enum Command {
663664
/// Compile a shader crate according to the `cargo gpu build` parameters
664665
/// found in the given toml file.
665666
Toml(Toml),
667+
668+
/// A hidden command that can be used to recursively print out all the subcommand help messages:
669+
/// `cargo gpu dump-usage`
670+
/// Useful for updating the README.
671+
#[clap(hide(true))]
672+
DumpUsage,
666673
}
667674

668675
#[derive(Parser)]
@@ -702,6 +709,40 @@ fn main() {
702709
}
703710
Command::Build(mut build) => build.run(),
704711
Command::Toml(toml) => toml.run(),
712+
Command::DumpUsage => dump_full_usage_for_readme(),
713+
}
714+
}
715+
716+
fn dump_full_usage_for_readme() {
717+
use clap::CommandFactory;
718+
let mut command = Cli::command();
719+
720+
let mut buffer: Vec<u8> = Default::default();
721+
command.build();
722+
723+
write_help(&mut buffer, &mut command, 0);
724+
let buffer = String::from_utf8(buffer).unwrap();
725+
println!("{}", buffer);
726+
}
727+
728+
fn write_help(buffer: &mut impl std::io::Write, cmd: &mut clap::Command, depth: usize) {
729+
if cmd.get_name() == "help" {
730+
return;
731+
}
732+
733+
let mut command = cmd.get_name().to_string();
734+
let _ = writeln!(
735+
buffer,
736+
"\n* {}{}",
737+
command.remove(0).to_uppercase(),
738+
command
739+
);
740+
let _ = writeln!(buffer);
741+
let _ = cmd.write_long_help(buffer);
742+
743+
for sub in cmd.get_subcommands_mut() {
744+
let _ = writeln!(buffer);
745+
write_help(buffer, sub, depth + 1);
705746
}
706747
}
707748

@@ -723,6 +764,7 @@ mod test {
723764
];
724765
if let Cli {
725766
command: Command::Build(build),
767+
..
726768
} = Cli::parse_from(args)
727769
{
728770
assert_eq!(shader_crate, build.shader_crate);

0 commit comments

Comments
 (0)