Skip to content

Commit

Permalink
feat: Add Arg getters for all settings
Browse files Browse the repository at this point in the history
This is prep for clap-rs#2717
  • Loading branch information
epage committed Feb 10, 2022
1 parent 519fcc2 commit 9ca0d6a
Show file tree
Hide file tree
Showing 32 changed files with 764 additions and 543 deletions.
4 changes: 2 additions & 2 deletions clap_complete/src/generator/utils.rs
@@ -1,6 +1,6 @@
//! Helpers for writing generators

use clap::{App, Arg, ArgSettings};
use clap::{App, Arg};

/// Gets all subcommands including child subcommands in the form of `("name", "bin_name")`.
///
Expand Down Expand Up @@ -121,7 +121,7 @@ pub fn longs_and_visible_aliases(p: &App) -> Vec<String> {
pub fn flags<'help>(p: &App<'help>) -> Vec<Arg<'help>> {
debug!("flags: name={}", p.get_name());
p.get_arguments()
.filter(|a| !a.is_set(ArgSettings::TakesValue) && !a.is_positional())
.filter(|a| !a.is_takes_value_set() && !a.is_positional())
.cloned()
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/src/shells/fish.rs
Expand Up @@ -143,7 +143,7 @@ fn gen_fish_inner(root_command: &str, parent_commands: &[&str], app: &App, buffe
}

