quicli icon indicating copy to clipboard operation
quicli copied to clipboard

Meta: Collect relevant use cases from the cookbook

Open killercup opened this issue 6 years ago • 11 comments

The Rust Cookbook has a bunch of

simple examples that demonstrate good practices to accomplish common programming tasks, using the crates of the Rust ecosystem.

Many of these examples can work as CLI apps. We should go through them and see if there are some common patterns that we want to offer abstractions for.

killercup avatar Jan 27 '18 13:01 killercup

My first try: I implemented this script to resize JPGs in #23

It uses glob, rayon, and image. I think of these three, glob and rayon are general purpose for CLI tools but dealing with image is more special purpose.

killercup avatar Jan 27 '18 13:01 killercup

I can try implementing a simple archiver app based on the decompress a tarball & compress directory into tarball cookbooks. This will use the flate2 and tar crates.

mattgathu avatar Jan 31 '18 19:01 mattgathu

Sure, go ahead! Feel free to post a link to a repo here. I'd love to hear what surprised you with quicli and what was missing :)

killercup avatar Jan 31 '18 19:01 killercup

I can write a simple nc-like tool emulating Listen on unused port TCP/IP cookbook example :)

deg4uss3r avatar Jan 31 '18 19:01 deg4uss3r

Go right ahead! I'd to get feedback on this :) Even just a gist with a few lines of Rust code and a Cargo.toml work!

killercup avatar Jan 31 '18 19:01 killercup

Here's the main.rs and the Cargo.toml

deg4uss3r avatar Feb 01 '18 17:02 deg4uss3r

Looks good, @deg4uss3r!

This waits for the connection to close before printing, right? I think you could wrap tcp_listener in a std::io::BufReader and then use for line in buffered_tcp_listener.line() to print the lines as they come it. Might be a nice experiment (and a cool example for a tutorial), haven't tested that, though!

By the way: You put these files into the same gist – and have it compile – by telling cargo the explicit path to your main.rs. If you do this:

[[bin]]
path = "main.rs"

you can just drop the two files into the same directory.

I'm pretty sure you can specify the Ipv4Addr type directly in your Cli struct, by the way.

killercup avatar Feb 01 '18 18:02 killercup

I updated the main.rs gist per your suggestions. I like it a lot more now, but it does deviate a bit from the Cookbook example now.

deg4uss3r avatar Feb 04 '18 16:02 deg4uss3r

@killercup feel free to steal ideas from the ergo cookbook examples.

I just pushed all of the major "core crates" and am going to start working on getting the ergo cookbook started.

vitiral avatar Feb 05 '18 01:02 vitiral

I've been writing a simple cli tool using quicli for zip and tar files packing and unpacking:

My thoughts so far:

  • Supporting the use ? in main! is heaven sent! ✅
  • The wildcard import use quicli::prelude::*; IMO hides information. I'm never sure what is available (in scope) and tracking imports is a bit hard. I had to consult the docs a lot! I'd prefer encouraging the use of explicit imports.
  • I hadn't used StructOpt before and it slowed me down figuring how it fits with clap, more so when adding extra argument options e.g. argument x conflicts with argument y. But I'm happy clap 3 will be adopting what StructOpt has done.
  • The getting started guide uses struct Cli to define the arguments and uses it as args in main!. I fumbled a bit before I realised args refers to struct Cli. Maybe renaming struct Cli to struct Args? 🤷‍♂️ (In hindsight this seems so trivial)

mattgathu avatar Feb 17 '18 09:02 mattgathu

Thanks for the great feedback, @mattgathu!

I've been writing a simple cli tool using quicli for zip and tar files packing and unpacking

Cool! Do you have a public repo? I'd love to collect some "quicli in the wild" links… and opened #62 for that :)

The wildcard import use quicli::prelude::*; IMO hides information.

Very good point. This because of two things:

  • I'm lazy (we can fix that)
  • Exporting (proc-)macros is AFAIK only possible with pub foo::* syntax (we can't easily fix that)

I hadn't used StructOpt before and it slowed me down figuring how it fits with clap

Ah, interesting! Do you think it'd make sense to cover more advanced structopt usage in a guide? (How to do subcommands was requested, too.)

The getting started guide uses struct Cli to define the arguments and uses it as args in main!. I fumbled a bit before I realised args refers to struct Cli.

Yeah, sorry, that's not explicitly mentioned I believe. The closure-like syntax in main!(|args: Cli| { … }); was used to suggest that args is of type Cli, but that may not be obvious, especially because you usually don't need type annotations in Rust's closures.

killercup avatar Feb 17 '18 15:02 killercup