snarkos_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 account;
17pub use account::*;
18
19mod clean;
20pub use clean::*;
21
22mod developer;
23pub use developer::*;
24
25mod start;
26pub use start::*;
27
28mod update;
29pub use update::*;
30
31use anstyle::{AnsiColor, Color, Style};
32use anyhow::Result;
33use clap::{Parser, builder::Styles};
34
35const HEADER_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Yellow));
36const LITERAL_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Green));
37const STYLES: Styles = Styles::plain()
38    .header(Style::new().bold().fg_color(HEADER_COLOR))
39    .usage(Style::new().bold().fg_color(HEADER_COLOR))
40    .literal(Style::new().bold().fg_color(LITERAL_COLOR));
41
42// Note: the basic clap-supplied version is overridden in the main module.
43#[derive(Debug, Parser)]
44#[clap(name = "snarkOS", author = "The Aleo Team <hello@aleo.org>", styles = STYLES, version)]
45pub struct CLI {
46    /// Specify the verbosity [options: 0, 1, 2, 3]
47    #[clap(default_value = "2", short, long)]
48    pub verbosity: u8,
49    /// Specify a subcommand.
50    #[clap(subcommand)]
51    pub command: Command,
52}
53
54#[derive(Debug, Parser)]
55pub enum Command {
56    #[clap(subcommand)]
57    Account(Account),
58    #[clap(name = "clean")]
59    Clean(Clean),
60    #[clap(subcommand)]
61    Developer(Developer),
62    #[clap(name = "start")]
63    Start(Box<Start>),
64    #[clap(name = "update")]
65    Update(Update),
66}
67
68impl Command {
69    /// Parses the command.
70    pub fn parse(self) -> Result<String> {
71        match self {
72            Self::Account(command) => command.parse(),
73            Self::Clean(command) => command.parse(),
74            Self::Developer(command) => command.parse(),
75            Self::Start(command) => command.parse(),
76            Self::Update(command) => command.parse(),
77        }
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    // As per the official clap recommendation.
86    #[test]
87    fn verify_cli() {
88        use clap::CommandFactory;
89        CLI::command().debug_assert()
90    }
91}