Crate facet_args

Crate facet_args 

Source
Expand description

§facet-args

Coverage Status crates.io documentation MIT/Apache-2.0 licensed Discord

Provides CLI argument parsing (WIP).

use facet_pretty::FacetPretty;
use facet::Facet;
use facet_args as args;

#[derive(Facet)]
struct Args {
    #[facet(args::positional)]
    path: String,

    #[facet(args::named, args::short = 'v')]
    verbose: bool,

    #[facet(args::named, args::short = 'j')]
    concurrency: usize,
}

let args: Args = facet_args::from_slice(&["--verbose", "-j", "14", "example.rs"])?;
eprintln!("args: {}", args.pretty());
Ok(())

§Behavior

The behavior of facet-args is still in flux, but here are the broad strokes:

  • We’re always parsing to a struct (not an enum, vec etc.)
  • The struct we’re parsing to is always owned — no borrowing happening here, it gets too complicated with &'slice [&'text str]
  • Arguments are either positional or named — fields lacking either annotation are ignored
  • Accepted syntaxes for short flags are: args::short = 'v' and args::short = "v" (where v can be any letter)
  • positional args of type Vec (or anything that has a Def::List) will soak up all the positional arguments — if followed by positional arguments of type String for example, those will never get filled
  • After parsing every available argument, uninitialized struct fields are filled with their default value if they have facet(default) set: this includes Vec.

§Sponsors

Thanks to all individual sponsors:

GitHub Sponsors Patreon

…along with corporate sponsors:

AWS Zed Depot

…without whom this work could not exist.

§Special thanks

The facet logo was drawn by Misiasart.

§License

Licensed under either of:

at your option.

Re-exports§

pub use completions::Shell;
pub use completions::generate_completions;
pub use completions::generate_completions_for_shape;
pub use help::HelpConfig;
pub use help::generate_help;
pub use help::generate_help_for_shape;

Modules§

completions
Shell completion script generation for command-line interfaces.
help
Help text generation for command-line interfaces.

Structs§

ArgsErrorWithInput
An args parsing error, with input info, so that it can be formatted nicely

Enums§

ArgsErrorKind
An error kind for argument parsing.
Attr
Args attribute types for field configuration.

Functions§

from_slice
Parse command line arguments into a Facet-compatible type
from_slice_with_config
Parse command line arguments with custom help configuration
from_std_args
Parse command line arguments provided by std::env::args() into a Facet-compatible type