Skip to content

Commit

Permalink
feat(cli): add default-config cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
EdenEast committed Sep 11, 2023
1 parent 95c75a4 commit 4efaf8f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/cmd/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::path::PathBuf;
use clap::{crate_description, crate_version, Subcommand};
use clap::{Args, Parser};

pub const DEFAULT_CONFIG: &str = include_str!("../../config.kdl");

pub const CONFIG_OPTIONS: [&str; 3] = ["depth", "height", "finder"];

const ARG_AFTER_HELP_MSG: &str = "\
Expand Down Expand Up @@ -51,7 +53,11 @@ EXAMPLES:
)]
pub struct Cli {
#[command(subcommand)]
pub command: Cmd,
pub command: Option<Cmd>,

/// Dump the default configuration file to stdout
#[arg(long, default_value_t = false)]
pub default_config: bool,
}

#[derive(Debug, Subcommand)]
Expand Down
25 changes: 18 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,30 @@ fn main() -> Result<()> {
return cmd::Attach::default().use_cwd(&config);
}

let starts_with_long = first.starts_with("--");
let contains_help_or_version = HELP_AND_VERSION_FLAGS.iter().any(|v| *v == first);
if !contains_help_or_version && !VALID_FIRST_OPTIONS.iter().any(|v| *v == first) {
if !contains_help_or_version
&& !starts_with_long
&& !VALID_FIRST_OPTIONS.iter().any(|v| *v == first)
{
args.insert(1, "attach".to_owned());
}
}
None => args.push("attach".to_owned()),
}

match cmd::Cli::parse_from(args).command {
cmd::Cmd::Attach(c) => c.run(),
cmd::Cmd::Jump(c) => c.run(),
cmd::Cmd::Kill(c) => c.run(),
cmd::Cmd::List(c) => c.run(),
cmd::Cmd::Wcmd(c) => c.run(),
let cmd = cmd::Cli::parse_from(args);
if cmd.default_config {
println!("{}", cmd::DEFAULT_CONFIG);
return Ok(());
}

match cmd.command {
Some(cmd::Cmd::Attach(c)) => c.run(),
Some(cmd::Cmd::Jump(c)) => c.run(),
Some(cmd::Cmd::Kill(c)) => c.run(),
Some(cmd::Cmd::List(c)) => c.run(),
Some(cmd::Cmd::Wcmd(c)) => c.run(),
_ => Ok(()),
}
}

0 comments on commit 4efaf8f

Please sign in to comment.