Command Line Interface (CLI) tools often require input parameters to dictate their behavior.
clap (Command Line Argument Parser) is a feature-rich Rust
library that facilitates the parsing of these arguments in an intuitive manner.
Defining Command Line Arguments
In this snippet, we utilize the clap library to define an Args struct, which will be used to
capture and structure the arguments passed to the application:
Here, the Args struct defines one command-line arguments:
app_tick_rate: Dictates the application’s tick rate.
This is supplied with default values, ensuring that even if the user doesn’t provide this argument,
the application can still proceed with its defaults.
Displaying Version Information
One common convention in CLIs is the ability to display version information. Here, the version
information is presented as a combination of various parameters, including the Git commit hash.
The version() function, as seen in the snippet, fetches this information:
This function also makes use of an environment variable RATATUI_TEMPLATE_GIT_INFO to derive the
Git commit hash. The variable can be populated during the build process by build.rs:
By invoking the CLI tool with the --version flag, users will be presented with the version
details, including the authors, commit hash, and the paths to the configuration and data
directories.
The version() function’s output is just an example. You can easily adjust its content by amending
the string template code above.