main.rs
mod options;
use options::{Options, Parser};
fn main() {
Options::parse();
}
CLI
with πAn ecosystem quick peek
Grenoble Rust Meetup :: 2024-01-11
Letβs create a new Rust π¦ project for tonight
β― cargo new meetup
Created binary (application) `meetup` package
β― cd meetup
β― cargo add clap --features derive
Updating crates.io index
Adding clap v4.4.11 to dependencies.
Features:
+ color
+ derive
+ error-context
+ help
+ std
+ suggestions
+ usage
[...]
Updating crates.io index
options.rs
main.rs
β― cargo run -- -h
Usage: meetup
Options:
-h, --help Print help
options.rs
main.rs
β― cargo run -- -h
Usage: meetup <NAME>
Arguments:
<NAME>
Options:
-h, --help Print help
β― cargo run -- meetup
Hello meetup
options.rs
β― cargo run -- -h
Usage: meetup <NAME>
Arguments:
<NAME> Name to say hello to π
Options:
-h, --help Print help
options.rs
main.rs
β― cargo run -- -h
Arguments:
[NAME] Name to say hello to π
Options:
-h, --help Print help
β― cargo run -- meetup
Hello meetup
β― cargo run
Hello world
options.rs
β― cargo run -- -h
Usage: meetup [OPTIONS]
Options:
-n, --name <NAME> Name to say hello to π
-h, --help Print help
β― cargo run -- -n meetup
Hello meetup
β― cargo run
Hello world
options.rs
main.rs
β― cargo run -- -h
Usage: meetup [OPTIONS]
Options:
-n, --name <NAME> Name to say hello to π [default: world]
-h, --help Print help
β― cargo run -- -n meetup
Hello meetup
β― cargo run
Hello world
options.rs
main.rs
β― cargo run -- -h
Usage: meetup [OPTIONS]
Options:
-p, --pokemon Did you play a pokemon game ?
-h, --help Print help
β― cargo run -- -p
We have so much in common!
options.rs
main.rs
β― cargo run -- -h
Usage: meetup [OPTIONS] <STARTER>
Arguments:
<STARTER> Life defining choice [possible values: charmander, bulbasaur, squirtle]
Options:
-h, --help Print help
β― cargo run -- charmander
Way to go pal π₯
options.rs
β― cargo run -- -h
Usage: meetup <COMMAND>
Commands:
first Red and Blue cartriges
second Gold and silver cartriges
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
β― cargo run -- first -h
Red and Blue cartriges
Usage: meetup first <STARTER>
Arguments:
<STARTER> First generations's starters [possible values: charmander, bulbasaur, squirtle]
Options:
-h, --help Print help
β― cargo run -- second -h
Gold and silver cartriges
Usage: meetup second <STARTER>
Arguments:
<STARTER> Second generations's starters [possible values: cyndaquil, totodile, chikorita]
Options:
-h, --help Print help
β― cargo add clap_complete
Updating crates.io index
Adding clap_complete v4.4.5 to dependencies.
Features:
- debug
- unstable-doc
- unstable-dynamic
Updating crates.io index
options.rs
main.rs
β― cargo run -- bash > meetup.bash && source meetup.bash
β― ./target/debug/meetup <TAB>
-h bash fish zsh second
--help elvish powershell first help
β― ./target/debug/meetup first <TAB>
-h --help charmander bulbasaur squirtle
β― ./target/debug/meetup second <TAB>
-h --help cyndaquil totodile chikorita
options.rs
build.rs
fn main() -> Result<(), Error> {
println!("cargo:rerun-if-changed=src/options.rs");
if let Ok(directory) = env::var("CARGO_MANIFEST_DIR").as_ref() {
let command = &mut Options::command();
let name = &command.get_name().to_string();
generate_to(Bash, command, name, directory)?;
generate_to(Zsh, command, name, directory)?;
}
Ok(())
}
β― ls -lh
-rw-r--r--@ 1 pcoves staff 6.2K Dec 27 22:38 Cargo.lock
-rw-r--r--@ 1 pcoves staff 327B Dec 27 22:39 Cargo.toml
-rw-r--r--@ 1 pcoves staff 542B Dec 27 22:50 build.rs
drwxr-xr-x@ 4 pcoves staff 128B Dec 27 22:40 src
drwxr-xr-x@ 5 pcoves staff 160B Dec 27 17:46 target
β― cargo build
β― ls -lh
-rw-r--r--@ 1 pcoves staff 6.2K Dec 27 22:38 Cargo.lock
-rw-r--r--@ 1 pcoves staff 327B Dec 27 22:39 Cargo.toml
-rw-r--r--@ 1 pcoves staff 3.1K Dec 27 22:50 _meetup
-rw-r--r--@ 1 pcoves staff 542B Dec 27 22:50 build.rs
-rw-r--r--@ 1 pcoves staff 4.0K Dec 27 22:50 meetup.bash
drwxr-xr-x@ 4 pcoves staff 128B Dec 27 22:40 src
drwxr-xr-x@ 5 pcoves staff 160B Dec 27 17:46 target
β― cargo run -- -h
Usage: meetup [COMMAND]
Commands:
first Red and Blue cartriges
second Gold and silver cartriges
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
β― source meetup.bash
β― ./target/debug/meetup
-h --help first second help
β― cargo add --build clap_mangen
Updating crates.io index
Adding clap_mangen v0.2.16 to build-dependencies.
Features:
- debug
Updating crates.io index
build.rs
use clap::CommandFactory;
use clap_mangen::Man;
use std::{env::var, fs::write, io::Error, path::PathBuf};
include!("./src/options.rs");
fn main() -> Result<(), Error> {
println!("cargo:rerun-if-changed=src/options.rs");
if let Ok(directory) = var("CARGO_MANIFEST_DIR").as_ref() {
let mut buffer: Vec<u8> = Default::default();
Man::new(Options::command()).render(&mut buffer)?;
write(PathBuf::from(directory).join("meetup.1"), buffer)?;
}
Ok(())
}
meetup(1) General Commands Manual
NAME
meetup
SYNOPSIS
meetup [-h|--help] [subcommands]
DESCRIPTION
OPTIONS
-h, --help
Print help
SUBCOMMANDS
meetup-first(1)
Red and Blue cartriges
meetup-second(1)
Gold and silver cartriges
meetup-help(1)
Print this message or the help of the given subcommand(s)
meetup