use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(name = "modelcontextprotocol", version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Command>,
#[arg(long, value_name = "DIR", num_args = 1..)]
pub filesystem: Option<Vec<PathBuf>>,
#[arg(long)]
pub fetch: bool,
#[arg(long)]
pub memory: bool,
#[arg(long, value_name = "DIR", num_args = 1..)]
pub shell: Option<Vec<PathBuf>>,
#[arg(long, value_name = "PATH")]
pub memory_file: Option<PathBuf>,
#[command(flatten)]
pub fetch_options: FetchOptions,
}
#[derive(Debug, Subcommand)]
pub enum Command {
Filesystem {
#[arg(value_name = "DIR", required = true, num_args = 1..)]
dirs: Vec<PathBuf>,
},
Fetch(FetchOptions),
Memory {
#[arg(long, value_name = "PATH")]
memory_file: Option<PathBuf>,
},
Shell {
#[arg(value_name = "DIR", required = true, num_args = 1..)]
dirs: Vec<PathBuf>,
},
}
#[derive(Debug, Clone, Default, PartialEq, Args)]
pub struct FetchOptions {
#[arg(long)]
pub ignore_robots_txt: bool,
#[arg(long, value_name = "USER_AGENT")]
pub user_agent: Option<String>,
#[arg(long, value_name = "URL")]
pub proxy_url: Option<String>,
}
impl Cli {
pub fn into_command(self) -> Option<Command> {
let has_fetch_options = self.fetch_options != FetchOptions::default();
match (
self.command,
self.filesystem,
self.fetch,
self.memory,
self.shell,
) {
(Some(command), None, false, false, None) => {
if has_fetch_options || self.memory_file.is_some() {
None
} else {
Some(command)
}
}
(None, Some(dirs), false, false, None) => {
if has_fetch_options || self.memory_file.is_some() {
None
} else {
Some(Command::Filesystem { dirs })
}
}
(None, None, true, false, None) => {
if self.memory_file.is_some() {
None
} else {
Some(Command::Fetch(self.fetch_options))
}
}
(None, None, false, true, None) => {
if has_fetch_options {
None
} else {
Some(Command::Memory {
memory_file: self.memory_file,
})
}
}
(None, None, false, false, Some(dirs)) => {
if has_fetch_options || self.memory_file.is_some() {
None
} else {
Some(Command::Shell { dirs })
}
}
_ => None,
}
}
}
pub fn print_usage() {
eprintln!(
"modelcontextprotocol: exactly one server must be selected, and server-specific \
options must belong to the selected server.\n\n\
Usage:\n \
modelcontextprotocol filesystem <DIR> [DIR ...]\n \
modelcontextprotocol fetch [--ignore-robots-txt] [--user-agent <UA>] [--proxy-url <URL>]\n \
modelcontextprotocol memory [--memory-file <PATH>]\n \
modelcontextprotocol shell <DIR> [DIR ...]\n\n\
Equivalent flag forms:\n \
modelcontextprotocol --filesystem <DIR> [DIR ...]\n \
modelcontextprotocol --fetch [--ignore-robots-txt] [--user-agent <UA>] [--proxy-url <URL>]\n \
modelcontextprotocol --memory [--memory-file <PATH>]\n \
modelcontextprotocol --shell <DIR> [DIR ...]"
);
}