clap/examples/04_using_matches.rs
Ed Page 7a59dc3da4 Revert "Automatically read license field from Cargo.toml"
This reverts commit 6898fbde33.

PR #2144 added the `license` field but no consumer has been added since
the (like Issue #1768).  Since this is not ready yet, I am pulling it
from the 3.0 release.

So far, our main route for pulling a feature from the release has
been to put it behind a `unstable-*` feature flag and to create a
stablization tracking issue.  I chose to instead remove the feature
because a write-only field with no effect does not provide values for
people to use in as an early access and so doesn't outweight the cost of
the extra documentation noise and code noise it creates.  Additionally,
keeping an `unstable-` feature around when it has such an unknown path
(and time table) to stalbization feels like it violates YAGNI.  I'm
uncertain how much of this feature we can implement and not create a
legal trap for users because the crate's license is insufficient for the
final artifact's license.  I feel our stabliazation process sshould be
about iteration and collecting user feedback which this doesn't line up
with.

When someone is ready to tackle #1768, it will be easy to revert this
commit and pick up the work again.

Fixes #3001
2021-11-12 09:55:37 -06:00

61 lines
2.3 KiB
Rust

use clap::{App, Arg};
fn main() {
// Once all App settings (including all arguments) have been set, you call get_matches() which
// parses the string provided by the user, and returns all the valid matches to the ones you
// specified.
//
// You can then query the matches struct to get information about how the user ran the program
// at startup.
//
// For this example, let's assume you created an App which accepts three arguments (plus two
// generated by clap), a flag to display debugging information triggered with "-d" or
// "--debug" as well as an option argument which specifies a custom configuration file to use
// triggered with "-c file" or "--config file" or "--config=file" and finally a positional
// argument which is the input file we want to work with, this will be the only required
// argument.
let matches = App::new("MyApp")
.about("Parses an input file to do awesome things")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.arg(
Arg::new("debug")
.about("turn on debugging information")
.short('d')
.long("debug"),
)
.arg(
Arg::new("config")
.about("sets the config file to use")
.short('c')
.long("config")
.takes_value(true),
)
.arg(
Arg::new("input")
.about("the input file to use")
.index(1)
.required(true),
)
.get_matches();
// We can find out whether or not debugging was turned on
if matches.is_present("debug") {
println!("Debugging is turned on");
}
// If we wanted to do some custom initialization based off some configuration file
// provided by the user, we could get the file (A string of the file)
if let Some(ref file) = matches.value_of("config") {
println!("Using config file: {}", file);
}
// Because "input" is required we can safely call unwrap() because had the user NOT
// specified a value, clap would have explained the error the user, and exited.
println!(
"Doing real work with file: {}",
matches.value_of("input").unwrap()
);
// Continued program logic goes here...
}