use crate::lang::{LANGUAGE_SPECS, Language};
use crate::target::{Target, TargetAddress};
use anyhow::{Context, Result, bail};
use argh::FromArgs;
use std::path::{Path, PathBuf};
#[derive(Debug, FromArgs)]
#[argh(help_triggers("-h", "--help"))]
pub(crate) struct Cli {
#[argh(switch, short = 'V')]
pub(crate) version: bool,
#[argh(option, long = "output")]
pub(crate) output: Option<PathBuf>,
#[argh(option, long = "readseek-dir")]
pub(crate) readseek_dir: Option<PathBuf>,
#[argh(subcommand)]
pub(crate) command: Option<Command>,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand)]
pub(crate) enum Command {
Detect(DetectCommand),
Read(ReadCommand),
Map(MapCommand),
Check(CheckCommand),
Symbol(SymbolCommand),
Identify(IdentifyCommand),
Def(DefCommand),
Refs(RefsCommand),
Rename(RenameCommand),
Search(SearchCommand),
Init(InitCommand),
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "detect")]
#[argh(help_triggers("-h", "--help"))]
pub(crate) struct DetectCommand {
#[argh(positional)]
pub(crate) target: Option<String>,
#[argh(option)]
pub(crate) stdin: Option<PathBuf>,
#[argh(option, from_str_fn(parse_language))]
pub(crate) language: Option<Language>,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "read")]
#[argh(help_triggers("-h", "--help"))]
pub(crate) struct ReadCommand {
#[argh(positional)]
pub(crate) target: Option<String>,
#[argh(option)]
pub(crate) stdin: Option<PathBuf>,
#[argh(option)]
pub(crate) offset: Option<usize>,
#[argh(option)]
pub(crate) end: Option<usize>,
#[argh(option)]
pub(crate) limit: Option<usize>,
#[argh(option, from_str_fn(parse_language))]
pub(crate) language: Option<Language>,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "map")]
#[argh(help_triggers("-h", "--help"))]
pub(crate) struct MapCommand {
#[argh(positional)]
pub(crate) target: Option<String>,
#[argh(option)]
pub(crate) stdin: Option<PathBuf>,
#[argh(option, from_str_fn(parse_language))]
pub(crate) language: Option<Language>,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "check")]
#[argh(help_triggers("-h", "--help"))]
pub(crate) struct CheckCommand {
#[argh(positional)]
pub(crate) target: Option<String>,
#[argh(option)]
pub(crate) stdin: Option<PathBuf>,
#[argh(option, from_str_fn(parse_language))]
pub(crate) language: Option<Language>,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "symbol")]
#[argh(help_triggers("-h", "--help"))]
pub(crate) struct SymbolCommand {
#[argh(positional)]
pub(crate) target: Option<String>,
#[argh(option)]
pub(crate) stdin: Option<PathBuf>,
#[argh(option)]
pub(crate) line: Option<usize>,
#[argh(option)]
pub(crate) name: Option<String>,
#[argh(option, from_str_fn(parse_language))]
pub(crate) language: Option<Language>,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "identify")]
#[argh(help_triggers("-h", "--help"))]
pub(crate) struct IdentifyCommand {
#[argh(positional)]
pub(crate) target: Option<String>,
#[argh(option)]
pub(crate) stdin: Option<PathBuf>,
#[argh(option)]
pub(crate) line: Option<usize>,
#[argh(option)]
pub(crate) column: Option<usize>,
#[argh(option, from_str_fn(parse_language))]
pub(crate) language: Option<Language>,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "def")]
#[argh(help_triggers("-h", "--help"))]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct DefCommand {
#[argh(positional)]
pub(crate) target: PathBuf,
#[argh(positional)]
pub(crate) name: Option<String>,
#[argh(switch)]
pub(crate) stdin: bool,
#[argh(option, long = "format", default = "crate::output::Format::Json")]
pub(crate) format: crate::output::Format,
#[argh(option, from_str_fn(parse_language))]
pub(crate) language: Option<Language>,
#[argh(switch, short = 'c')]
pub(crate) cached: bool,
#[argh(switch, short = 'o')]
pub(crate) others: bool,
#[argh(switch, short = 'i')]
pub(crate) ignored: bool,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "refs")]
#[argh(help_triggers("-h", "--help"))]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct RefsCommand {
#[argh(positional)]
pub(crate) target: PathBuf,
#[argh(positional)]
pub(crate) name: String,
#[argh(option, long = "format", default = "crate::output::Format::Json")]
pub(crate) format: crate::output::Format,
#[argh(switch)]
pub(crate) scope: bool,
#[argh(option)]
pub(crate) line: Option<usize>,
#[argh(option)]
pub(crate) column: Option<usize>,
#[argh(option, from_str_fn(parse_language))]
pub(crate) language: Option<Language>,
#[argh(switch, short = 'c')]
pub(crate) cached: bool,
#[argh(switch, short = 'o')]
pub(crate) others: bool,
#[argh(switch, short = 'i')]
pub(crate) ignored: bool,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "rename")]
#[argh(help_triggers("-h", "--help"))]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct RenameCommand {
#[argh(positional)]
pub(crate) target: PathBuf,
#[argh(option)]
pub(crate) line: usize,
#[argh(option)]
pub(crate) column: Option<usize>,
#[argh(option, long = "to")]
pub(crate) to: String,
#[argh(option)]
pub(crate) workspace: Option<PathBuf>,
#[argh(switch)]
pub(crate) apply: bool,
#[argh(option, from_str_fn(parse_language))]
pub(crate) language: Option<Language>,
#[argh(switch, short = 'c')]
pub(crate) cached: bool,
#[argh(switch, short = 'o')]
pub(crate) others: bool,
#[argh(switch, short = 'i')]
pub(crate) ignored: bool,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "search")]
#[argh(help_triggers("-h", "--help"))]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct SearchCommand {
#[argh(positional)]
pub(crate) target: PathBuf,
#[argh(positional)]
pub(crate) pattern: String,
#[argh(option, from_str_fn(parse_language))]
pub(crate) language: Option<Language>,
#[argh(switch, short = 'c')]
pub(crate) cached: bool,
#[argh(switch, short = 'o')]
pub(crate) others: bool,
#[argh(switch, short = 'i')]
pub(crate) ignored: bool,
}
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "init")]
#[argh(help_triggers("-h", "--help"))]
pub(crate) struct InitCommand {
#[argh(positional)]
pub(crate) path: Option<PathBuf>,
}
pub(crate) fn parse_language(value: &str) -> std::result::Result<Language, String> {
if let Ok(language) = value.parse::<Language>() {
return Ok(language);
}
let alias = value.to_ascii_lowercase().replace(['-', '_'], "");
LANGUAGE_SPECS
.iter()
.find_map(|spec| {
spec.aliases
.contains(&alias.as_str())
.then_some(spec.language)
})
.ok_or_else(|| format!("unknown language: {value}"))
}
fn parse_input_target_with(
target: Option<&str>,
stdin: Option<&Path>,
parse: fn(&str) -> Result<Target>,
) -> Result<Target> {
if let Some(path) = stdin {
if target.is_some() {
bail!("target cannot be combined with --stdin");
}
return Ok(Target {
path: path.to_path_buf(),
address: None,
});
}
parse(target.context("target required")?)
}
fn parse_target(value: &str) -> Result<Target> {
if value.is_empty() {
bail!("target must not be empty");
}
if let Some((path, suffix)) = value.rsplit_once(':') {
if path.is_empty() {
bail!("target path must not be empty");
}
if suffix.chars().all(|ch| ch.is_ascii_digit()) {
let line = suffix
.parse::<usize>()
.with_context(|| format!("invalid target line: {suffix}"))?;
if line == 0 {
bail!("target line must be greater than zero");
}
return Ok(Target {
path: PathBuf::from(path),
address: Some(TargetAddress::Line(line)),
});
}
if is_line_hash(suffix) {
return Ok(Target {
path: PathBuf::from(path),
address: Some(TargetAddress::Hash(suffix.to_ascii_lowercase())),
});
}
}
Ok(Target {
path: PathBuf::from(value),
address: None,
})
}
fn is_line_hash(value: &str) -> bool {
value.len() == 3 && value.chars().all(|ch| ch.is_ascii_hexdigit())
}
pub(crate) struct InputArgs {
pub(crate) target: Option<String>,
pub(crate) stdin: Option<PathBuf>,
pub(crate) language: Option<Language>,
}
impl InputArgs {
pub(crate) fn to_target(&self) -> Result<Target> {
parse_input_target_with(self.target.as_deref(), self.stdin.as_deref(), parse_target)
}
}