Skip to content

Latest commit

 

History

History
100 lines (67 loc) · 2.47 KB

clap-show-version-number.md

File metadata and controls

100 lines (67 loc) · 2.47 KB
title timestamp author published description tags todo
Clap - show version number of the command line application in Rust
2023-12-25 04:30:01 -0800
szabgab
true
It is quite easy with clap to include the version number of the command-line tool.
clap
command
version
--version
-V
include additional information in the output of --version, just as we have in rustup

It is usually quite useful for command line applications to be able to tell which version they are.

The common way is that they have a command line flag --version and sometimes -V does the same.

Just try

rustup --version

rustup 1.26.0 (5af9b9484 2023-04-05)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.74.1 (a28077b28 2023-12-04)`

or

rustup -V

It is very easy to do this using Clap, we only need to add the following to the struct that defines the parameters:

#[command(version)]

Clap will automatically add a --version and the -V flags to the application and when the user runs the cli tool providing either of those flags it will print the name of the cli tool and the version number taken from Cargo.toml.

Both values coming from the Cargo.toml file.

Cargo.toml

To demonstrate I created a Crate called show-version and added the Clap crate as a dependency including the derive feature:

cargo add clap --features derive

The Cargo.toml looks like this:

{% include file="examples/clap/show-version/Cargo.toml" %}

Full Code

{% include file="examples/clap/show-version/src/main.rs" %}

cargo run -- --version
show-version 1.2.3

As we saw in the article about getting started with command line parsing in Rust we can also run the compiled code directly and then we don't need the -- separator:

$ ./target/debug/show-version -V
show-version 1.2.3

If we move and rename that executable:

mv ./target/debug/show-version qqrq
./qqrq -V
show-version 1.2.3

So this always uses the name that was defined in Cargo.toml unlike the way we printed the usage message in ARGV - command line parameters for simple CLI program where we always print the name of the current executable.

Notes

Actually you can embed the version in the binary without Clap as well.