clap
is a crate that provides a lot of functionality for handling and processing command line arguments.
Here is the basic structure of a CLI tool using clap:
let matches = Command::new("Image Tool")
.version("0.1.0")
.author("Author: James Rutter")
.about("A simple image processing tool")
.arg(Arg::new("input")
.short('i')
.long("input")
.value_name("INPUT_PATH")
.help("Sets the input image file")
.required(false))
.arg(Arg::new("output")
.short('o')
.long("output")
.value_name("OUTPUT_DIR")
.help("Sets the output directory for the processed images")
.required(true))
.arg(Arg::new("prefix")
.short('p')
.long("prefix")
.value_name("PREFIX")
.help("Sets the output image file prefix")
.required(true))
.get_matches();
There is more code, but this tutorial assumes you have a working program at this point and are ready for deploying and production.
cargo build # builds executable in debug mode
cargo build --release # builds optimized executable for production
./target/release/<package_name> [OPTIONS] --output <OUTPUT_DIR> --prefix <PREFIX>
Executables located in the system’s PATH
will be able to be executed from the command line. The install process simply copies the local executable file into one of several directories available on the computer:
/usr/local/bin
/usr/bin
sudo cp ./target/release/<package_name> /usr/local/bin/
which image_processing