Expand description
Repeatable comma-separated set-style cli flags.
Given a fixed list of known tokens, parse one occurrence of a flag whose
value is a comma-separated list of those tokens, accumulating into a
HashSet. A - prefix on a token removes it from the set instead of
adding. Unknown tokens error.
Each KnownToken has a single canonical spelling plus zero or more
Aliases. Aliases let multiple input spellings (e.g. nocolor and
no-color) resolve to the same canonical entry, so callers test
set.contains("nocolor") regardless of which spelling the user typed.
Designed for flags like --quirks=foo,bar --quirks=-foo where the
final state is {bar}. Order across flag occurrences matters; order
within a single occurrence also matters (left-to-right).
Resolution complexity is O(input_tokens x canonicals x aliases). Fine
for the small flag tables this crate targets; do not graft a runtime
plugin loader on top.
§Example
use std::collections::HashSet;
use polyflag::{KnownToken, apply, token};
const KNOWN: &[KnownToken] = &[
token!("foo"),
token!("bar"; "barre"),
token!("baz"; "baz-alt", deprecated "old-baz"),
];
let mut set: HashSet<&'static str> = HashSet::new();
apply("foo,barre", KNOWN, &mut set).unwrap();
apply("baz", KNOWN, &mut set).unwrap();
apply("bar,-foo", KNOWN, &mut set).unwrap();
assert!(set.contains("bar") && set.contains("baz"));
assert!(!set.contains("foo"));
// aliases never live in the set; only canonicals do.
assert!(!set.contains("barre"));Macros§
- debug_
check_ known - Wrap
check_knownin adebug_assert!-shaped call site. Identical runtime semantics to callingcheck_knowndirectly (both are stripped in release), but makes intent unambiguous at the call site: - token
- Construct a
KnownTokentersely.
Structs§
- Alias
- One alias spelling for a
KnownToken. - Known
Token - One entry in the caller’s known-token table: a canonical spelling plus
zero or more aliases that resolve to it, and an optional
defaultmarker for tokens that should be in the set before any cli/env input. - Resolved
- Result of resolving one input token against a known-token list.
- Unknown
Token - Returned when an input token (with any leading
-stripped) does not appear as a canonical or alias in the caller’sknownlist.
Enums§
- Alias
Status - How an alias is treated by callers that surface user-facing messaging.
- Resolved
Kind - Why an input matched – mirrors
AliasStatusplus aResolvedKind::Canonicalvariant for canonical-spelling input.
Functions§
- apply
- Apply one occurrence of a set flag’s value to
set. - apply_
env_ for_ flag - Apply an env-var-sourced default for the named cli flag to
set. - apply_
env_ for_ flag_ with_ callback - Like
apply_env_for_flag, but threads a deprecation callback through toapply_with_callback. - apply_
with_ callback - Like
apply, but invokeson_deprecated(input_spelling, canonical)each time an input token resolves through aAliasStatus::Deprecatedalias.Hiddenaliases never call back; canonicals andAlternativealiases never call back. - canonicalize
- Look up
input(one bare token, no-prefix) againstknown. Returns the canonical entry and how the input matched.Noneif the input is not a canonical or alias for any known token. - check_
known - defaults
- Seed a fresh
HashSetwith the canonicals of everyKnownTokenmarkeddefault = true. Use as the starting set before applying any env-var or cli occurrences, so the caller’s table doubles as both the schema and the on-by-default policy. - env_
var_ name - Compute the env var name corresponding to a flag, using the same
derivation as
apply_env_for_flag. Exposed so callers can include the resolved name in error messages without repeating the rule. - format_
known_ for_ help - Render the known-token table as a comma-separated string suitable for
embedding in help text or error messages. Canonicals are joined with
", "; non-AliasStatus::Hiddenaliases for each canonical are listed parenthetically.Hiddenaliases are omitted (that’s their point).