Skip to content

Commit

Permalink
more intuitive arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Aug 15, 2024
1 parent f646062 commit e85ffd4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/command/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use env2::EnvFile;
#[derive(clap::Parser, Debug)]
pub struct Run {
/// The command to run. The command can begin with variables in KEY=VALUE format.
command: Vec<String>,
pub command: Vec<String>,
}

impl Run {
Expand Down
28 changes: 21 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod command;

use std::mem::take;
use std::path::{Path, PathBuf};
use std::str::FromStr;

Expand Down Expand Up @@ -33,6 +34,7 @@ use command::*;
/// modenv check
#[derive(Parser)]
#[command(author, version, about)]
#[command(arg_required_else_help(true))]
pub struct Cli {
#[clap(subcommand)]
command: Option<Command>,
Expand Down Expand Up @@ -60,7 +62,7 @@ pub struct Cli {
#[clap(short, long, global = true)]
env_file: Vec<String>,

pairs: Vec<String>,
subcommand_args: Vec<String>,
}

impl Cli {
Expand Down Expand Up @@ -138,12 +140,24 @@ fn default_envfile() -> PathBuf {
fn main() {
let mut cli = Cli::parse();

let command: Option<Command> = std::mem::replace(&mut cli.command, None);
match command.unwrap_or_else(|| Command::Add(Add {
force: false,
all: false,
pairs: std::mem::replace(&mut cli.pairs, vec![])
})) {
let command: Option<Command> = take(&mut cli.command);
let command = if let Some(c) = command {
c
} else {
let first = cli.subcommand_args.iter().next().unwrap();
if first.contains('=') {
Command::Add(Add {
force: false,
all: false,
pairs: take(&mut cli.subcommand_args),
})
} else {
Command::Run(Run {
command: take(&mut cli.subcommand_args),
})
}
};
match command {
Command::Init(init) => init.run(),
Command::Add(add) => add.run(&cli),
Command::Check(check) => check.run(&cli),
Expand Down

0 comments on commit e85ffd4

Please sign in to comment.