Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support option starts with + #261

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 59 additions & 6 deletions examples/options.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# @cmd
# @describe All kind of options
# @cmd All kind of options
# @option --oa
# @option -b --ob short
# @option -c short only
Expand All @@ -23,8 +22,7 @@ options() {
:;
}

# @cmd
# @describe All kind of flags
# @cmd All kind of flags
# @flag --fa
# @flag -b --fb short
# @flag -c short only
Expand All @@ -34,8 +32,7 @@ flags() {
:;
}

# @cmd
# @describe Flags or options with single dash
# @cmd Flags or options with single dash
# @flag -fa
# @flag -b -fb
# @flag -fd*
Expand All @@ -48,6 +45,62 @@ flags() {
:;
}


# @cmd All kind of options
# @option +oa
# @option +b +ob short
# @option +c short only
# @option +oc! required
# @option +od* multi-occurs
# @option +oe+ required + multi-occurs
# @option +ona <PATH> value notation
# @option +onb <CMD> <FILE> two-args value notations
# @option +onc <CMD> <FILE+> unlimited-args value notations
# @option +oda=a default
# @option +odb=`_default_fn` default from fn
# @option +oca[a|b] choice
# @option +ocb[=a|b] choice + default
# @option +occ*[a|b] multi-occurs + choice
# @option +ocd+[a|b] required + multi-occurs + choice
# @option +ofa[`_choice_fn`] choice from fn
# @option +ofb[?`_choice_fn`] choice from fn + no validation
# @option +ofc*[`_choice_fn`] multi-occurs + choice from fn
# @option +oxa~ capture all remaining args
plus_options() {
:;
}


# @cmd All kind of flags
# @flag +fa
# @flag +b +fb short
# @flag +c short only
# @flag +fd* multi-occurs
# @flag +e +fe* short + multi-occurs
plus_flags() {
:;
}

# @cmd Flags or options with single dash
# @flag +fa
# @flag +b +fb
# @flag +fd*
# @option +oa
# @option +od*
# @option +ona <PATH>
# @option +oca[a|b]
# @option +ofa[`_choice_fn`]
plus_1dash() {
:;
}


# @cmd Mixed `-` and `+` options
# @option +b --ob
mix_options() {
:;
}

_default_fn() {
whoami
}
Expand Down
50 changes: 26 additions & 24 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::Result;
use anyhow::{anyhow, bail};
use indexmap::IndexMap;
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

pub fn eval(
Expand Down Expand Up @@ -271,6 +271,18 @@ impl Command {
self.metadata.iter().any(|(k, _, _)| k == key)
}

pub(crate) fn flag_option_signs(&self) -> String {
let mut signs = HashSet::new();
signs.insert('-');
for param in &self.flag_option_params {
if let Some(short) = &param.short {
signs.extend(short.chars().take(1))
}
signs.extend(param.long_prefix.chars())
}
signs.into_iter().collect()
}

pub(crate) fn render_help(&self, cmd_paths: &[&str], term_width: Option<usize>) -> String {
let mut output = vec![];
if self.version.is_some() {
Expand Down Expand Up @@ -361,10 +373,10 @@ impl Command {
}
let mut list = vec![];
let mut any_describe = false;
let mut single_hyphen = false;
let mut single = false;
for param in self.flag_option_params.iter() {
if param.single_hyphen {
single_hyphen = true;
if param.long_prefix.len() == 1 {
single = true;
}
let value = param.render_body();
let describe = param.render_describe();
Expand All @@ -373,8 +385,8 @@ impl Command {
}
list.push((value, describe));
}
self.add_help_flag(&mut list, single_hyphen, any_describe);
self.add_version_flag(&mut list, single_hyphen, any_describe);
self.add_help_flag(&mut list, single, any_describe);
self.add_version_flag(&mut list, single, any_describe);
output.push("OPTIONS:".to_string());
let value_size = list.iter().map(|v| v.0.len()).max().unwrap_or_default() + 2;
for (value, describe) in list {
Expand Down Expand Up @@ -471,7 +483,7 @@ impl Command {
pub(crate) fn find_flag_option(&self, name: &str) -> Option<&FlagOptionParam> {
self.flag_option_params
.iter()
.find(|v| v.name() == name || v.is_match(name))
.find(|v| v.var_name() == name || v.is_match(name))
}

pub(crate) fn find_prefixed_option(&self, name: &str) -> Option<(&FlagOptionParam, String)> {
Expand All @@ -487,14 +499,14 @@ impl Command {

pub(crate) fn match_version_short_name(&self) -> bool {
match self.find_flag_option("-V") {
Some(param) => param.name() == "version",
Some(param) => param.var_name() == "version",
None => true,
}
}

pub(crate) fn match_help_short_name(&self) -> bool {
match self.find_flag_option("-h") {
Some(param) => param.name() == "help",
Some(param) => param.var_name() == "help",
None => true,
}
}
Expand Down Expand Up @@ -546,7 +558,7 @@ impl Command {
for subcmd in self.subcommands.iter_mut() {
let mut inherited_flag_options = vec![];
for flag_option in &self.flag_option_params {
if subcmd.find_flag_option(flag_option.name()).is_none() {
if subcmd.find_flag_option(flag_option.var_name()).is_none() {
let mut flag_option = flag_option.clone();
flag_option.inherit = true;
inherited_flag_options.push(flag_option);
Expand Down Expand Up @@ -593,16 +605,11 @@ impl Command {
self.subcommands.last_mut().unwrap()
}

fn add_help_flag(
&self,
list: &mut Vec<(String, String)>,
single_hyphen: bool,
any_describe: bool,
) {
fn add_help_flag(&self, list: &mut Vec<(String, String)>, single: bool, any_describe: bool) {
if self.find_flag_option("help").is_some() {
return;
}
let hyphens = if single_hyphen { " -" } else { "--" };
let hyphens = if single { " -" } else { "--" };
list.push((
if self.match_help_short_name() {
format!("-h, {}help", hyphens)
Expand All @@ -617,19 +624,14 @@ impl Command {
));
}

fn add_version_flag(
&self,
list: &mut Vec<(String, String)>,
single_hyphen: bool,
any_describe: bool,
) {
fn add_version_flag(&self, list: &mut Vec<(String, String)>, single: bool, any_describe: bool) {
if self.version.is_none() {
return;
}
if self.find_flag_option("version").is_some() {
return;
}
let hyphens = if single_hyphen { " -" } else { "--" };
let hyphens = if single { " -" } else { "--" };
list.push((
if self.match_version_short_name() {
format!("-V, {}version", hyphens)
Expand Down
4 changes: 2 additions & 2 deletions src/command/names_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl NamesChecker {
pos: Position,
) -> Result<()> {
let tag_name = param.tag_name();
let names = param.list_names();
let names = param.list_option_names();
for name in names.iter() {
if let Some((exist_pos, _)) = self.flag_options.get(name) {
bail!("{}", Self::conflict_error(tag_name, pos, name, *exist_pos));
Expand All @@ -33,7 +33,7 @@ impl NamesChecker {
param: &PositionalParam,
pos: Position,
) -> Result<()> {
let name = param.name();
let name = param.var_name();
if let Some(exist_pos) = self.positionals.get(name) {
bail!(
"{}",
Expand Down
Loading