brandtm / cargo-example Goto Github PK
View Code? Open in Web Editor NEWCargo subcommand to run examples of any published crate easily
License: MIT License
Cargo subcommand to run examples of any published crate easily
License: MIT License
You're maybe already aware of this, but I recently learned about cargo install --examples
. I had looked for easy ways to run examples from crates without manually checking them out and had come across this project. It wasn't until much later that I realized that cargo
could do the heavy lifting of getting the source code and building examples, and I put together this shell script based around it that approximates this crate:
cargo_example() (
if test "$#" -lt 1; then
echo "usage: cargo_example <crate> <example> [...]"
return
fi
set -e
crate="$1"; shift
dir="$HOME/.cargo-examples/$crate"
if ! test -d "$dir/bin"; then
cargo install "$crate" --examples --root "$dir"
fi
if test "$#" -lt 1; then
echo "Available examples for crate '$crate':"
ls "$dir/bin"
return
fi
command="$1"; shift
if ! test -x "$dir/bin/$command"; then
echo "No example '$command' found for crate '$crate' (remove the $dir directory to force a rebuild)"
return 1
fi
"$dir/bin/$command" "$@"
)
There are a few advantages to this approach:
As a downside, cargo does print a warning about adding the custom root directory to your $PATH after installing, but that can probably be worked around.
I mostly wanted to open this issue to to share the knowledge because this project has good Google placement for queries related to running cargo examples. Given that this seems to mostly be a proof of concept I'm not sure whether you think it'd be worth reworking this crate to use cargo install --examples
, but would you at least consider updating this sentence from the README to mention cargo install as an alternative?: "In order to run examples you'll always have to manually clone or download the repository of the crate whose examples you wish to run."
It has to be done this way due to the way that
From skimming the readme I still can't figure out what I need to type to run a crate's example. The readme mentions cargo example ...args
but I don't know how to say which crate or which example to run. The readme also mentions cargo run example ...args
and cargo --example
, neither of which are real commands.
I would expect to see a complete screenshot or console codeblock showing the entire intended usage on some popular crate like rand, as in:
$ cargo example ??? ??? monte-carlo
Compiling rand v0.7.0 (/git/rand)
Finished dev [unoptimized + debuginfo] target(s) in 1.56s
Running `target/debug/examples/monte-carlo`
π is approximately 3.142296
After the example has finished running control is not returned to the shell. The output seems to freeze and you need to hit enter to be able to type again.
This is definitely caused by the run_command
function in main.rs and a side-effect of how the command is invoked. Previously I tried getting the output of the command and just println!()
ing it.
Doing that doesn't cause this bug but has one major downside:
You can't get output from cargo in real-time. This is much worse for usability I think because it will look like nothing is happening until the cargo build process has finished, which can take a while.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.