The standard library provides std::env::args()
to get command line arguments, the first value is the name of the program, similar to the way arguments are obtained in other languages:
But in the process of product development, we need more program parameters, and need certain rules and checks, this time we need to use some other libraries to parse these parameters, such as the structopt library.
structopt
can easily parse command line arguments into a struct.
Here is an official example, which can be tested with cargo run -- --help
: cargo run -- --help
.
|
|
We define a struct: to hold the command line arguments: Opt
, which is defined using macros to define some properties of the arguments.
Then with a single line of code let opt = Opt::from_args();
we can resolve the command line arguments to an instance of Opt
.
The official library provides a lot of examples that can be used to understand and learn the functions and usage of structopt.
structopt
uses parsing as an argument, but by way of macros, which greatly simplifies the use of clap.
First let’s see what structopt
does to the above example by means of macros.
structopt
macro
structopt
implements the structopt::StructOpt
trait for Opt
.
|
|
fn clap<'a, 'b>() -> clap::App<'a, 'b>
generates a clap::App
, the name of this App is what we defined basic
, we put no defined about property, so here it takes the comment as about description information, this is the pose used by the clap library, clap application definition Some properties and parameters.
But we use structopt
not to create a clap app, but only to parse the parameters and map them into a struct, so the clap app created here is just an object to help handle the parameters.
The augment_clap(app)
function is also called in the clap()
method, which is defined below and defines the app’s arguments.
fn from_clap(matches: &ArgMatches) -> Self
is mapping the parameters in the clap’s App object to Opt’s fields.
For example the speed
field.
The way to configure the clap app Args is in the function augment_clap
:
The attribute configuration of the arguments is generated according to the definition of each field in Opt.
Thus, when we call let opt = Opt::from_args()
in our code, we actually call from_clap(&Self::clap().get_matches())
.
As a whole, we can see that structopt
actually converts various definitions of macros into clap configurations, and we can learn the complex use of its macros.
Properties
The struct you define will map to clap::App, and the non-subcommand fields of this struct will map to clap::Arg.
This is set via the attribute #[structopt(...)]
is set, so let’s take a look at its properties.
The attributes of structopt can be divided into two categories:
- structopt’s own
magical method
: used by structopt itself,attr = ["whatever"]
orattr(args...)
format raw attributes
: mapped to a method call ofclap::Arg/App
,#[structopt(raw(...))]
format
raw properties/methods
property corresponds to clap::App
/ clap::Arg
one by one.
Format.
#[structopt(method_name = single_arg)]
or #[structopt(method_name(arg1, arg2))]
magical properties/methods
For example, name
, version
, no_version
, author
, about
, short
, long
, rename_all
, parse
, skip
, flatten
, subcommand
.
For details, see: Magical methods.
Type
A number of built-in types are defined, along with corresponding clap methods.
- bool:
.takes_value(false).multiple(false)
。 - Option<T: FromStr>:
.takes_value(true).multiple(false)
。 - Option<Option<T: FromStr»:
.takes_value(true).multiple(false).min_values(0).max_values(1)
。 - Vec<T: FromStr>:
.takes_value(true).multiple(true)
。 - Option<Vec<T: FromStr>:
.takes_values(true).multiple(true).min_values(0)
。 - T: FromStr:
.takes_value(true).multiple(false).required(! has_default)
。
Subcommands
|
|
Custom String Parsing
If the type does not implement the FromStr
trait, or if you just want to customize the parsing method, you can set a custom parsing method.
For more information, see the structureopt documentation doc.rs/structopt.
When developing cli/terminal applications, if you don’t want this declarative way of getting parameters, then you can just use the clap library, which is powerful and widely used.
There are also extensions to structopt based libraries.
- structopt-toml: parsing from toml files
- structopt-flags: a set of reusable flags
paw converts rust main functions into c-style main functions with incoming arguments, which can also be used in combination with structopt:.
|
|
Refurence https://colobu.com/2019/09/29/rust-lib-per-week-structopt/