Use structOpt to replace clap

This commit is contained in:
jianfengjiang 2021-09-17 17:35:05 +08:00 committed by Zongmin.Gu
parent 2795b8672f
commit ff986cce1f
3 changed files with 77 additions and 72 deletions

@ -83,7 +83,6 @@ dependencies = [
name = "copy_bom"
version = "0.1.0"
dependencies = [
"clap",
"data-encoding",
"elf",
"env_logger",
@ -94,6 +93,7 @@ dependencies = [
"serde_yaml",
"sha2",
"shellexpand",
"structopt",
]
[[package]]
@ -196,6 +196,15 @@ version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -260,6 +269,30 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.29"
@ -374,6 +407,30 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf9d950ef167e25e0bdb073cf1d68e9ad2795ac826f2f3f59647817cf23c0bfa"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "134d838a2c9943ac3125cf6df165eda53493451b719f3255b2a26b85f772d0ba"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.75"
@ -409,6 +466,12 @@ version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06"
[[package]]
name = "unicode-segmentation"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
[[package]]
name = "unicode-width"
version = "0.1.8"

@ -10,10 +10,10 @@ serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
log = "0.4.14"
env_logger = "0.9.0"
clap = "2.33.3"
sha2 = "0.9.5"
data-encoding = "2.3.2"
lazy_static = "1.4.0"
regex = "1.5.4"
shellexpand = "2.1"
elf = "0.0.10"
structopt = "0.3.23"

@ -7,53 +7,31 @@ extern crate env_logger;
extern crate regex;
extern crate shellexpand;
use bom::Bom;
use clap::{App, Arg, ArgMatches};
use env_logger::Env;
use structopt::StructOpt;
mod bom;
mod error;
mod util;
/// The command line options
#[derive(Debug, Clone)]
/// copy files described in a bom file to a given dest root dir
#[derive(Debug, Clone, StructOpt)]
struct CopyBomOption {
// The top bom file
/// Set the bom file to copy
#[structopt(short = "f", long = "file")]
bom_file: String,
// the root dir where we try to copy files to
/// The dest root dir
#[structopt(long = "root")]
root_dir: String,
// set dry run mode. If this flag is set, no real options will done
/// Dry run mode
#[structopt(long = "dry-run")]
dry_run: bool,
// indicate which dirs to find included bom files
/// Set the paths where to find included bom files
#[structopt(long = "include-dir")]
included_dirs: Vec<String>,
}
impl CopyBomOption {
// use clap to parse command lines options
fn parse_command_line() -> Self {
let arg_matches = read_command_line_options();
// unwrap can never fail
let bom_file = arg_matches
.value_of("bom-file")
.map(|s| s.to_string())
.unwrap();
let root_dir = arg_matches
.value_of("root-dir")
.map(|s| s.to_string())
.unwrap();
let dry_run = arg_matches.is_present("dry-run");
let included_dirs = match arg_matches.values_of("include-dirs") {
None => Vec::new(),
Some(values) => values.into_iter().map(|s| s.to_string()).collect(),
};
CopyBomOption {
bom_file,
root_dir,
dry_run,
included_dirs,
}
}
/// copy files based on command line options
fn copy_files(&self) {
let CopyBomOption {
bom_file,
@ -66,47 +44,11 @@ impl CopyBomOption {
}
}
/// use clap to read command line options
fn read_command_line_options<'a>() -> ArgMatches<'a> {
App::new("copy_bom")
.version("v0.1")
.about("copy files described in a bom file to a given dest root dir")
.arg(
Arg::with_name("bom-file")
.short("f")
.long("file")
.required(true)
.takes_value(true)
.help("Set the bom file to copy"),
)
.arg(
Arg::with_name("root-dir")
.long("root")
.required(true)
.takes_value(true)
.help("The dest root dir"),
)
.arg(
Arg::with_name("dry-run")
.long("dry-run")
.help("Dry run mode"),
)
.arg(
Arg::with_name("include-dirs")
.long("include-dir")
.short("i")
.multiple(true)
.takes_value(true)
.help("Set the paths where to find included bom files"),
)
.get_matches()
}
fn main() {
// the copy_bom log environmental variable
let env = Env::new().filter("OCCLUM_LOG_LEVEL");
env_logger::init_from_env(env);
let copy_bom_option = CopyBomOption::parse_command_line();
let copy_bom_option = CopyBomOption::from_args();
copy_bom_option.copy_files();
}