Skip to content

Commit

Permalink
Merge pull request #5405 from dlsmith/i5388
Browse files Browse the repository at this point in the history
Upgrade to clap 4
  • Loading branch information
bhansconnect committed May 15, 2023
2 parents 1fe4aae + dfc365b commit d94b3f5
Show file tree
Hide file tree
Showing 10 changed files with 603 additions and 547 deletions.
880 changes: 467 additions & 413 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -82,7 +82,7 @@ bumpalo = { version = "3.12.0", features = ["collections"] }
bytemuck = { version = "1.13.1", features = ["derive"] }
capstone = { version = "0.11.0", default-features = false }
cgmath = "0.18.0"
clap = { version = "3.2.23", default-features = false, features = ["std", "color", "suggestions"] }
clap = { version = "4.2.7", default-features = false, features = ["std", "color", "suggestions"] }
colored = "2.0.0"
confy = { git = 'https://github.com/rust-cli/confy', features = ["yaml_conf"], default-features = false }
console_error_panic_hook = "0.1.7"
Expand Down
168 changes: 85 additions & 83 deletions crates/cli/src/lib.rs

Large diffs are not rendered by default.

68 changes: 35 additions & 33 deletions crates/cli/src/main.rs
Expand Up @@ -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]
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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.");
Expand All @@ -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,
Expand All @@ -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),
};
Expand All @@ -108,20 +109,25 @@ 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()),
&target.to_triple(),
)
}
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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)?;
Expand All @@ -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();

Expand All @@ -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());
}
}
}
Expand All @@ -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,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/cli_run.rs
Expand Up @@ -51,7 +51,7 @@ mod cli_run {
const OPTIMIZE_FLAG: &str = concatcp!("--", roc_cli::FLAG_OPTIMIZE);
const LINKER_FLAG: &str = concatcp!("--", roc_cli::FLAG_LINKER);
const CHECK_FLAG: &str = concatcp!("--", roc_cli::FLAG_CHECK);
const PREBUILT_PLATFORM: &str = concatcp!("--", roc_cli::FLAG_PREBUILT, "=true");
const PREBUILT_PLATFORM: &str = concatcp!("--", roc_cli::FLAG_PREBUILT);
#[allow(dead_code)]
const TARGET_FLAG: &str = concatcp!("--", roc_cli::FLAG_TARGET);

Expand Down
13 changes: 7 additions & 6 deletions crates/compiler/build/src/program.rs
Expand Up @@ -528,7 +528,7 @@ fn gen_from_mono_module_dev_wasm32<'a>(

let host_bytes = std::fs::read(preprocessed_host_path).unwrap_or_else(|_| {
internal_error!(
"Failed to read host object file {}! Try setting --prebuilt-platform=false",
"Failed to read host object file {}! Try omitting --prebuilt-platform",
preprocessed_host_path.display()
)
});
Expand Down Expand Up @@ -794,7 +794,7 @@ fn build_loaded_file<'a>(
};

// For example, if we're loading the platform from a URL, it's automatically prebuilt
// even if the --prebuilt-platform=true CLI flag wasn't set.
// even if the --prebuilt-platform CLI flag wasn't set.
let is_platform_prebuilt = prebuilt_requested || loaded.uses_prebuilt_platform;

let cwd = app_module_path.parent().unwrap();
Expand Down Expand Up @@ -1036,9 +1036,10 @@ fn build_loaded_file<'a>(
}

