Switch subcommand parsing to an enum Subcommand#666
Open
mkeeter wants to merge 3 commits into
Open
Conversation
8b3da22 to
f3e41ab
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
(staged on #661)
tl;dr
This PR adds an
enum Subcommandcontaining every subcommand's arguments, so that all of our command parsing is done by the outer#[derive(Parser)].How things stand
Subcommands are accumulated by the
humility-bin'sbuild.rs. In current code, every command crate has to define afn init(), e.g.The build script uses
cargo-metadatato find allhumility-cmd-*dependencies ofhumility-bin, then constructs adcmdsfunction which returns a table of subcommands:Then, we dispatch based on command name; the first thing that every command's
initfunction has to do is to parse its arguments from thecmd: Vec<String>trailing argument:Because these subcommands are dispatched by our code (and not known at
#[derive(Parser)]time), we have to engage in additional shenanigans to get the command docstrings properly plumbed through to Clap.Let's just not do that
After this PR, every subcommand is required to define a
pub type Args(replacing thepub fn init()). This object must implement a newHumilitySubcommandtrait, e.g.We still use a
build.rsto iterate over subcommands, but now we produce apub enum Subcommand:This is embedded verbatim in the CLI argument object of
humility-bin. With this organization, theclapparser Just Works™. We also generate apub fn dispatchwhich takes theenumand calls the appropriate runner function with theArgsvariant as its first argument (removing the subargs parsing from the function itself).Other changes
humility_cmd::Command, which leftDumperas the only member of that crate, so I renamed it tohumility-hexdumpstd::process::exitin favor of returning anExitCodefrommain. This is still a little unorthodox, but I wanted to exactly preserve our existing CLI output. A few cases ofexitremain, and I'll get to them later