fn value_completion(option: &Arg) -> String {
if !option.is_set(ArgSettings::TakesValue) {
if !option.is_takes_value_set() {
return "".to_string();
}

Expand Down
14 changes: 6 additions & 8 deletions clap_complete/src/shells/zsh.rs
Expand Up @@ -359,14 +359,14 @@ fn value_completion(arg: &Arg) -> Option<String> {
if let Some(values) = &arg.get_possible_values() {
if values
.iter()
.any(|value| !value.is_hidden() && value.get_help().is_some())
.any(|value| !value.is_hide_set() && value.get_help().is_some())
{
Some(format!(
"(({}))",
values
.iter()
.filter_map(|value| {
if value.is_hidden() {
if value.is_hide_set() {
None
} else {
Some(format!(
Expand Down Expand Up @@ -447,7 +447,7 @@ fn write_opts_of(p: &App, p_global: Option<&App>) -> String {
let help = o.get_help().map_or(String::new(), escape_help);
let conflicts = arg_conflicts(p, o, p_global);

let multiple = if o.is_set(ArgSettings::MultipleOccurrences) {
let multiple = if o.is_multiple_occurrences_set() {
"*"
} else {
""
Expand Down Expand Up @@ -550,7 +550,7 @@ fn write_flags_of(p: &App, p_global: Option<&App>) -> String {
let help = f.get_help().map_or(String::new(), escape_help);
let conflicts = arg_conflicts(p, &f, p_global);

let multiple = if f.is_set(ArgSettings::MultipleOccurrences) {
let multiple = if f.is_multiple_occurrences_set() {
"*"
} else {
""
Expand Down Expand Up @@ -628,11 +628,9 @@ fn write_positionals_of(p: &App) -> String {
for arg in p.get_positionals() {
debug!("write_positionals_of:iter: arg={}", arg.get_name());

let cardinality = if arg.is_set(ArgSettings::MultipleValues)
|| arg.is_set(ArgSettings::MultipleOccurrences)
{
let cardinality = if arg.is_multiple_values_set() || arg.is_multiple_occurrences_set() {
"*:"
} else if !arg.is_set(ArgSettings::Required) {
} else if !arg.is_required_set() {
":"
} else {
""
Expand Down
6 changes: 3 additions & 3 deletions clap_complete_fig/src/fig.rs
Expand Up @@ -212,7 +212,7 @@ fn gen_options(app: &App, indent: usize) -> String {
}

fn gen_args(arg: &Arg, indent: usize) -> String {
if !arg.is_set(ArgSettings::TakesValue) {
if !arg.is_takes_value_set() {
return "".to_string();
}

Expand All @@ -225,15 +225,15 @@ fn gen_args(arg: &Arg, indent: usize) -> String {
indent = indent
));

if arg.is_set(ArgSettings::MultipleValues) {
if arg.is_multiple_values_set() {
buffer.push_str(&format!(
"{:indent$}isVariadic: true,\n",
"",
indent = indent + 2
));
}

if !arg.is_set(ArgSettings::Required) {
if !arg.is_required_set() {
buffer.push_str(&format!(
"{:indent$}isOptional: true,\n",
"",
Expand Down
3 changes: 1 addition & 2 deletions clap_mangen/src/lib.rs
Expand Up @@ -259,8 +259,7 @@ fn app_has_version(app: &clap::App) -> bool {

// Does the application have any command line arguments?
fn app_has_arguments(app: &clap::App) -> bool {
app.get_arguments()
.any(|i| !i.is_set(clap::ArgSettings::Hidden))
app.get_arguments().any(|i| !i.is_hide_set())
}

// Does the application have any subcommands?
Expand Down
11 changes: 4 additions & 7 deletions clap_mangen/src/render.rs
@@ -1,4 +1,4 @@
use clap::{AppSettings, ArgSettings};
use clap::AppSettings;
use roff::{bold, italic, roman, Inline, Roff};

pub(crate) fn subcommand_heading(app: &clap::App) -> String {
Expand Down Expand Up @@ -81,10 +81,7 @@ pub(crate) fn synopsis(roff: &mut Roff, app: &clap::App) {
}

pub(crate) fn options(roff: &mut Roff, app: &clap::App) {
let items: Vec<_> = app
.get_arguments()
.filter(|i| !i.is_set(ArgSettings::Hidden))
.collect();
let items: Vec<_> = app.get_arguments().filter(|i| !i.is_hide_set()).collect();

for opt in items.iter().filter(|a| !a.is_positional()) {
let mut body = vec![];
Expand Down Expand Up @@ -189,7 +186,7 @@ fn subcommand_markers(cmd: &clap::App) -> (&'static str, &'static str) {
}

fn option_markers(opt: &clap::Arg) -> (&'static str, &'static str) {
markers(opt.is_set(ArgSettings::Required))
markers(opt.is_required_set())
}

fn markers(required: bool) -> (&'static str, &'static str) {
Expand All @@ -209,7 +206,7 @@ fn long_option(opt: &str) -> Inline {
}

fn option_environment(opt: &clap::Arg) -> Option<Vec<Inline>> {
if opt.is_set(ArgSettings::HideEnv) {
if opt.is_hide_env_set() {
return None;
} else if let Some(env) = opt.get_env() {
return Some(vec![
Expand Down
2 changes: 1 addition & 1 deletion examples/multicall-busybox.rs
Expand Up @@ -24,7 +24,7 @@ fn main() {
.exclusive(true)
.takes_value(true)
.default_missing_value("/usr/local/bin")
.use_delimiter(false),
.use_value_delimiter(false),
)
.subcommands(applet_commands()),
)
Expand Down
37 changes: 17 additions & 20 deletions src/build/app/debug_asserts.rs
Expand Up @@ -2,7 +2,7 @@ use crate::{
build::arg::{debug_asserts::assert_arg, ArgProvider},
mkeymap::KeyType,
util::Id,
App, AppSettings, Arg, ArgSettings, ValueHint,
App, AppSettings, Arg, ValueHint,
};
use std::cmp::Ordering;

Expand Down Expand Up @@ -177,7 +177,7 @@ pub(crate) fn assert_app(app: &App) {
);
}

if arg.is_set(ArgSettings::Last) {
if arg.is_last_set() {
assert!(
arg.long.is_none(),
"App {}: Flags or Options cannot have last(true) set. '{}' has both a long and last(true) set.",
Expand All @@ -193,7 +193,7 @@ pub(crate) fn assert_app(app: &App) {
}

assert!(
!(arg.is_set(ArgSettings::Required) && arg.get_global()),
!(arg.is_required_set() && arg.is_global_set()),
"App {}: Global arguments cannot be required.\n\n\t'{}' is marked as both global and required",
app.get_name(),
arg.name
Expand Down Expand Up @@ -463,9 +463,9 @@ fn _verify_positionals(app: &App) -> bool {

// Either the final positional is required
// Or the second to last has a terminator or .last(true) set
let ok = last.is_set(ArgSettings::Required)
|| (second_to_last.terminator.is_some() || second_to_last.is_set(ArgSettings::Last))
|| last.is_set(ArgSettings::Last);
let ok = last.is_required_set()
|| (second_to_last.terminator.is_some() || second_to_last.is_last_set())
|| last.is_last_set();
assert!(
ok,
"When using a positional argument with .multiple_values(true) that is *not the \
Expand All @@ -474,7 +474,7 @@ fn _verify_positionals(app: &App) -> bool {
);

// We make sure if the second to last is Multiple the last is ArgSettings::Last
let ok = second_to_last.is_multiple() || last.is_set(ArgSettings::Last);
let ok = second_to_last.is_multiple() || last.is_last_set();
assert!(
ok,
"Only the last positional argument, or second to last positional \
Expand All @@ -485,12 +485,12 @@ fn _verify_positionals(app: &App) -> bool {
let count = app
.get_positionals()
.filter(|p| {
p.settings.is_set(ArgSettings::MultipleOccurrences)
|| (p.settings.is_set(ArgSettings::MultipleValues) && p.num_vals.is_none())
p.is_multiple_occurrences_set()
|| (p.is_multiple_values_set() && p.num_vals.is_none())
})
.count();
let ok = count <= 1
|| (last.is_set(ArgSettings::Last)
|| (last.is_last_set()
&& last.is_multiple()
&& second_to_last.is_multiple()
&& count == 2);
Expand All @@ -509,16 +509,16 @@ fn _verify_positionals(app: &App) -> bool {
let mut foundx2 = false;

for p in app.get_positionals() {
if foundx2 && !p.is_set(ArgSettings::Required) {
if foundx2 && !p.is_required_set() {
assert!(
p.is_set(ArgSettings::Required),
p.is_required_set(),
"Found non-required positional argument with a lower \
index than a required positional argument by two or more: {:?} \
index {:?}",
p.name,
p.index
);
} else if p.is_set(ArgSettings::Required) && !p.is_set(ArgSettings::Last) {
} else if p.is_required_set() && !p.is_last_set() {
// Args that .last(true) don't count since they can be required and have
// positionals with a lower index that aren't required
// Imagine: prog <req1> [opt1] -- <req2>
Expand All @@ -541,13 +541,13 @@ fn _verify_positionals(app: &App) -> bool {
for p in (1..=num_p).rev().filter_map(|n| app.args.get(&n)) {
if found {
assert!(
p.is_set(ArgSettings::Required),
p.is_required_set(),
"Found non-required positional argument with a lower \
index than a required positional argument: {:?} index {:?}",
p.name,
p.index
);
} else if p.is_set(ArgSettings::Required) && !p.is_set(ArgSettings::Last) {
} else if p.is_required_set() && !p.is_last_set() {
// Args that .last(true) don't count since they can be required and have
// positionals with a lower index that aren't required
// Imagine: prog <req1> [opt1] -- <req2>
Expand All @@ -560,15 +560,12 @@ fn _verify_positionals(app: &App) -> bool {
}
}
assert!(
app.get_positionals()
.filter(|p| p.is_set(ArgSettings::Last))
.count()
< 2,
app.get_positionals().filter(|p| p.is_last_set()).count() < 2,
"Only one positional argument may have last(true) set. Found two."
);
if app
.get_positionals()
.any(|p| p.is_set(ArgSettings::Last) && p.is_set(ArgSettings::Required))
.any(|p| p.is_last_set() && p.is_required_set())
&& app.has_subcommands()
&& !app.is_set(AppSettings::SubcommandsNegateReqs)
{
Expand Down
12 changes: 6 additions & 6 deletions src/build/app/mod.rs
Expand Up @@ -23,7 +23,7 @@ use os_str_bytes::RawOsStr;
use yaml_rust::Yaml;

// Internal
use crate::build::{arg::ArgProvider, Arg, ArgGroup, ArgPredicate, ArgSettings};
use crate::build::{arg::ArgProvider, Arg, ArgGroup, ArgPredicate};
use crate::error::ErrorKind;
use crate::error::Result as ClapResult;
use crate::mkeymap::MKeyMap;
Expand Down Expand Up @@ -2373,7 +2373,7 @@ impl<'help> App<'help> {
/// Iterate through the *options*.
pub fn get_opts(&self) -> impl Iterator<Item = &Arg<'help>> {
self.get_arguments()
.filter(|a| a.is_set(ArgSettings::TakesValue) && !a.is_positional())
.filter(|a| a.is_takes_value_set() && !a.is_positional())
}

/// Get a list of all arguments the given argument conflicts with.
Expand All @@ -2387,7 +2387,7 @@ impl<'help> App<'help> {
/// this `App`.
pub fn get_arg_conflicts_with(&self, arg: &Arg) -> Vec<&Arg<'help>> // FIXME: This could probably have been an iterator
{
if arg.get_global() {
if arg.is_global_set() {
self.get_global_arg_conflicts_with(arg)
} else {
arg.blacklist
Expand Down Expand Up @@ -2708,7 +2708,7 @@ impl<'help> App<'help> {
global_arg_vec.extend(
self.args
.args()
.filter(|a| a.get_global())
.filter(|a| a.is_global_set())
.map(|ga| ga.id.clone()),
);
if let Some((id, matches)) = matches.subcommand() {
Expand Down Expand Up @@ -2783,7 +2783,7 @@ impl<'help> App<'help> {
}

// Figure out implied settings
if a.is_set(ArgSettings::Last) {
if a.is_last_set() {
// if an arg has `Last` set, we need to imply DontCollapseArgsInUsage so that args
// in the usage string don't get confused or left out.
self.settings.set(AppSettings::DontCollapseArgsInUsage);
Expand Down Expand Up @@ -2852,7 +2852,7 @@ impl<'help> App<'help> {
debug!("App::_propagate_global_args:{}", self.name);

for sc in &mut self.subcommands {
for a in self.args.args().filter(|a| a.get_global()) {
for a in self.args.args().filter(|a| a.is_global_set()) {
let mut propagate = false;
let is_generated = matches!(
a.provider,
Expand Down
4 changes: 2 additions & 2 deletions src/build/app/settings.rs
Expand Up @@ -256,7 +256,7 @@ pub enum AppSettings {
/// was used.
///
/// **NOTE:** The same thing can be done manually by setting the final positional argument to
/// [`Arg::use_delimiter(false)`]. Using this setting is safer, because it's easier to locate
/// [`Arg::use_value_delimiter(false)`]. Using this setting is safer, because it's easier to locate
/// when making changes.
///
/// # Examples
Expand All @@ -268,7 +268,7 @@ pub enum AppSettings {
/// .get_matches();
/// ```
///
/// [`Arg::use_delimiter(false)`]: crate::Arg::use_delimiter()
/// [`Arg::use_value_delimiter(false)`]: crate::Arg::use_value_delimiter()
DontDelimitTrailingValues,

/// Allow partial matches of long arguments or their [aliases].
Expand Down

0 comments on commit 9ca0d6a

Please sign in to comment.