1use std::{io::Write, path::PathBuf, process::exit};
23use clap::builder::TypedValueParser;
4use serde_json::Value;
56use cuprate_helper::network::Network;
78use crate::{config::Config, version::CupratedVersionInfo};
910/// Cuprate Args.
11#[derive(clap::Parser, Debug)]
12#[command(about)]
13pub struct Args {
14/// The network to run on.
15#[arg(
16 long,
17 default_value_t = Network::Mainnet,
18 value_parser = clap::builder::PossibleValuesParser::new(["mainnet", "testnet", "stagenet"])
19 .map(|s| s.parse::<Network>().unwrap()),
20 )]
21pub network: Network,
2223/// Disable fast sync, all past blocks will undergo full verification when syncing.
24 ///
25 /// This significantly increases initial sync time. This provides no extra security, you just
26 /// have to trust the devs to insert the correct hashes (which are verifiable).
27#[arg(long)]
28no_fast_sync: bool,
2930/// The amount of outbound clear-net connections to maintain.
31#[arg(long)]
32pub outbound_connections: Option<usize>,
3334/// The PATH of the `cuprated` config file.
35#[arg(long)]
36pub config_file: Option<PathBuf>,
3738/// Generate a config file and print it to stdout.
39#[arg(long)]
40pub generate_config: bool,
4142/// Stops the missing config warning and startup delay if a config file is missing.
43#[arg(long)]
44pub skip_config_warning: bool,
4546/// Print misc version information in JSON.
47#[arg(short, long)]
48pub version: bool,
49}
5051impl Args {
52/// Complete any quick requests asked for in [`Args`].
53 ///
54 /// May cause the process to [`exit`].
55pub fn do_quick_requests(&self) {
56if self.version {
57let version_info = CupratedVersionInfo::new();
58let json = serde_json::to_string_pretty(&version_info).unwrap();
59println!("{json}");
60 exit(0);
61 }
6263if self.generate_config {
64println!("{}", Config::documented_config());
65 exit(0);
66 }
67 }
6869/// Apply the [`Args`] to the given [`Config`].
70 ///
71 /// This may exit the program if a config value was set that requires an early exit.
72pub const fn apply_args(&self, mut config: Config) -> Config {
73 config.network = self.network;
74 config.fast_sync = config.fast_sync && !self.no_fast_sync;
7576if let Some(outbound_connections) = self.outbound_connections {
77 config.p2p.clear_net.general.outbound_connections = outbound_connections;
78 }
7980 config
81 }
82}