Skip to content

Commit

Permalink
streamline clap subcommand matching
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasLaPiana committed Dec 30, 2023
1 parent b111983 commit 83553ce
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn construct_cli(
pipelines: &Option<Vec<Pipeline>>,
docs: &Option<Vec<Docs>>,
) -> clap::Command {
let mut cli = cli_builder();
let mut cli = cli_builder(true);

// Docs
if let Some(docs) = docs {
Expand All @@ -28,12 +28,12 @@ pub fn construct_cli(
}

/// Construct the CLI
pub fn cli_builder() -> Command {
pub fn cli_builder(strict_subcommands: bool) -> Command {
Command::new("rox")
.about("Rox: The Robust Developer Experience CLI")
.version(crate_version!())
.arg_required_else_help(true)
.allow_external_subcommands(true)
.allow_external_subcommands(!strict_subcommands)
// TODO: Add a "watch" flag to run the command on file changes to a path?
.arg(
Arg::new("roxfile")
Expand Down
7 changes: 2 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RoxResult<T> = Result<T, Box<dyn Error>>;
/// This is required because we might need to
/// dynamically populate the CLI based on this arg
fn get_filepath_arg_value() -> String {
let cli = cli_builder();
let cli = cli_builder(false);
// Get the file arg from the CLI if set
let cli_matches = cli.clone().arg_required_else_help(false).get_matches();
cli_matches.get_one::<String>("roxfile").unwrap().to_owned()
Expand Down Expand Up @@ -116,10 +116,7 @@ pub fn rox() -> RoxResult<()> {
)];
execution_results
}
command => {
println!("'{}' is not a valid subcommand!", command);
std::process::exit(2);
}
_ => unreachable!("Invalid subcommand"),
};
let results = AllResults {
job_name: subcommand_name.to_string(),
Expand Down

0 comments on commit 83553ce

Please sign in to comment.