use clap::{Args, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;
use snip::domain::{FolderFilter, SearchField};
use snip::sort::SortMode;
#[derive(Parser, Debug)]
#[command(
name = "snip",
version,
about = "Filesystem-native snippets for humans, scripts, and AI agents"
)]
pub struct Cli {
#[arg(long, global = true, env = "SNIP_LIBRARY")]
pub library: Option<PathBuf>,
#[arg(long, global = true, value_enum)]
pub output: Option<OutputMode>,
#[arg(long, global = true, value_enum)]
pub color: Option<ColorMode>,
#[command(subcommand)]
pub command: Option<Command>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
pub enum OutputMode {
Human,
Json,
Jsonl,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
pub enum ColorMode {
Auto,
Always,
Never,
}
#[derive(Subcommand, Debug)]
pub enum Command {
#[cfg(feature = "tui")]
Tui,
Config(ConfigArgs),
Init(InitArgs),
Info,
List(FilterArgs),
Search(SearchArgs),
Show(SelectorArgs),
Cat(FragmentSelectorArgs),
Preview(PreviewArgs),
Path(PathArgs),
Open(OpenArgs),
Create(CreateArgs),
Edit(EditArgs),
Fragment(FragmentArgs),
Folder(FolderArgs),
Tag(TagArgs),
Theme(ThemeArgs),
Delete(DeleteArgs),
Trash,
Restore(RestoreArgs),
Purge(PurgeArgs),
Doctor(DoctorArgs),
Organize(OrganizeArgs),
Import(ImportArgs),
Git(GitArgs),
Man(ManArgs),
Completion(CompletionArgs),
}
#[derive(Args, Debug)]
pub struct InitArgs {
pub path: Option<PathBuf>,
#[arg(long)]
pub name: Option<String>,
#[arg(long)]
pub git: bool,
#[arg(long, short = 'y')]
pub yes: bool,
}
#[derive(Args, Debug)]
pub struct FilterArgs {
#[arg(long)]
pub folder: Option<String>,
#[arg(long, requires = "folder")]
pub no_subfolders: bool,
#[arg(long)]
pub tag: Option<String>,
#[arg(long, default_value = "modified")]
pub sort: SortMode,
}
impl FilterArgs {
pub fn folder_filter(&self) -> Option<FolderFilter<'_>> {
self.folder
.as_deref()
.map(|folder| FolderFilter::new(folder, !self.no_subfolders))
}
}
#[derive(Args, Debug)]
pub struct SearchArgs {
pub query: String,
#[arg(long)]
pub regex: bool,
#[arg(long)]
pub folder: Option<String>,
#[arg(long, requires = "folder")]
pub no_subfolders: bool,
#[arg(long)]
pub tag: Option<String>,
#[arg(long = "field")]
pub fields: Vec<SearchField>,
#[arg(long, short = 'C', default_value_t = 0, value_name = "N")]
pub context: usize,
#[arg(long, short = 'm', value_name = "N")]
pub limit: Option<usize>,
}
impl SearchArgs {
pub fn folder_filter(&self) -> Option<FolderFilter<'_>> {
self.folder
.as_deref()
.map(|folder| FolderFilter::new(folder, !self.no_subfolders))
}
}
#[derive(Args, Debug)]
pub struct SelectorArgs {
pub selector: String,
}
#[derive(Args, Debug)]
pub struct FragmentSelectorArgs {
pub selector: String,
#[arg(long)]
pub fragment: Option<String>,
}
#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum RenderArg {
Ansi,
Plain,
Html,
}
#[derive(Args, Debug)]
pub struct PreviewArgs {
pub selector: String,
#[arg(long, value_enum)]
pub render: Option<RenderArg>,
#[arg(long, conflicts_with = "no_pager")]
pub pager: bool,
#[arg(long, conflicts_with = "pager")]
pub no_pager: bool,
}
#[derive(Args, Debug)]
pub struct PathArgs {
pub selector: String,
#[arg(long, conflicts_with_all = ["readme", "fragment"])]
pub metadata: bool,
#[arg(long, conflicts_with_all = ["metadata", "fragment"])]
pub readme: bool,
#[arg(long, conflicts_with_all = ["metadata", "readme"])]
pub fragment: Option<String>,
}
#[derive(Args, Debug)]
pub struct OpenArgs {
pub selector: String,
#[arg(long, conflicts_with_all = ["readme", "fragment"])]
pub metadata: bool,
#[arg(long, conflicts_with_all = ["metadata", "fragment"])]
pub readme: bool,
#[arg(long, conflicts_with_all = ["metadata", "readme"])]
pub fragment: Option<String>,
#[arg(long)]
pub app: Option<String>,
}
#[derive(Args, Debug)]
pub struct CreateArgs {
#[arg(long)]
pub title: String,
#[arg(long)]
pub folder: Option<String>,
#[arg(long = "tag")]
pub tags: Vec<String>,
#[arg(long)]
pub language: Option<String>,
#[arg(long, default_value = "Fragment")]
pub fragment_title: String,
#[arg(long, conflicts_with = "content_file")]
pub content: Option<String>,
#[arg(long)]
pub content_file: Option<String>,
#[arg(long, conflicts_with = "note_file")]
pub note: Option<String>,
#[arg(long)]
pub note_file: Option<String>,
#[arg(long, conflicts_with = "readme_file")]
pub readme: Option<String>,
#[arg(long)]
pub readme_file: Option<String>,
#[arg(long)]
pub pin: bool,
#[arg(long)]
pub lock: bool,
}
#[derive(Args, Debug, Clone)]
pub struct OptimisticArgs {
#[arg(long)]
pub if_hash: Option<String>,
#[arg(long)]
pub force: bool,
}
#[derive(Args, Debug)]
pub struct EditArgs {
pub selector: String,
#[arg(long)]
pub title: Option<String>,
#[arg(long)]
pub folder: Option<String>,
#[arg(long = "tag", conflicts_with = "clear_tags")]
pub tags: Vec<String>,
#[arg(long, conflicts_with = "tags")]
pub clear_tags: bool,
#[arg(long, conflicts_with = "unpin")]
pub pin: bool,
#[arg(long, conflicts_with = "pin")]
pub unpin: bool,
#[arg(long, conflicts_with = "unlock")]
pub lock: bool,
#[arg(long, conflicts_with = "lock")]
pub unlock: bool,
#[arg(long)]
pub fragment: Option<String>,
#[arg(long)]
pub fragment_title: Option<String>,
#[arg(long)]
pub language: Option<String>,
#[arg(long, conflicts_with = "content_file")]
pub content: Option<String>,
#[arg(long)]
pub content_file: Option<String>,
#[arg(long, conflicts_with_all = ["note_file", "clear_note"])]
pub note: Option<String>,
#[arg(long, conflicts_with = "clear_note")]
pub note_file: Option<String>,
#[arg(long, conflicts_with_all = ["note_file", "note"])]
pub clear_note: bool,
#[arg(long, conflicts_with_all = ["readme_file", "clear_readme"])]
pub readme: Option<String>,
#[arg(long, conflicts_with = "clear_readme")]
pub readme_file: Option<String>,
#[arg(long, conflicts_with_all = ["readme_file", "readme"])]
pub clear_readme: bool,
#[arg(long, conflicts_with = "readme_editor")]
pub metadata_editor: bool,
#[arg(long, conflicts_with = "metadata_editor")]
pub readme_editor: bool,
#[arg(long)]
pub note_editor: bool,
#[command(flatten)]
pub optimistic: OptimisticArgs,
}
#[derive(Args, Debug)]
pub struct FragmentArgs {
#[command(subcommand)]
pub command: FragmentCommand,
}
#[derive(Subcommand, Debug)]
pub enum FragmentCommand {
Add(FragmentAddArgs),
Edit(FragmentEditArgs),
Remove(FragmentRemoveArgs),
Reorder(FragmentReorderArgs),
}
#[derive(Args, Debug)]
pub struct FragmentAddArgs {
pub selector: String,
#[arg(long)]
pub title: String,
#[arg(long)]
pub language: Option<String>,
#[arg(long, conflicts_with = "content_file")]
pub content: Option<String>,
#[arg(long)]
pub content_file: Option<String>,
#[arg(long, conflicts_with = "note_file")]
pub note: Option<String>,
#[arg(long)]
pub note_file: Option<String>,
#[command(flatten)]
pub optimistic: OptimisticArgs,
}
#[derive(Args, Debug)]
pub struct FragmentEditArgs {
pub selector: String,
pub fragment: String,
#[arg(long)]
pub title: Option<String>,
#[arg(long)]
pub language: Option<String>,
#[arg(long, conflicts_with = "content_file")]
pub content: Option<String>,
#[arg(long)]
pub content_file: Option<String>,
#[arg(long, conflicts_with_all = ["note_file", "clear_note"])]
pub note: Option<String>,
#[arg(long, conflicts_with = "clear_note")]
pub note_file: Option<String>,
#[arg(long, conflicts_with_all = ["note_file", "note"])]
pub clear_note: bool,
#[command(flatten)]
pub optimistic: OptimisticArgs,
}
#[derive(Args, Debug)]
pub struct FragmentRemoveArgs {
pub selector: String,
pub fragment: String,
#[command(flatten)]
pub optimistic: OptimisticArgs,
}
#[derive(Args, Debug)]
pub struct FragmentReorderArgs {
pub selector: String,
pub fragment: String,
#[arg(long)]
pub position: usize,
#[command(flatten)]
pub optimistic: OptimisticArgs,
}
#[derive(Args, Debug)]
pub struct FolderArgs {
#[command(subcommand)]
pub command: FolderCommand,
}
#[derive(Subcommand, Debug)]
pub enum FolderCommand {
List,
Create { folder: String },
Rename { folder: String, new_name: String },
Move { folder: String, target: String },
Delete { folder: String },
}
#[derive(Args, Debug)]
pub struct TagArgs {
#[command(subcommand)]
pub command: TagCommand,
}
#[derive(Subcommand, Debug)]
pub enum TagCommand {
List,
Rename { tag: String, new_name: String },
Delete { tag: String },
}
#[derive(Args, Debug)]
pub struct DeleteArgs {
pub selector: String,
#[command(flatten)]
pub optimistic: OptimisticArgs,
}
#[derive(Args, Debug)]
pub struct RestoreArgs {
pub selector: String,
#[arg(long)]
pub folder: Option<String>,
}
#[derive(Args, Debug)]
pub struct PurgeArgs {
pub selector: String,
#[arg(long)]
pub yes: bool,
}
#[derive(Args, Debug)]
pub struct DoctorArgs {
#[arg(long)]
pub repair: bool,
}
#[derive(Args, Debug)]
pub struct OrganizeArgs {
#[arg(long)]
pub dry_run: bool,
}
#[derive(Args, Debug)]
pub struct ImportArgs {
#[command(subcommand)]
pub command: ImportCommand,
}
#[derive(Subcommand, Debug)]
pub enum ImportCommand {
Snippetslab {
source: PathBuf,
#[arg(long)]
into: PathBuf,
#[arg(long)]
dry_run: bool,
},
}
#[derive(Args, Debug)]
pub struct GitArgs {
#[command(subcommand)]
pub command: GitCommand,
}
#[derive(Subcommand, Debug)]
pub enum GitCommand {
Status,
Init,
Commit {
#[arg(short, long)]
message: Option<String>,
},
Backup,
Push,
Fetch,
}
#[derive(Args, Debug)]
pub struct ManArgs {
#[command(subcommand)]
pub command: ManCommand,
}
#[derive(Subcommand, Debug)]
pub enum ManCommand {
Path {
#[arg(long)]
prefix: Option<PathBuf>,
},
Install {
#[arg(long)]
prefix: Option<PathBuf>,
#[arg(long)]
force: bool,
},
Uninstall {
#[arg(long)]
prefix: Option<PathBuf>,
},
Show {
page: Option<String>,
},
Generate {
directory: PathBuf,
},
}
#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum CompletionShell {
Bash,
Elvish,
Fish,
Powershell,
Zsh,
}
#[derive(Args, Debug)]
pub struct CompletionArgs {
#[arg(value_enum)]
pub shell: CompletionShell,
}
#[derive(Args, Debug)]
pub struct ConfigArgs {
#[command(subcommand)]
pub command: ConfigCommand,
}
#[derive(Args, Debug)]
pub struct ThemeArgs {
#[command(subcommand)]
pub command: ThemeCommand,
}
#[derive(Subcommand, Debug)]
pub enum ThemeCommand {
List {
#[arg(long, value_enum)]
appearance: Option<AppearanceArg>,
},
Show { name: String },
Check { name: String },
Path,
Export {
name: String,
#[arg(long)]
r#as: Option<String>,
#[arg(long)]
force: bool,
},
Use {
name: String,
#[arg(long, value_enum)]
appearance: Option<AppearanceArg>,
},
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
#[value(rename_all = "lowercase")]
pub enum AppearanceArg {
Light,
Dark,
}
#[derive(Subcommand, Debug)]
pub enum ConfigCommand {
Path,
Show,
Init {
#[arg(long)]
library: Option<PathBuf>,
#[arg(long)]
force: bool,
},
Set {
#[arg(value_enum)]
key: ConfigKey,
value: String,
},
Unset {
#[arg(value_enum)]
key: ConfigKey,
},
}
#[derive(Clone, Copy, Debug, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum ConfigKey {
DefaultLibrary,
Output,
Color,
PreviewRender,
PreviewPager,
Editor,
Pager,
DefaultLanguage,
DefaultFolder,
DefaultTags,
TuiTheme,
TuiLightTheme,
TuiDarkTheme,
TuiSort,
TuiDensity,
TuiLineNumbers,
GitAutoCommitInterval,
GitAutoPush,
GitBackupOnQuit,
}
#[cfg(test)]
mod tests {
use super::Cli;
use clap::CommandFactory;
#[test]
fn clap_command_tree_is_valid() {
Cli::command().debug_assert();
}
}