Skip to content
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

Make c2rust just forward args to discovered subcommands #579

Merged
merged 7 commits into from
Aug 25, 2022

Conversation

kkysen
Copy link
Contributor

@kkysen kkysen commented Aug 4, 2022

This changes c2rust to be a simpler wrapper around the other c2rust-* subcommand.

Instead of c2rust having to know about all the subcommands and their arguments upfront, c2rust $subcommand $args just runs c2rust-$subcommand $args. This allows c2rust instrument to work as before (before #554), while also enabling c2rust analyze and c2rust pdg in the same way. The clap help messages are still preserved for the most part, except for the short help messages for the subcommands. Otherwise, c2rust --help works as before (while also suggesting the new subcommands), and c2rust $subcommand --help works by running c2rust-$subcommand --help (instead of clap intercepting the --help).

The way this is implemented is, first the c2rust binary's directory is searched for executables named c2rust-* to discover subcommands. This is combined with the simple list of known subcommands (["transpile", "instrument", "pdg", "analyze"]) in case they're not discovered properly and we still want to suggest them. Then we check if the first argument is one of these subcommands. If it exists, we invoke it. If it doesn't exist, but is known, we suggest building it, and it doesn't exist and isn't known (or there was no subcommand given), then we run the clap parser and let it handle arg parsing and nice error/help messages. The reason we don't have everything go through clap is that I couldn't figure out a way to have clap just forward all arguments, even ones like --metadata with hyphens (Arg::allow_hyphen_values didn't work), without requiring a leading -- argument.

Copy link
Contributor

@rinon rinon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Can you paste an example of what a bare c2rust invocation with no args looks like now? Would save me pulling this down and building it, thanks.

c2rust/src/main.rs Show resolved Hide resolved
c2rust/src/main.rs Outdated Show resolved Hide resolved
c2rust/src/main.rs Show resolved Hide resolved
c2rust/src/main.rs Outdated Show resolved Hide resolved
c2rust/src/main.rs Outdated Show resolved Hide resolved
c2rust/src/main.rs Show resolved Hide resolved
c2rust/src/main.rs Outdated Show resolved Hide resolved
@kkysen
Copy link
Contributor Author

kkysen commented Aug 23, 2022

❯ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/c2rust`
C2Rust v0.16.0+904 (7bab0a01b 2022-08-04) dirty 2 modifications
The C2Rust Project Developers <c2rust@immunant.com>

USAGE:
    c2rust <SUBCOMMAND>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

SUBCOMMANDS:
    analyze
    help          Prints this message or the help of the given subcommand(s)
    instrument
    pdg
    transpile

…::to_str` over `UtfPath::to_str` since we just want to skip the file, not error on it.
…()`s to make it cleaner and more concise.
@kkysen kkysen merged commit 6e3b50f into master Aug 25, 2022
@kkysen kkysen deleted the kkysen/c2rust-forward branch August 25, 2022 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants