/ cli / src / commands / mod.rs
mod.rs
  1  // Copyright (c) 2025 ADnet Contributors
  2  // This file is part of the AlphaOS 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  
 16  mod account;
 17  pub use account::*;
 18  
 19  mod clean;
 20  pub use clean::*;
 21  
 22  mod developer;
 23  pub use developer::*;
 24  
 25  mod start;
 26  pub use start::*;
 27  
 28  mod update;
 29  pub use update::*;
 30  
 31  use anstyle::{AnsiColor, Color, Style};
 32  use anyhow::Result;
 33  use clap::{Parser, builder::Styles};
 34  
 35  const HEADER_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Yellow));
 36  const LITERAL_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Green));
 37  const ERROR_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Red));
 38  const INVALID_COLOR: Option<Color> = Some(Color::Ansi(AnsiColor::Magenta));
 39  
 40  const 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 = "AlphaOS", author, about, styles = STYLES, version)]
 53  pub 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)]
 65  pub 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  
 78  impl 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)]
 92  mod 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  }