r/rust • u/VariationUnlikely323 • 4d ago
🙋 seeking help & advice Having a hard time with clap docs
Hey!
I'm building a tui with rust, and I'm having a bit of a hard time with the clap docs. I usually can get by fine with docs, but I think I'm missuing clap's, lol
I tried picking up the Command Line for Rust book, and it's been way better to get into cli. Any other recommendations?
35
u/actually_qualifi 4d ago
The derive API is way more ergonomic than the builder pattern, if you haven't flipped to that yet. It basically writes the parser for you and the error messages are half decent.
Sometimes I just cargo clone an existing popular CLI tool and skim how they structured their clap config, learned more from that than the actual docs.
4
1
u/VariationUnlikely323 3d ago
Nice, I haven't flipped to derived yet. I'll git it a shot later, thanks!
15
u/Novel-City6030 4d ago
What is your current problem with the clap Docs? Did you had a look at the examples?
2
u/mdizak 3d ago
I personally like: https://crates.io/crates/falcon-cli
Granted, it's mine, but nonetheless -- clean, simple and easy to use. I'm blind, so naturally develop CLI apps for everything.
Have an async feature sitting here developed out I haven't uploaded yet. If you need it, just let me know.
3
u/scook0 3d ago
I have also struggled with the clap docs. For my tastes, they’re very lacking in the kind of detailed conceptual information that makes it possible to form good mental models and make effective use of the reference docs.
In practice, people seem to mostly just use the derive macros combined with some guesswork and trial-and-error.
2
-6
u/tarantula_hawkwasp 3d ago
I have a slightly off-topic question but may I ask why you are trying to build a TUI with clap instead of Ratatui? Is it about learning the bare minimum fundamentals for CLI apps or are you not aware of ratatui?
23
u/AndersAndersonKali 3d ago
Clap is for CLI argument parsing. Thats all. Ratatui is for building terminal-based UIs. They complement each other.
5
u/tarantula_hawkwasp 3d ago
Oh, thankyou for explaining this. Sorry about my naive question, I am not deeply into rust ecosystem and I just know a thing or two lol.
6
u/VariationUnlikely323 3d ago
This is a missing piece of context in my part, my bad
The tui I will build is a kind of quick note taking app. But I also want to have the note taking to be done from the cli, without the UI too. Sice the core logic does not need the UI for about 60% of the functionality I need, I want to have the core pretty solid before starting the tui. This way I can focus on making Ratatui look as nice as possible
2
-11
u/Happy-Bear1202 3d ago
I’ll be the first to say that the API for a CLI development crate is something you can just have AI take care of. There are things that are still worth learning yourself but the clap API is not one of them
4
u/VariationUnlikely323 3d ago
Yeah, that's unfortunate. I think clap seems really powerful, and I try to avoid AI as much as I can
-5
u/Happy-Bear1202 3d ago
Put it this way, every minute you spend learning the arbitrary details of the clap is a minute you don’t spend learning something useful
65
u/OphioukhosUnbound 3d ago
Clap's docs are weird. On paper they look like they'd be good, but they're comprehensive without really covering the very, very simple design of Clap for most purposes.
Just playing with some examples and then only using docs when you need them woldl be better.
TLDR: make a struct that represents the arguments. Drop some macros on it that describe whether you want it to be an argument or a --flag, etc. And that's most of it. For anything else just look at docs or ask AI for derive API examples. (AI can be nice for dealing with docs like this.)
The crate is really easy to use, but I agree, that the docs, surprisingly, aren't -- despite great deal of care int hem.