oapi_codegen/cli.rs
1//! Definition of the command-line interface.
2//!
3//! [`Cli`] is the source of truth for the CLI.
4//! The binary parses this definition.
5//! The `cli_docs` test renders this definition as Markdown.
6//! This keeps `docs/cli.md` in sync with the interface.
7
8use std::path::PathBuf;
9
10use clap::Parser;
11
12/// Extra `--help` text with examples.
13pub const EXAMPLES: &str = "\
14Examples:
15 # Write generated code. Select artifacts in the configuration file:
16 oapi-codegen --config-file oapi-codegen.yaml --output-file src/api.rs api.yaml
17
18 # You can also set output with the `output:` configuration key:
19 oapi-codegen --config-file oapi-codegen.yaml api.yaml
20
21You must provide a configuration file.
22The configuration file must enable at least one artifact.
23You must set output with --output-file or the `output:` configuration key:
24 # oapi-codegen.yaml
25 output: src/api.rs
26 generate:
27 models: true
28 std-http-server: true
29 client: true";
30
31/// Generate Rust code from an OpenAPI 3 specification.
32#[derive(Debug, Parser)]
33#[command(name = "oapi-codegen", version, about, after_long_help = EXAMPLES)]
34pub struct Cli {
35 /// Path to the OpenAPI 3 specification (YAML or JSON).
36 pub spec_file: PathBuf,
37
38 /// Path to an `oapi-codegen` YAML configuration file (required).
39 #[arg(short = 'c', long)]
40 pub config_file: PathBuf,
41
42 /// Output file path (overrides configuration `output:`).
43 /// Required unless the configuration sets `output:`.
44 #[arg(short = 'o', long)]
45 pub output_file: Option<PathBuf>,
46
47 /// Compare the generated code with the output file, and write nothing.
48 ///
49 /// The exit code is 0 when the output file holds the generated code.
50 /// The exit code is 1 when the file differs, or when the file is absent.
51 /// Use this in continuous integration to make stale output a failed build.
52 /// This flag reports no dependencies, because it adds none.
53 #[arg(long, conflicts_with = "install_deps")]
54 pub check: bool,
55
56 /// After the write, run `cargo add` for each required crate.
57 ///
58 /// If set, this runs with no prompt.
59 /// If stdin is interactive and the flag is not set, the CLI asks first.
60 /// If stdin is not interactive and the flag is not set, the CLI prints only the list.
61 /// `cargo add` targets the package whose `Cargo.toml` is nearest output.
62 /// It merges with an existing declaration.
63 /// A crate that already exists is updated in place.
64 #[arg(long)]
65 pub install_deps: bool,
66}