fn invalid_prebuilt_platform(prebuilt_requested: bool, preprocessed_host_path: PathBuf) {
let prefix = match prebuilt_requested {
true => "Because I was run with --prebuilt-platform=true, ",
false => "",
let prefix = if prebuilt_requested {
"Because I was run with --prebuilt-platform, "
} else {
""
};

let preprocessed_host_path_str = preprocessed_host_path.to_string_lossy();
Expand All @@ -1057,7 +1058,7 @@ fn invalid_prebuilt_platform(prebuilt_requested: bool, preprocessed_host_path: P
However, it was not there!{}
If you have the platform's source code locally, you may be able to generate it by re-running this command with --prebuilt-platform=false
If you have the platform's source code locally, you may be able to generate it by re-running this command omitting --prebuilt-platform
"#
),
prefix,
Expand Down
9 changes: 4 additions & 5 deletions crates/docs_cli/src/main.rs
@@ -1,5 +1,5 @@
//! Provides a binary that is only used for static build servers.
use clap::{Arg, Command};
use clap::{value_parser, Arg, Command};
use roc_docs::generate_docs_html;
use std::io;
use std::path::PathBuf;
Expand All @@ -12,16 +12,15 @@ fn main() -> io::Result<()> {
.about("Generate documentation for a Roc package")
.arg(
Arg::new(ROC_FILE)
.multiple_values(true)
.help("The package's main .roc file")
.allow_invalid_utf8(true)
.required(false)
.num_args(0..)
.value_parser(value_parser!(PathBuf))
.default_value(DEFAULT_ROC_FILENAME),
)
.get_matches();

// Populate roc_files
generate_docs_html(PathBuf::from(matches.value_of_os(ROC_FILE).unwrap()));
generate_docs_html(matches.get_one::<PathBuf>(ROC_FILE).unwrap().to_owned());

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/packaging/src/cache.rs
Expand Up @@ -44,7 +44,7 @@ fn nixos_error_if_dynamic(url: &str, dest_dir: &Path) {
You can:\n\n\t\
- Download the source of the platform and build it locally, like in this example:\n\t \
https://github.com/roc-lang/roc/blob/main/examples/platform-switching/rocLovesRust.roc.\n\t \
When building your roc application, you can use the flag `--prebuilt-platform=true` to prevent the platform from being rebuilt every time.\n\t \
When building your roc application, you can use the flag `--prebuilt-platform` to prevent the platform from being rebuilt every time.\n\t \
For some graphical platforms you may need to use https://github.com/guibou/nixGL.\n\n\t\
- Contact the author of the platform to ask them to statically link their platform.\n\t \
musl can be used to prevent a dynamic dependency on the systems' libc.\n\t \
Expand Down
4 changes: 1 addition & 3 deletions crates/wasm_interp/src/main.rs
Expand Up @@ -44,16 +44,14 @@ fn main() -> io::Result<()> {

let args_for_app = Arg::new(ARGS_FOR_APP)
.help("Arguments to pass into the WebAssembly app\ne.g. `roc_wasm_interp app.wasm 123 123.45`")
.multiple_values(true)
.takes_value(true);
.num_args(0..);

let app = Command::new("roc_wasm_interp")
.about("Run the given .wasm file")
.arg(flag_function)
.arg(flag_debug)
.arg(flag_hex)
.arg(wasm_file_to_run)
.trailing_var_arg(true)
.arg(args_for_app);

// Parse the command line arguments
Expand Down
2 changes: 1 addition & 1 deletion default.nix
Expand Up @@ -23,7 +23,7 @@ rustPlatform.buildRustPackage {
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"confy-0.5.1" = "sha256-3PQdz9W/uJd4CaUZdwAd2u3JJ100SFAoKLCFE6THRZI=";
"confy-0.5.1" = "sha256-KML/uoze2djsFhYk488QAtauethDaC+0aZ3q56yAhuY=";
"criterion-0.3.5" = "sha256-+FibPQGiR45g28xCHcM0pMN+C+Q8gO8206Wb5fiTy+k=";
"inkwell-0.1.0" = "sha256-1kpvY3naS33B99nuu5ZYhb7mdddAyG+DkbUl/RG1Ptg=";
"plotters-0.3.1" = "sha256-noy/RSjoEPZZbOJTZw1yxGcX5S+2q/7mxnUrzDyxOFw=";
Expand Down

0 comments on commit d94b3f5

Please sign in to comment.