Skip to main content

snarkos_cli/commands/
mod.rs

1// Copyright (c) 2019-2025 Provable Inc.
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 ERROR_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Red));
38const INVALID_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Magenta));
39
40const STYLES: Styles = Styles::plain()
41    .header(Style::new().bold().fg_color(HEADER_COLOR))
42    .usage(Style::new().bold().fg_color(HEADER_COLOR))
43    .error(Style::new().bold().fg_color(ERROR_COLOR))
44    .invalid(Style::new().fg_color(INVALID_COLOR))
45    .valid(Style::new().bold().fg_color(LITERAL_COLOR))
46    .literal(Style::new().bold().fg_color(LITERAL_COLOR));
47
48// The top-level command-line argument.
49//
50// Metadata is sourced from Cargo.toml. However, the version will be overridden in the main module (snarkos/main.rs).
51#[derive(Debug, Parser)]
52#[clap(name = "snarkOS", author, about, styles = STYLES, version)]
53pub struct CLI {
54    /// Specify a subcommand.
55    #[clap(subcommand)]
56    pub command: Command,
57
58    /// Disable checking for new versions at startup.
59    #[clap(long, global = true)]
60    pub noupdater: bool,
61}
62
63/// The subcommand passed after `snarkos`, e.g. `Start` corresponds to `snarkos start`.
64#[derive(Debug, Parser)]
65pub enum Command {
66    #[clap(subcommand)]
67    Account(Account),
68    #[clap(name = "clean")]
69    Clean(Clean),
70    #[clap(name = "developer", alias = "dev")]
71    Developer(Box<Developer>),
72    #[clap(name = "start")]
73    Start(Box<Start>),
74    #[clap(name = "update")]
75    Update(Update),
76}
77
78impl Command {
79    /// Runs the given command.
80    pub fn parse(self) -> Result<String> {
81        match self {
82            Self::Account(command) => command.parse(),
83            Self::Clean(command) => command.parse(),
84            Self::Developer(command) => command.parse(),
85            Self::Start(command) => command.parse(),
86            Self::Update(command) => command.parse(),
87        }
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    // As per the official clap recommendation.
96    #[test]
97    fn verify_cli() {
98        use clap::CommandFactory;
99        CLI::command().debug_assert()
100    }
101}