Crate we_clap

Source
Expand description

§WE_CLAP : Web Enabled Command Line Argument Parser

§we_clap

Pronounced “We clap”, as in “Give yourself a round of applause.”

The goal is to be flexible, write your command line code once and it should be able to run anywhere!

We_clap has two traits, WeCommand and WeParser that wrap the clap::Command struct and the clap::Parser traits. These traits have wrapper functions that you call to fill your derive struct or work with your command. For example; WeCommand::we_get_matches() and WeParser::we_parse(). These wrappers get arguments from ArgsOs on native and UrlArgs on the web. They also handle error and help output like clap on native, on the web the error and help messages are to the console or popup alert.

§We_clap Features

Web output functionality of the we_clap crate is gated by features.

  • web-alert
    • Enable output to browser popup alert.
  • web-console
    • Enable output to browser console.
    • Set by default

§Example

§we_clap_demo

§Cargo.toml

[package]
name = "we_clap_demo"
version = "0.1.0"
edition = "2021"

[dependencies]
# get clap powers
clap = { version = "4.5.30", features = ["derive"] }
# get web ability for clap
we_clap = { version = "0.1.1" , features = ["web-alert"] }

§main.rs

use clap::Parser;
use we_clap::WeParser; // Wrapper for clap Parser

// Implement web enabled parser for your struct
impl we_clap::WeParser for Opts {}

#[derive(Parser, Debug, Default)]
#[command(author, version, about, long_about)]
pub struct Opts {
    /// An optional value
    #[arg(short, long)]
    pub value: Option<f32>,
}

fn main() {
// Like magic, using we_parse() will work on native parsing
// the command line arguments or on the web parsing the url query
// string as if it were command line arguments, providing clap help
// and error messages to stdout/stderr on native or a popup alert
// on web/wasm.

    // Type annotations needed
    let opts: Opts = Opts::we_parse();

    // this app doesn't do anything, except parse arguments and
    // demonstrate clap powers in the web.
}

Traits§

WeCommand
Wrapper trait implemented for clap::Command
WeParser
Wrapper trait for clap::Parser