use std::path::PathBuf;
use clap::{ArgAction, Args, Parser, Subcommand, ValueEnum};
use sokf_core::Output;
#[derive(Debug, Parser)]
#[command(
name = "sokf",
about,
long_about = None,
version,
disable_version_flag = true,
disable_help_flag = true
)]
#[command(
after_help = "Exit codes: 0 if there are no errors, 1 if there are errors, 2 on a usage or internal error.\n\
Docs: https://github.com/six5536/sokf"
)]
pub struct Cli {
#[arg(long, global = true, value_name = "DIR")]
pub repo_root: Option<PathBuf>,
#[arg(long, global = true)]
pub no_cache: bool,
#[command(subcommand)]
pub command: Option<Command>,
#[arg(short = 'V', long = "version", global = true)]
pub version: bool,
#[arg(short = 'h', long = "help", global = true, action = ArgAction::Help)]
pub help: Option<bool>,
}
#[derive(Debug, Subcommand)]
pub enum Command {
Init(InitArgs),
Harness(HarnessArgs),
Validate(ValidateArgs),
Info(InfoArgs),
New(NewArgs),
Graph(GraphArgs),
Locate(LocateArgs),
Guide(GuideArgs),
Completions(CompletionsArgs),
#[command(hide = true)]
Man,
}
impl Command {
pub fn json_schema(&self) -> Option<Output> {
match self {
Command::Init(a) => a.json_schema.then_some(Output::Init),
Command::Harness(a) => match &a.what {
HarnessCommand::Install(a) => a.json_schema.then_some(Output::Harness),
HarnessCommand::Status(a) => a.json_schema.then_some(Output::Harness),
HarnessCommand::Diff(_) | HarnessCommand::Hook(_) => None,
},
Command::Validate(a) => a.json_schema.then_some(Output::Validate),
Command::New(a) => a.json_schema.then_some(Output::New),
Command::Graph(a) => a.json_schema.then_some(Output::Graph),
Command::Locate(a) => a.json_schema.then_some(Output::Locate),
Command::Guide(a) => a.json_schema.then_some(Output::Guide),
Command::Info(a) => match &a.what {
InfoCommand::Formats(a) => a.json_schema.then_some(Output::InfoFormats),
InfoCommand::Grammar => None,
},
Command::Completions(_) | Command::Man => None,
}
}
}
#[derive(Debug, Args)]
pub struct InitArgs {
#[arg(value_name = "DIR")]
pub dir: Option<PathBuf>,
#[arg(long, value_name = "NAME")]
pub name: Option<String>,
#[arg(long, value_name = "SCOPE")]
pub scope: Option<String>,
#[arg(long, value_name = "TEXT")]
pub description: Option<String>,
#[arg(long)]
pub json: bool,
#[arg(long, exclusive = true)]
pub json_schema: bool,
}
#[derive(Debug, Args)]
pub struct HarnessArgs {
#[command(subcommand)]
pub what: HarnessCommand,
}
#[derive(Debug, Subcommand)]
pub enum HarnessCommand {
Install(HarnessInstallArgs),
Status(HarnessStatusArgs),
Diff(HarnessDiffArgs),
#[command(hide = true)]
Hook(HarnessHookArgs),
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, ValueEnum)]
pub enum HarnessScopeArg {
#[default]
Project,
User,
Local,
}
#[derive(Debug, Args)]
pub struct HarnessInstallArgs {
#[arg(value_name = "HARNESS", required = true)]
pub names: Vec<String>,
#[arg(long, value_enum, value_name = "SCOPE", default_value_t)]
pub scope: HarnessScopeArg,
#[arg(long, value_name = "PART", value_delimiter = ',', action = ArgAction::Append)]
pub force: Vec<String>,
#[arg(long)]
pub json: bool,
#[arg(long, exclusive = true)]
pub json_schema: bool,
}
#[derive(Debug, Args)]
pub struct HarnessStatusArgs {
#[arg(value_name = "HARNESS", required = true)]
pub names: Vec<String>,
#[arg(long, value_enum, value_name = "SCOPE", default_value_t)]
pub scope: HarnessScopeArg,
#[arg(long)]
pub json: bool,
#[arg(long, exclusive = true)]
pub json_schema: bool,
}
#[derive(Debug, Args)]
pub struct HarnessDiffArgs {
#[arg(value_name = "HARNESS", required = true)]
pub names: Vec<String>,
#[arg(long, value_enum, value_name = "SCOPE", default_value_t)]
pub scope: HarnessScopeArg,
}
#[derive(Debug, Args)]
pub struct HarnessHookArgs {
#[arg(value_name = "HARNESS")]
pub name: String,
#[arg(value_name = "EVENT")]
pub hook: String,
}
#[derive(Debug, Args)]
pub struct ValidateArgs {
#[arg(value_name = "PATHS")]
pub paths: Vec<PathBuf>,
#[arg(long)]
pub fix: bool,
#[arg(long)]
pub json: bool,
#[arg(long, exclusive = true)]
pub json_schema: bool,
#[arg(long, value_name = "EXPR", requires = "json")]
pub query: Option<String>,
#[arg(long)]
pub warnings: bool,
#[arg(long, value_name = "PATH", conflicts_with = "no_cache")]
pub changed: Vec<PathBuf>,
}
#[derive(Debug, Args)]
pub struct GraphArgs {
#[arg(value_name = "START")]
pub start: Vec<String>,
#[arg(long, value_name = "N")]
pub depth: Option<usize>,
#[arg(long)]
pub all: bool,
#[arg(long)]
pub archived: bool,
#[arg(short = 'l', long = "selector", value_name = "EXPR", action = ArgAction::Append)]
pub selector: Vec<String>,
#[arg(long, value_name = "EXPR", action = ArgAction::Append)]
pub edge: Vec<String>,
#[arg(long, value_name = "EXPR", action = ArgAction::Append)]
pub to: Vec<String>,
#[arg(long, value_name = "TPL", conflicts_with_all = ["json", "mermaid"])]
pub format: Option<String>,
#[arg(long, conflicts_with = "mermaid")]
pub json: bool,
#[arg(long, exclusive = true)]
pub json_schema: bool,
#[arg(long, value_name = "EXPR", requires = "json", conflicts_with_all = ["format", "mermaid"])]
pub query: Option<String>,
#[arg(long)]
pub mermaid: bool,
}
#[derive(Debug, Args)]
pub struct LocateArgs {
#[arg(value_name = "ID", required = true)]
pub ids: Vec<String>,
#[arg(long, conflicts_with = "json")]
pub asset: bool,
#[arg(long)]
pub json: bool,
#[arg(long, exclusive = true)]
pub json_schema: bool,
}
#[derive(Debug, Args)]
pub struct GuideArgs {
#[arg(value_name = "ID|PATH", required = true)]
pub documents: Vec<String>,
#[arg(long, value_name = "KEY=STATE", action = ArgAction::Append, value_parser = parse_state)]
pub state: Vec<(String, String)>,
#[arg(long)]
pub json: bool,
#[arg(long, exclusive = true)]
pub json_schema: bool,
#[arg(long, value_name = "EXPR", requires = "json")]
pub query: Option<String>,
}
#[derive(Debug, Args)]
pub struct InfoArgs {
#[command(subcommand)]
pub what: InfoCommand,
}
#[derive(Debug, Subcommand)]
pub enum InfoCommand {
Grammar,
Formats(InfoFormatsArgs),
}
#[derive(Debug, Args)]
pub struct InfoFormatsArgs {
#[arg(long, value_enum, value_name = "LAYER", conflicts_with = "schema")]
pub layer: Option<Layer>,
#[arg(long, value_name = "ID")]
pub schema: Option<String>,
#[arg(long)]
pub json: bool,
#[arg(long, exclusive = true)]
pub json_schema: bool,
#[arg(long, value_name = "EXPR", requires = "json")]
pub query: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Layer {
Embedded,
Repo,
#[value(name = "formats.d")]
FormatsD,
Effective,
}
#[derive(Debug, Args)]
pub struct NewArgs {
#[arg(value_name = "TYPE", required = true)]
pub type_name: Option<String>,
#[arg(long, value_name = "SLUG", conflicts_with = "id")]
pub slug: Option<String>,
#[arg(long, value_name = "ID")]
pub id: Option<String>,
#[arg(long, value_name = "TEXT")]
pub title: Option<String>,
#[arg(long, value_name = "N")]
pub seq: Option<u64>,
#[arg(long, value_name = "KEY=STATE", action = ArgAction::Append, value_parser = parse_state)]
pub state: Vec<(String, String)>,
#[arg(long, value_name = "DIR", conflicts_with = "for_asset")]
pub dir: Option<PathBuf>,
#[arg(long = "for", value_name = "PATH")]
pub for_asset: Option<PathBuf>,
#[arg(long, conflicts_with = "id_only")]
pub write: bool,
#[arg(long, conflicts_with = "id_only")]
pub json: bool,
#[arg(long, exclusive = true)]
pub json_schema: bool,
#[arg(long = "id-only")]
pub id_only: bool,
}
fn parse_state(s: &str) -> Result<(String, String), String> {
match s.split_once('=') {
Some((k, v)) if !k.is_empty() && !v.is_empty() => Ok((k.to_string(), v.to_string())),
_ => Err(format!("`{s}` is not in the form KEY=STATE")),
}
}
#[derive(Debug, Args)]
pub struct CompletionsArgs {
#[arg(value_enum)]
pub shell: clap_complete::Shell,
}
#[cfg(test)]
mod tests;