amareleo_chain_cli/commands/
mod.rs

1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16mod clean;
17pub use clean::*;
18
19mod start;
20pub use start::*;
21
22mod update;
23pub use update::*;
24
25use anstyle::{AnsiColor, Color, Style};
26use anyhow::Result;
27use clap::{Parser, builder::Styles};
28
29const HEADER_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Yellow));
30const LITERAL_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Green));
31const STYLES: Styles = Styles::plain()
32    .header(Style::new().bold().fg_color(HEADER_COLOR))
33    .usage(Style::new().bold().fg_color(HEADER_COLOR))
34    .literal(Style::new().bold().fg_color(LITERAL_COLOR));
35
36#[derive(Debug, Parser)]
37#[clap(styles = STYLES, version)]
38pub struct CLI {
39    /// Specify the verbosity [options: 0, 1, 2, 3]
40    #[clap(default_value = "2", short, long)]
41    pub verbosity: u8,
42    /// Specify a subcommand.
43    #[clap(subcommand)]
44    pub command: Command,
45}
46
47#[derive(Debug, Parser)]
48pub enum Command {
49    #[clap(name = "clean")]
50    Clean(Clean),
51    #[clap(name = "start")]
52    Start(Box<Start>),
53    #[clap(name = "update")]
54    Update(Update),
55}
56
57impl Command {
58    /// Parses the command.
59    pub fn parse(self, repo_name: &str, bin_name: &str) -> Result<String> {
60        match self {
61            Self::Clean(command) => command.parse(),
62            Self::Start(command) => command.parse(),
63            Self::Update(command) => command.parse(repo_name, bin_name),
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    // As per the official clap recommendation.
73    #[test]
74    fn verify_cli() {
75        use clap::CommandFactory;
76        CLI::command().debug_assert()
77    }
78}