Skip to content

clap_derive: Vec/Option<Vec> behavior is inconsistent with other types #1772

@CreepySkeleton

Description

@CreepySkeleton

This is actually a summary of TeXitoi/structopt#364

Current behavior

  • Vec<T> and Option<Vec<T>> are not required = true by default.
  • Vec<T> is multiple = true by default which allows not only multiple values (--foo 1 2 3) but also multiple occurrences (--foo 1 --foo 2 3).
  • Option<Vec<T>> additionally allows zero number of values (--foo).

What's wrong

  • The fact that Vec<T> is not required by default is inconsistent with all the other types in clap_derive that are required by default unless they are wrapped in Option (except bool but it's a very special case).
  • The fact that Option<Vec<T>> is different from Vec<T>, different not in "not required" sense, confuses newcomers.
  • The fact that Vec<T> allows multiple occurrences along with values is misleading.

Proposal

  • Use min_values = 1 for both Option<Vec<T>> and Vec<T> instead of multiple = true, allowing only non-zero number of values and disallow multiple occurrences (--foo 1 2 but not --foo nor --foo 1 --foo 2). If a user wants to allow zero values or multiple occurrences as well, they can explicitly specify it via min_values = 0 and multiple = true respectively.
  • Use required = true for Vec<T>.

cc @TeXitoi @Dylan-DPC @pksunkara

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-deriveArea: #[derive]` macro APIC-enhancementCategory: Raise on the bar on expectations

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions