snarkvm_debug/cli/
cli.rs

1// Copyright (C) 2019-2023 Aleo Systems Inc.
2// This file is part of the snarkVM 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// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::cli::commands::{Build, Clean, Execute, New, Run, Update};
16
17use anstyle::{AnsiColor, Color, Style};
18use anyhow::Result;
19use clap::{builder::Styles, Parser};
20
21const HEADER_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Yellow));
22const LITERAL_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Green));
23const STYLES: Styles = Styles::plain()
24    .header(Style::new().bold().fg_color(HEADER_COLOR))
25    .usage(Style::new().bold().fg_color(HEADER_COLOR))
26    .literal(Style::new().bold().fg_color(LITERAL_COLOR));
27
28#[derive(Debug, Parser)]
29#[clap(name = "snarkVM", author = "The Aleo Team <hello@aleo.org>", styles = STYLES)]
30pub struct CLI {
31    /// Specify the verbosity [options: 0, 1, 2, 3]
32    #[clap(default_value = "2", short, long)]
33    pub verbosity: u8,
34    /// Specify a subcommand.
35    #[clap(subcommand)]
36    pub command: Command,
37}
38
39#[derive(Debug, Parser)]
40pub enum Command {
41    #[clap(name = "build")]
42    Build(Build),
43    #[clap(name = "clean")]
44    Clean(Clean),
45    #[clap(name = "execute")]
46    Execute(Execute),
47    #[clap(name = "new")]
48    New(New),
49    #[clap(name = "run")]
50    Run(Run),
51    #[clap(name = "update")]
52    Update(Update),
53}
54
55impl Command {
56    /// Parse the command.
57    pub fn parse(self) -> Result<String> {
58        match self {
59            Self::Build(command) => command.parse(),
60            Self::Clean(command) => command.parse(),
61            Self::Execute(command) => command.parse(),
62            Self::New(command) => command.parse(),
63            Self::Run(command) => command.parse(),
64            Self::Update(command) => command.parse(),
65        }
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    // A test case recommended by clap (https://docs.rs/clap/latest/clap/_derive/_tutorial/index.html#testing).
74    #[test]
75    fn verify_cli() {
76        use clap::CommandFactory;
77        CLI::command().debug_assert()
78    }
79}