You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nenv/src/args.rs

69 lines
1.3 KiB
Rust

1 year ago
use std::str::FromStr;
use clap::{Parser, Subcommand};
use semver::VersionReq;
1 year ago
#[derive(Clone, Debug, Parser)]
#[clap(infer_subcommands = true)]
pub struct Args {
#[command(subcommand)]
pub commmand: Command,
}
#[derive(Clone, Debug, Subcommand)]
pub enum Command {
#[command()]
Install(InstallArgs),
#[command()]
Use(UseArgs),
#[command()]
Default,
#[command(short_flag = 'v', aliases = &["--version"])]
Version,
}
#[derive(Clone, Debug, Parser)]
pub struct InstallArgs {
#[arg()]
pub version: Version,
}
#[derive(Clone, Debug, Parser)]
pub struct UseArgs {
#[arg()]
pub version: Version,
}
impl FromStr for Version {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let input = s.to_lowercase();
let version = match &*input {
"latest" => Self::Latest,
"lts" => Self::LatestLts,
_ => {
if let Ok(req) = VersionReq::parse(s) {
Self::Req(req)
} else {
Self::Lts(s.to_lowercase())
}
}
1 year ago
};
Ok(version)
}
}
#[derive(Clone, Debug)]
pub enum Version {
Latest,
LatestLts,
Lts(String),
Req(VersionReq),
1 year ago
}