Skip to content

Commit

Permalink
Add support for possible_values
Browse files Browse the repository at this point in the history
  • Loading branch information
crodas committed Feb 14, 2024
1 parent c8ce28c commit cc8f743
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
28 changes: 28 additions & 0 deletions forc-util/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ impl CommandInfo {
cmd.get_arguments()
.map(|opt| ArgInfo {
name: opt.get_name().to_string(),
possible_values: opt
.get_possible_values()
.map(|x| {
x.iter()
.map(|x| PossibleValues {
name: x.get_name().to_owned(),
help: x.get_help().unwrap_or_default().to_owned(),
})
.collect::<Vec<_>>()
})
.unwrap_or_default(),
short: opt.get_short_and_visible_aliases(),
aliases: opt
.get_long_and_visible_aliases()
Expand All @@ -89,9 +100,18 @@ impl CommandInfo {
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PossibleValues {
pub name: String,
pub help: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ArgInfo {
pub name: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub possible_values: Vec<PossibleValues>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub short: Option<Vec<char>>,
Expand Down Expand Up @@ -122,6 +142,14 @@ impl ArgInfo {
if let Some(long_help) = &self.long_help {
arg = arg.long_help(long_help.as_str());
}
if !self.possible_values.is_empty() {
arg = arg.possible_values(
self.possible_values
.iter()
.map(|pv| clap::PossibleValue::new(pv.name.as_str()).help(pv.help.as_str()))
.collect::<Vec<_>>(),
);
}
if self.is_repeatable {
arg = arg.multiple(true);
}
Expand Down
11 changes: 10 additions & 1 deletion forc/src/cli/commands/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap_complete::{generate, Generator, Shell as BuiltInShell};
use forc_util::cli::CommandInfo;
use forc_util::ForcResult;
use std::collections::HashMap;
use std::str::FromStr;
use std::{fmt::Display, str::FromStr};

use crate::cli::plugin::find_all;

Expand All @@ -15,6 +15,15 @@ enum Target {
Fig,
}

impl Display for Target {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.to_possible_value()
.expect("no values are skipped")
.get_name()
.fmt(f)
}
}

impl FromStr for Target {
type Err = String;

Expand Down

0 comments on commit cc8f743

Please sign in to comment.