Skip to main content

cargo_compatible/
cli.rs

1use clap::{Args, Parser, Subcommand, ValueEnum};
2use std::path::PathBuf;
3
4const AFTER_HELP: &str = "\
5Examples:
6  cargo compatible scan --workspace
7  cargo compatible scan --package my-crate --format json
8  cargo compatible resolve --rust-version 1.70
9  cargo compatible resolve --write-candidate .cargo-compatible/candidate/Cargo.lock
10  cargo compatible apply-lock --candidate-lockfile .cargo-compatible/candidate/Cargo.lock
11  cargo compatible suggest-manifest --package my-crate
12  cargo compatible explain serde";
13
14#[derive(Parser, Debug)]
15#[command(name = "cargo-compatible", bin_name = "cargo compatible", version, after_help = AFTER_HELP)]
16pub struct Cli {
17    #[command(subcommand)]
18    pub command: Commands,
19}
20
21#[derive(Subcommand, Debug)]
22pub enum Commands {
23    Scan(ScanCommand),
24    Resolve(ResolveCommand),
25    ApplyLock(ApplyLockCommand),
26    SuggestManifest(SuggestManifestCommand),
27    Explain(ExplainCommand),
28}
29
30#[derive(Copy, Clone, Debug, Eq, PartialEq, ValueEnum)]
31pub enum OutputFormat {
32    Human,
33    Json,
34    Markdown,
35}
36
37#[derive(Args, Debug, Clone)]
38pub struct SelectionArgs {
39    #[arg(long)]
40    pub manifest_path: Option<PathBuf>,
41    #[arg(long)]
42    pub rust_version: Option<String>,
43    #[arg(long)]
44    pub workspace: bool,
45    #[arg(long = "package", short = 'p')]
46    pub package: Vec<String>,
47}
48
49#[derive(Args, Debug)]
50pub struct ScanCommand {
51    #[command(flatten)]
52    pub selection: SelectionArgs,
53    #[arg(long, value_enum, default_value = "human")]
54    pub format: OutputFormat,
55}
56
57#[derive(Args, Debug, Clone)]
58pub struct ResolveCommand {
59    #[command(flatten)]
60    pub selection: SelectionArgs,
61    #[arg(long, value_enum, default_value = "human")]
62    pub format: OutputFormat,
63    #[arg(long = "write-candidate")]
64    pub write_candidate: Option<PathBuf>,
65    #[arg(long)]
66    pub write_report: Option<PathBuf>,
67}
68
69#[derive(Args, Debug)]
70pub struct ApplyLockCommand {
71    #[arg(long)]
72    pub manifest_path: Option<PathBuf>,
73    #[arg(long)]
74    pub candidate_lockfile: Option<PathBuf>,
75}
76
77#[derive(Args, Debug)]
78pub struct SuggestManifestCommand {
79    #[command(flatten)]
80    pub selection: SelectionArgs,
81    #[arg(long)]
82    pub allow_major: bool,
83    #[arg(long)]
84    pub write_manifests: bool,
85    #[arg(long, value_enum, default_value = "human")]
86    pub format: OutputFormat,
87}
88
89#[derive(Args, Debug)]
90pub struct ExplainCommand {
91    #[command(flatten)]
92    pub selection: SelectionArgs,
93    #[arg(value_name = "CRATE-OR-PKGID")]
94    pub query: String,
95    #[arg(long, value_enum, default_value = "human")]
96    pub format: OutputFormat,
97}