alpm_mtree/
cli.rs

1use std::path::PathBuf;
2
3use clap::{Parser, Subcommand, ValueEnum};
4
5use crate::MtreeSchema;
6
7/// The command-line interface handling for `alpm-mtree`.
8#[derive(Clone, Debug, Parser)]
9#[command(about, author, name = "alpm-mtree", version)]
10pub struct Cli {
11    /// The `alpm-mtree` commands.
12    #[command(subcommand)]
13    pub command: Command,
14}
15
16/// Output format for the parse command
17#[derive(Clone, Debug, Default, strum::Display, ValueEnum)]
18pub enum OutputFormat {
19    /// The JSON output format.
20    #[default]
21    #[strum(serialize = "json")]
22    Json,
23}
24
25/// The `alpm-mtree` commands.
26#[allow(clippy::large_enum_variant)]
27#[derive(Clone, Debug, Subcommand)]
28pub enum Command {
29    /// Read an MTREE file and return it in another file format
30    ///
31    /// Reads and validates an MTREE file according to a schema and outputs it in another file
32    /// format (currently, only JSON is supported). If the file can be validated, the program
33    /// exits with the data returned in another file format on stdout and a return code of 0.
34    /// If the file can not be validated, an error is emitted on stderr and the program exits with
35    /// a non-zero exit code.
36    #[command()]
37    Format {
38        /// An optional file to read from.
39        ///
40        /// If no file is provided, stdin is used instead.
41        #[arg(value_name = "FILE")]
42        file: Option<PathBuf>,
43
44        /// Provide the MTREE schema version to use.
45        ///
46        /// If no schema version is provided, it will be deduced from the file itself.
47        ///
48        /// Valid values are ['1', '2'].
49        #[arg(short, long, value_name = "VERSION")]
50        schema: Option<MtreeSchema>,
51
52        /// Provide the output format
53        #[arg(
54            short,
55            long,
56            value_name = "OUTPUT_FORMAT",
57            default_value_t = OutputFormat::Json
58        )]
59        output_format: OutputFormat,
60
61        /// Determines whether the output will be displayed in a pretty non-minimized fashion.
62        ///
63        /// Only applies to formats that support pretty output, otherwise it's just ignored.
64        #[arg(short, long)]
65        pretty: bool,
66    },
67    /// Validate an MTREE file
68    ///
69    /// Validate an MTREE file according to a schema.
70    /// If the file can be validated, the program exits with no output and a return code of 0.
71    /// If the file can not be validated, an error is emitted on stderr and the program exits with
72    /// a non-zero exit code.
73    #[command()]
74    Validate {
75        /// An optional file to read from.
76        ///
77        /// If no file is provided, stdin is used instead.
78        #[arg(value_name = "FILE")]
79        file: Option<PathBuf>,
80
81        /// Provide the MTREE schema version to use.
82        ///
83        /// If no schema version is provided, it will be deduced from the file itself.
84        ///
85        /// Valid values are ['1', '2'].
86        #[arg(short, long, value_name = "VERSION")]
87        schema: Option<MtreeSchema>,
88    },
89}