-
Notifications
You must be signed in to change notification settings - Fork 286
Make c2rust just forward args to discovered subcommands
#579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a4219b5
Allow all discovered `c2rust` subcommands and don't do subcommand arg…
kkysen 5956ff5
Deleted old `clap` code I tried that didn't work and moved the failur…
kkysen 3cacb96
Replaced 2 `args.next()`s with 1 `args.nth(1)` to correctly propagate…
kkysen 18e95f5
Simplified control flow logic for the `.strip_prefix`s and used `Path…
kkysen c4e17fd
Refactored things to use more `.filter()`s, `.and_then()`s, and `.map…
kkysen 4ee2582
Replaced the `match` and `continue` with an `if let`.
kkysen 3f58677
Merge branch 'master' into kkysen/c2rust-forward
kkysen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,56 +1,147 @@ | ||
| use clap::{crate_authors, load_yaml, App, AppSettings, SubCommand}; | ||
| use anyhow::anyhow; | ||
| use camino::Utf8Path; | ||
| use clap::{crate_authors, App, AppSettings, Arg}; | ||
| use git_testament::{git_testament, render_testament}; | ||
| use is_executable::IsExecutable; | ||
| use std::borrow::Cow; | ||
| use std::collections::HashMap; | ||
| use std::env; | ||
| use std::ffi::OsStr; | ||
| use std::process::{exit, Command}; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::process; | ||
| use std::process::Command; | ||
| use std::str; | ||
|
|
||
| git_testament!(TESTAMENT); | ||
|
|
||
| fn main() { | ||
| let subcommand_yamls = [load_yaml!("transpile.yaml")]; | ||
| let matches = App::new("C2Rust") | ||
| .version(&*render_testament!(TESTAMENT)) | ||
| .author(crate_authors!(", ")) | ||
| .setting(AppSettings::SubcommandRequiredElseHelp) | ||
| .subcommands( | ||
| subcommand_yamls | ||
| .iter() | ||
| .map(|yaml| SubCommand::from_yaml(yaml)), | ||
| ) | ||
| .get_matches(); | ||
| /// A `c2rust` sub-command. | ||
| struct SubCommand { | ||
| /// The path to the [`SubCommand`]'s executable, | ||
| /// if it was found (see [`Self::find_all`]). | ||
| /// Otherwise [`None`] if it is a known [`SubCommand`] (see [`Self::known`]). | ||
| path: Option<PathBuf>, | ||
| /// The name of the [`SubCommand`], i.e. in `c2rust-{name}`. | ||
| name: Cow<'static, str>, | ||
| } | ||
|
|
||
| let mut os_args = env::args_os(); | ||
| os_args.next(); // Skip executable name | ||
| let arg_name = os_args.next().and_then(|name| name.into_string().ok()); | ||
| match (&arg_name, matches.subcommand_name()) { | ||
| (Some(arg_name), Some(subcommand)) if arg_name == subcommand => { | ||
| invoke_subcommand(subcommand, os_args); | ||
| } | ||
| _ => { | ||
| eprintln!("{:?}", arg_name); | ||
| panic!("Could not match subcommand"); | ||
| impl SubCommand { | ||
| /// Find all [`SubCommand`]s adjacent to the current (`c2rust`) executable. | ||
| /// They are of the form `c2rust-{name}`. | ||
| pub fn find_all() -> anyhow::Result<Vec<Self>> { | ||
| let c2rust = env::current_exe()?; | ||
| let c2rust_name: &Utf8Path = c2rust | ||
| .file_name() | ||
| .map(Path::new) | ||
| .ok_or_else(|| anyhow!("no file name: {}", c2rust.display()))? | ||
| .try_into()?; | ||
| let c2rust_name = c2rust_name.as_str(); | ||
| let dir = c2rust | ||
| .parent() | ||
| .ok_or_else(|| anyhow!("no directory: {}", c2rust.display()))?; | ||
| let mut sub_commands = Vec::new(); | ||
| for entry in dir.read_dir()? { | ||
| let entry = entry?; | ||
| let file_type = entry.file_type()?; | ||
| let path = entry.path(); | ||
| let name = path | ||
| .file_name() | ||
| .and_then(|name| name.to_str()) | ||
| .and_then(|name| name.strip_prefix(c2rust_name)) | ||
| .and_then(|name| name.strip_prefix('-')) | ||
| .map(|name| name.to_owned()) | ||
| .map(Cow::from) | ||
| .filter(|_| file_type.is_file() || file_type.is_symlink()) | ||
| .filter(|_| path.is_executable()); | ||
| if let Some(name) = name { | ||
| sub_commands.push(Self { | ||
| path: Some(path), | ||
| name, | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
| Ok(sub_commands) | ||
| } | ||
|
|
||
| /// Get all known [`SubCommand`]s. These have no [`SubCommand::path`]. | ||
| /// Even if the subcommand executables aren't there, we can still suggest them. | ||
| pub fn known() -> impl Iterator<Item = Self> { | ||
| ["transpile", "instrument", "pdg", "analyze"] | ||
| .into_iter() | ||
| .map(|name| Self { | ||
| path: None, | ||
| name: name.into(), | ||
| }) | ||
| } | ||
|
|
||
| /// Get all known ([`Self::known`]) and actual, found ([`Self::find_all`]) subcommands, | ||
| /// putting the known ones first so that the found ones overwrite them and take precedence. | ||
| pub fn all() -> anyhow::Result<impl Iterator<Item = Self>> { | ||
| Ok(Self::known().chain(Self::find_all()?)) | ||
| } | ||
|
|
||
| pub fn invoke<I, S>(&self, args: I) -> anyhow::Result<()> | ||
| where | ||
| I: IntoIterator<Item = S>, | ||
| S: AsRef<OsStr>, | ||
| { | ||
| let path = self.path.as_ref().ok_or_else(|| { | ||
| anyhow!( | ||
| "known subcommand not found (probably not built): {}", | ||
| self.name | ||
| ) | ||
| })?; | ||
| let status = Command::new(&path).args(args).status()?; | ||
| process::exit(status.code().unwrap_or(1)); | ||
| } | ||
| } | ||
|
|
||
| fn invoke_subcommand<I, S>(subcommand: &str, args: I) | ||
| where | ||
| I: IntoIterator<Item = S>, | ||
| S: AsRef<OsStr>, | ||
| { | ||
| // Assumes the subcommand executable is in the same directory as this driver | ||
| // program. | ||
| let cmd_path = std::env::current_exe().expect("Cannot get current executable path"); | ||
| let mut cmd_path = cmd_path.as_path().canonicalize().unwrap(); | ||
| cmd_path.pop(); // remove current executable | ||
| cmd_path.push(format!("c2rust-{}", subcommand)); | ||
| assert!(cmd_path.exists(), "{:?} is missing", cmd_path); | ||
| exit( | ||
| Command::new(cmd_path.into_os_string()) | ||
| .args(args) | ||
| .status() | ||
| .expect("SubCommand failed to start") | ||
| .code() | ||
| .unwrap_or(-1), | ||
| ); | ||
| fn main() -> anyhow::Result<()> { | ||
| let sub_commands = SubCommand::all()?.collect::<Vec<_>>(); | ||
| let sub_commands = sub_commands | ||
| .iter() | ||
rinon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .map(|cmd| (cmd.name.as_ref(), cmd)) | ||
| .collect::<HashMap<_, _>>(); | ||
|
|
||
| // If the subcommand matches, don't use `clap` at all. | ||
| // | ||
| // I can't seem to get `clap` to pass through all arguments as is, | ||
| // like the ones with hyphens like `--metadata`, | ||
| // even though I've set [`Arg::allow_hyphen_values`]. | ||
| // This is faster anyways. | ||
| // I also tried a single "subcommand" argument with [`Arg::possible_values`], | ||
| // but that had the same problem passing through all arguments as well. | ||
| // | ||
| // Furthermore, doing it this way correctly forwards `--help` through to the subcommand | ||
| // instead of `clap` intercepting it and displaying the top-level `--help`. | ||
| let mut args = env::args_os(); | ||
| let sub_command = args.nth(1); | ||
| let sub_command = sub_command | ||
| .as_ref() | ||
| .and_then(|arg| arg.to_str()) | ||
| .and_then(|name| sub_commands.get(name)); | ||
|
|
||
| if let Some(sub_command) = sub_command { | ||
| return sub_command.invoke(args); | ||
| } | ||
|
|
||
| // If we didn't get a subcommand, then use `clap` for parsing and error/help messages. | ||
| let matches = App::new("C2Rust") | ||
| .version(&*render_testament!(TESTAMENT)) | ||
kkysen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .author(crate_authors!(", ")) | ||
| .settings(&[ | ||
| AppSettings::SubcommandRequiredElseHelp, | ||
| AppSettings::AllowExternalSubcommands, | ||
| ]) | ||
| .subcommands(sub_commands.keys().map(|name| { | ||
| clap::SubCommand::with_name(name).arg( | ||
| Arg::with_name("args") | ||
| .multiple(true) | ||
| .allow_hyphen_values(true), | ||
| ) | ||
| })) | ||
| .get_matches(); | ||
| let sub_command_name = matches | ||
| .subcommand_name() | ||
| .ok_or_else(|| anyhow!("no subcommand"))?; | ||
| sub_commands[sub_command_name].invoke(args) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.