Skip to content

Commit

Permalink
refactor!: Run scripts using dev run
Browse files Browse the repository at this point in the history
Add a new subcommand `run` for running a script. This removes
arguments for running a script from the top-level command and therefore
allows to add other subcommands in the future without them conflicting
with script names.
  • Loading branch information
einfachIrgendwer0815 committed Sep 21, 2024
1 parent a4d4d50 commit 8787d88
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
31 changes: 18 additions & 13 deletions src/bin/dev/clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ pub fn build_clap_app() -> Command {
.action(ArgAction::SetTrue)
.help("List available scripts in alphabetical order."),
)
.arg(
Arg::new("script_name")
.action(ArgAction::Set)
.value_name("SCRIPT")
.help("Name of the script to execute"),
)
.arg(
Arg::new("args")
.action(ArgAction::Append)
.value_name("ARGS")
.help("Arguments to be passed to the script")
.trailing_var_arg(true)
.allow_hyphen_values(true),
.subcommand(
Command::new("run")
.about("Run a script")
.arg(
Arg::new("script_name")
.action(ArgAction::Set)
.value_name("SCRIPT")
.help("Name of the script to execute")
.required(true),
)
.arg(
Arg::new("args")
.action(ArgAction::Append)
.value_name("ARGS")
.help("Arguments to be passed to the script")
.trailing_var_arg(true)
.allow_hyphen_values(true),
),
)
}
17 changes: 9 additions & 8 deletions src/bin/dev/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ fn main() -> Result<ExitCode, anyhow::Error> {
println!("{script}");
}

Ok(ExitCode::SUCCESS)
} else {
if !matches.contains_id("script_name") {
return Ok(ExitCode::SUCCESS);
}

match matches.subcommand() {
Some(("run", args)) => run(args, config),
_ => {
app.print_help()?;
return Ok(ExitCode::SUCCESS);
Ok(ExitCode::SUCCESS)
}

run(matches, config)
}
}

fn run(matches: ArgMatches, config: Config) -> Result<ExitCode, anyhow::Error> {
fn run(matches: &ArgMatches, config: Config) -> Result<ExitCode, anyhow::Error> {
#[expect(
clippy::unwrap_used,
reason = "That `script_name` exists was checked above."
reason = "`script_name` is a required parameter, its existence is checked by clap."
)]
let script_name = matches.get_one::<String>("script_name").unwrap();
let args = matches
Expand Down

0 comments on commit 8787d88

Please sign in to comment.