alpm_srcinfo/
cli.rs

1//! Commandline argument handling.
2use std::path::PathBuf;
3
4use alpm_types::Architecture;
5use clap::{Parser, Subcommand};
6
7use crate::SourceInfoSchema;
8
9/// The command-line interface handling for `alpm-srcinfo`.
10#[derive(Clone, Debug, Parser)]
11#[command(about, author, name = "alpm-srcinfo", version)]
12pub struct Cli {
13    /// The `alpm-srcinfo` commands.
14    #[command(subcommand)]
15    pub command: Command,
16}
17
18/// Output format for the `format-packages` command.
19#[derive(Clone, Debug, Default, strum::Display, clap::ValueEnum)]
20pub enum PackagesOutputFormat {
21    /// The JSON output format.
22    #[default]
23    #[strum(serialize = "json")]
24    Json,
25}
26
27/// Output format for the `format` command.
28#[derive(Clone, Debug, Default, strum::Display, clap::ValueEnum)]
29pub enum SourceInfoOutputFormat {
30    /// The JSON output format.
31    #[strum(serialize = "json")]
32    Json,
33
34    /// The SRCINFO output format
35    #[default]
36    #[strum(serialize = "srcinfo")]
37    Srcinfo,
38}
39
40/// The `alpm-srcinfo` commands.
41#[derive(Clone, Debug, Subcommand)]
42pub enum Command {
43    /// Create a SRCINFO file from a PKGBUILD file at a given path.
44    ///
45    /// If the PKGBUILD can be created and validated, the program exits with no output and a return
46    /// code of 0. If the file is missing or can not be validated, an error is emitted on stderr and
47    /// the program exits with a non-zero exit status.
48    #[command()]
49    Create {
50        /// An optional input file path to read from
51        ///
52        /// If no file is specified, stdin is read from and expected to contain PKGINFO data to
53        /// validate.
54        #[arg(value_name = "FILE")]
55        file: PathBuf,
56
57        /// Provide the output format
58        #[arg(
59            short,
60            long,
61            value_name = "OUTPUT_FORMAT",
62            default_value_t = SourceInfoOutputFormat::Srcinfo,
63        )]
64        output_format: SourceInfoOutputFormat,
65
66        /// Pretty-print the output.
67        ///
68        /// Only applies to formats that support pretty output and is otherwise ignored.
69        #[arg(short, long)]
70        pretty: bool,
71    },
72
73    /// Validate a SRCINFO file from a path or `stdin`.
74    ///
75    /// If the file can be validated, the program exits with no output and a return code of 0.
76    /// If the file can not be validated, an error is emitted on stderr and the program exits with
77    /// a non-zero exit status.
78    #[command()]
79    Validate {
80        /// An optional input file path to read from
81        ///
82        /// If no file is specified, stdin is read from and expected to contain PKGINFO data to
83        /// validate.
84        #[arg(value_name = "FILE")]
85        file: Option<PathBuf>,
86
87        /// Provide the SRCINFO schema version to use.
88        ///
89        /// If no schema version is provided, it will be deduced from the file itself.
90        #[arg(short, long, value_name = "VERSION")]
91        schema: Option<SourceInfoSchema>,
92    },
93
94    /// Format a SRCINFO file from a path or `stdin`.
95    ///
96    /// If the file is valid, the program prints the data in the
97    /// requested file format to stdout and returns with an exit status of 0.
98    #[command()]
99    Format {
100        /// The file to read from.
101        ///
102        /// If no file is provided, stdin is used instead.
103        #[arg(value_name = "FILE")]
104        file: Option<PathBuf>,
105
106        /// Provide the SRCINFO schema version to use.
107        ///
108        /// If no schema version is provided, it will be deduced from the file itself.
109        #[arg(short, long, value_name = "VERSION")]
110        schema: Option<SourceInfoSchema>,
111
112        /// Provide the output format
113        #[arg(
114            short,
115            long,
116            value_name = "OUTPUT_FORMAT",
117            default_value_t = SourceInfoOutputFormat::Srcinfo,
118        )]
119        output_format: SourceInfoOutputFormat,
120
121        /// Pretty-print the output.
122        ///
123        /// Only applies to formats that support pretty output and is otherwise ignored.
124        #[arg(short, long)]
125        pretty: bool,
126    },
127
128    /// Format a SRCINFO file's packages from a path or `stdin`
129    ///
130    /// Read, validate and print all of the SRCINFO's packages in their final representation for a
131    /// specific architecture. If the file is valid, the program prints the data in the
132    /// requested file format to stdout and returns with an exit status of 0.
133    #[command()]
134    FormatPackages {
135        /// An optional input file path to read from
136        ///
137        /// If no file is specified, stdin is read from and expected to contain PKGINFO data to
138        /// validate.
139        #[arg(value_name = "FILE")]
140        file: Option<PathBuf>,
141
142        /// Provide the SRCINFO schema version to use.
143        ///
144        /// If no schema version is provided, it will be deduced from the file itself.
145        #[arg(short, long, value_name = "VERSION")]
146        schema: Option<SourceInfoSchema>,
147
148        /// The selected architecture that should be used to interpret the SRCINFO file.
149        ///
150        /// Only [split-]packages that are applicable for this architecture will be returned.
151        #[arg(short, long, alias = "arch")]
152        architecture: Architecture,
153
154        /// Provide the output format
155        #[arg(
156            short,
157            long,
158            value_name = "OUTPUT_FORMAT",
159            default_value_t = PackagesOutputFormat::Json
160        )]
161        output_format: PackagesOutputFormat,
162
163        /// Pretty-print the output.
164        ///
165        /// Only applies to formats that support pretty output and is otherwise ignored.
166        #[arg(short, long)]
167        pretty: bool,
168    },
169}