Function uucore::backup_control::determine_backup_mode
source · [−]pub fn determine_backup_mode(matches: &ArgMatches) -> UResult<BackupMode>
Expand description
Determine the “mode” for the backup operation to perform, if any.
Parses the backup options according to the GNU manual, and converts
them to an instance of BackupMode
for further processing.
Takes clap::ArgMatches
as argument which must contain the options
from arguments::backup()
and arguments::backup_no_args()
. Otherwise
the NoBackup
mode is returned unconditionally.
It is recommended for anyone who would like to implement the
backup-functionality to use the arguments prepared in the arguments
submodule (see examples)
Errors
If an argument supplied directly to the long backup
option, or read in
through the VERSION CONTROL
env var is ambiguous (i.e. may resolve to
multiple backup modes) or invalid, an InvalidArgument
or
AmbiguousArgument
error is returned, respectively.
Examples
Here’s how one would integrate the backup mode determination into an application.
#[macro_use]
extern crate uucore;
use uucore::backup_control::{self, BackupMode};
use clap::{Command, Arg, ArgMatches};
fn main() {
let matches = Command::new("command")
.arg(backup_control::arguments::backup())
.arg(backup_control::arguments::backup_no_args())
.get_matches_from(vec![
"command", "-b", "--backup=t"
]);
let backup_mode = backup_control::determine_backup_mode(&matches).unwrap();
assert_eq!(backup_mode, BackupMode::NumberedBackup)
}
This example shows an ambiguous input, as ‘n’ may resolve to 4 different backup modes.
#[macro_use]
extern crate uucore;
use uucore::backup_control::{self, BackupMode, BackupError};
use clap::{Command, Arg, ArgMatches};
fn main() {
let matches = Command::new("command")
.arg(backup_control::arguments::backup())
.arg(backup_control::arguments::backup_no_args())
.get_matches_from(vec![
"command", "-b", "--backup=n"
]);
let backup_mode = backup_control::determine_backup_mode(&matches);
assert!(backup_mode.is_err());
let err = backup_mode.unwrap_err();
// assert_eq!(err, BackupError::AmbiguousArgument);
// Use uucore functionality to show the error to the user
show!(err);
}