Code changes for clap 4 upgrade

This commit is contained in:
David Smith 2023-05-13 12:24:14 -04:00
parent 66f71b237b
commit 0f943c3e64
No known key found for this signature in database
GPG key ID: 979D8D09D9570EED
4 changed files with 125 additions and 116 deletions

View file

@ -16,6 +16,7 @@ use roc_packaging::cache::{self, RocCacheDir};
use std::fs::{self, FileType};
use std::io;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use target_lexicon::Triple;
#[macro_use]
@ -35,7 +36,7 @@ fn main() -> io::Result<()> {
let exit_code = match matches.subcommand() {
None => {
if matches.is_present(ROC_FILE) {
if matches.contains_id(ROC_FILE) {
build(
&matches,
BuildConfig::BuildAndRunIfNoErrors,
@ -50,7 +51,7 @@ fn main() -> io::Result<()> {
}
}
Some((CMD_RUN, matches)) => {
if matches.is_present(ROC_FILE) {
if matches.contains_id(ROC_FILE) {
build(
matches,
BuildConfig::BuildAndRun,
@ -65,7 +66,7 @@ fn main() -> io::Result<()> {
}
}
Some((CMD_TEST, matches)) => {
if matches.is_present(ROC_FILE) {
if matches.contains_id(ROC_FILE) {
test(matches, Triple::host())
} else {
eprintln!("What .roc file do you want to test? Specify it at the end of the `roc test` command.");
@ -74,7 +75,7 @@ fn main() -> io::Result<()> {
}
}
Some((CMD_DEV, matches)) => {
if matches.is_present(ROC_FILE) {
if matches.contains_id(ROC_FILE) {
build(
matches,
BuildConfig::BuildAndRunIfNoErrors,
@ -89,12 +90,12 @@ fn main() -> io::Result<()> {
}
}
Some((CMD_GLUE, matches)) => {
let input_path = Path::new(matches.value_of_os(ROC_FILE).unwrap());
let output_path = Path::new(matches.value_of_os(GLUE_DIR).unwrap());
let spec_path = Path::new(matches.value_of_os(GLUE_SPEC).unwrap());
let input_path = matches.get_one::<PathBuf>(ROC_FILE).unwrap();
let output_path = matches.get_one::<PathBuf>(GLUE_DIR).unwrap();
let spec_path = matches.get_one::<PathBuf>(GLUE_SPEC).unwrap();
// have the backend supply `roc_alloc` and friends
let backend = match matches.is_present(FLAG_DEV) {
let backend = match matches.get_flag(FLAG_DEV) {
true => CodeGenBackend::Assembly(AssemblyBackendMode::Test),
false => CodeGenBackend::Llvm(LlvmBackendMode::BinaryGlue),
};
@ -108,8 +109,12 @@ fn main() -> io::Result<()> {
}
}
Some((CMD_GEN_STUB_LIB, matches)) => {
let input_path = Path::new(matches.value_of_os(ROC_FILE).unwrap());
let target: Target = matches.value_of_t(FLAG_TARGET).unwrap_or_default();
let input_path = matches.get_one::<PathBuf>(ROC_FILE).unwrap();
let target = matches
.get_one::<String>(FLAG_TARGET)
.map(|s| Target::from_str(s).ok())
.flatten()
.unwrap_or_default();
roc_linker::generate_stub_lib(
input_path,
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
@ -117,11 +122,12 @@ fn main() -> io::Result<()> {
)
}
Some((CMD_BUILD, matches)) => {
let target: Target = matches.value_of_t(FLAG_TARGET).unwrap_or_default();
let link_type = match (
matches.is_present(FLAG_LIB),
matches.is_present(FLAG_NO_LINK),
) {
let target = matches
.get_one::<String>(FLAG_TARGET)
.map(|s| Target::from_str(s).ok())
.flatten()
.unwrap_or_default();
let link_type = match (matches.get_flag(FLAG_LIB), matches.get_flag(FLAG_NO_LINK)) {
(true, false) => LinkType::Dylib,
(true, true) => user_error!("build can only be one of `--lib` or `--no-link`"),
(false, true) => LinkType::None,
@ -139,22 +145,18 @@ fn main() -> io::Result<()> {
Some((CMD_CHECK, matches)) => {
let arena = bumpalo::Bump::new();
let emit_timings = matches.is_present(FLAG_TIME);
let filename = matches.value_of_os(ROC_FILE).unwrap();
let roc_file_path = PathBuf::from(filename);
let threading = match matches
.value_of(roc_cli::FLAG_MAX_THREADS)
.and_then(|s| s.parse::<usize>().ok())
{
let emit_timings = matches.get_flag(FLAG_TIME);
let roc_file_path = matches.get_one::<PathBuf>(ROC_FILE).unwrap();
let threading = match matches.get_one::<usize>(roc_cli::FLAG_MAX_THREADS) {
None => Threading::AllAvailable,
Some(0) => user_error!("cannot build with at most 0 threads"),
Some(1) => Threading::Single,
Some(n) => Threading::AtMost(n),
Some(n) => Threading::AtMost(*n),
};
match check_file(
&arena,
roc_file_path,
roc_file_path.to_owned(),
emit_timings,
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
threading,
@ -203,11 +205,11 @@ fn main() -> io::Result<()> {
Some((CMD_REPL, _)) => Ok(roc_repl_cli::main()),
Some((CMD_EDIT, matches)) => {
match matches
.values_of_os(DIRECTORY_OR_FILES)
.get_many::<OsString>(DIRECTORY_OR_FILES)
.map(|mut values| values.next())
{
Some(Some(os_str)) => {
launch_editor(Some(Path::new(os_str)))?;
Some(Some(os_string)) => {
launch_editor(Some(Path::new(os_string)))?;
}
_ => {
launch_editor(None)?;
@ -218,14 +220,14 @@ fn main() -> io::Result<()> {
Ok(0)
}
Some((CMD_DOCS, matches)) => {
let root_filename = matches.value_of_os(ROC_FILE).unwrap();
let root_path = matches.get_one::<PathBuf>(ROC_FILE).unwrap();
generate_docs_html(PathBuf::from(root_filename));
generate_docs_html(root_path.to_owned());
Ok(0)
}
Some((CMD_FORMAT, matches)) => {
let maybe_values = matches.values_of_os(DIRECTORY_OR_FILES);
let maybe_values = matches.get_many::<OsString>(DIRECTORY_OR_FILES);
let mut values: Vec<OsString> = Vec::new();
@ -241,8 +243,8 @@ fn main() -> io::Result<()> {
}
}
Some(os_values) => {
for os_str in os_values {
values.push(os_str.to_os_string());
for os_string in os_values {
values.push(os_string.to_owned());
}
}
}
@ -255,7 +257,7 @@ fn main() -> io::Result<()> {
roc_files_recursive(os_str.as_os_str(), metadata.file_type(), &mut roc_files)?;
}
let format_mode = match matches.is_present(FLAG_CHECK) {
let format_mode = match matches.get_flag(FLAG_CHECK) {
true => FormatMode::CheckOnly,
false => FormatMode::Format,
};