Crate Clap
Chester Wyke January 30, 2024 Updated: April 15, 2025 #rust
rust (Series)
Conditional Compilation
Install
Crate Serde
References
Cargo
Toolchain (Nightly)
String Formatting
Time
Crate Tokio
Publish Crate
rustfmt
Single file script
Snippets
CI
Pattern Type State
WASM
Create New Crate
Documentation
JSON
Pattern Builder
Testing
Working with collections of bytes
Thoughts about rust
Are we yet
OnceLock
Crate Actix Web
Stack Overflow
Crate Clap
Crate Poll Promise
Crate Insta
Tips
Create new egui project
Crate CSV
Crate egui
Iterators
Crate Tracing Subscriber
Regex
vscode
Enum Conversion
Macros
lettre
Google APIs
Conditional Compilation
Install
Crate Serde
References
Cargo
Toolchain (Nightly)
String Formatting
Time
Crate Tokio
Publish Crate
rustfmt
Single file script
Snippets
CI
Pattern Type State
WASM
Create New Crate
Documentation
JSON
Pattern Builder
Testing
Working with collections of bytes
Thoughts about rust
Are we yet
OnceLock
Crate Actix Web
Stack Overflow
Crate Clap
Crate Poll Promise
Crate Insta
Tips
Create new egui project
Crate CSV
Crate egui
Iterators
Crate Tracing Subscriber
Regex
vscode
Enum Conversion
Macros
lettre
Google APIs
Source: https://docs.rs/clap/latest/clap/
Setup Steps
An example of project where this was used is zola_chrono but I want to stop including the logging level as part of it and defer to using environment variables as already supported by env_logger.
- Add dependency
cargo add clap -F derive,cargo,wrap_help
- Copy cli.rs and add and remove as needed
- Copy from main.rs as needed
- Testing
Example of cargo subcommand
https://github.com/rust-practice/cargo-leet/
Example of using clap derive
Full details can be found in the clap docs. This is just an short example showing features I use more frequently.
#!/usr/bin/env -S cargo +nightly -Zscript
---cargo
package.edition = "2021" # Desirable to stop warning but not needed
version-control-clean-check =
anyhow = "1.0.94"
clap =
---
use ;
use PathBuf;
use ;
/// Short about text goes here
///
/// This part is only included in the long about text that shows when using --help
Example output:
% ./single_file_script.rs -h
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
Running `/home/one/.cargo/target/b1/bdd0db958a601c/debug/single_file_script -h`
Short about text goes here
Usage: single_file_script [OPTIONS] [FOLDER]
Arguments:
[FOLDER] Specify the root directory or uses current directory if not provided [default: .]
Options:
--allow-dirty Does not return an error for dirty files nor generate the list of said files
--allow-no-vcs This option basically disables checking. If true no checks are done. (Not even if the `path`
exists)
--allow-staged Does not return an error for staged files nor generate the list of said files
-o, --optional-flag A boolean flag that is set to true if the argument is provided
--mode <MODE> Included to give an example for an enum [default: cat] [possible values: bird, cat, dog]
-h, --help Print help (see more with '--help')
-V, --version Print version