Conversation

Jarkko Sakkinen

I rewrote the USB factory program that I've been talking about now in Rust based on my C PoC and the patterns for using io_uring while using it.

Reality hit and there's need to also talk to my program for managing things like USB hubs and ports, and images used as sources for them.

I first did JSON protocol yesterday, but it really makes the ad-hoc testing heavy. It's probably great when you have a product shipped and JSON is spoken by computers to each other but for human on terminal interaction it is a nightmare.

Today I found like ultimate solution for client-side of this: Clap. You need exactly one parser for a simple command-language, and then the commands that are typed are subcommands of that parser. And Clap has rich set of features what those contains.

Stuff coming back from program is still JSON (like listing of stuff) but since it is always data (vs not code + data) I will million great ways to fixup that part :-)

Definitely a trick to my hat of tricks that I will re-use also on other languages to get quickly ad-hoc command languages setup!

#rust #rustlang #clap
1
1
0

@jarkko

I did this once. I used rustyline https://crates.io/crates/rustyline to read commands from the terminal (like libreadline), shell-words https://crates.io/crates/shell-words to tokenize command lines (like the Unix shell does), and clap to parse them.

1
0
0

@jarkko

My only complaint is that there doesn't appear to be any way for a background thread to produce output without mucking up the rustyline prompt.

There is another crate named rustyline-async https://crates.io/crates/rustyline-async that can kinda-sorta do that, though: it has a SharedWriter type with which to write to the terminal without breaking the prompt. Requires an async executor, though, and also requires that *all* terminal output be routed through the SharedWriter (so no using println!).

1
0
0
@argv_minus_one I ended up to approach where all protocol is JSON between the service and cli but inside cli, clap does the translation. Best compromise for my needs and application. That way I can ad-hoc test with nicer syntax and send JSON from programs.
1
0
1
@argv_minus_one I don't about those other crates you mentioned but for this i really just need clap to get the job done.
0
0
1