we_clap 0.1.6

Web Enabled Command Line Argument Parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#![warn(missing_docs)]
#![allow(clippy::needless_doctest_main)]
#![allow(clippy::doc_markdown)]
//! # 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
//! ``` 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
//! ``` rust
//! 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.
//! }
//! ```
//!
//! [`alert`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/alert
//! [`ArgsOs`]: https://doc.rust-lang.org/std/env/struct.ArgsOs.html
//! [`clap::Command`]: https://docs.rs/clap/latest/clap/struct.Command.html
//! [`clap::Parser`]: https://docs.rs/clap/latest/clap/trait.Parser.html
//! [`console`]: https://developer.mozilla.org/en-US/docs/Web/API/console
//! [`UrlArgs`]: https://docs.rs/cliw/latest/cliw/url_args/struct.UrlArgs.html

use clap::{error, ArgMatches, Command, Parser};

#[cfg(target_arch = "wasm32")]
use clap::error::ErrorKind;

#[cfg(target_arch = "wasm32")]
use cliw::url_args::UrlArgs;

/// # Wrapper trait implemented for [`clap::Command`]
///
/// Functions to work with your [`clap::Command`] on native or the web.\
/// On native the arguments come from [`std::env::ArgsOs`].\
/// On the web the arguments come from [`cliw::url_args::UrlArgs`].
///
/// Also some functions to print the help and long help.
///
/// Use these instead of their clap counterparts to make your command
/// line program work on native or the web.
///
/// # Example
/// ``` rust
/// use clap::Command; // Use clap to parse the arguments
/// use we_clap::WeCommand; // Use we_clap to provide the arguments to clap.
///
/// // Create your clap command.
/// let mut cli = Command::new("Native and Web Program");
///
/// // Use WeCommand function instead of get_matches.
/// let matches = &cli.we_get_matches();
/// ```
pub trait WeCommand {
    /// # Wrapper for [`clap::Command::get_matches()`]
    ///
    /// Gets command line arguments on native or the web.\
    /// Native args are from [`std::env::ArgsOs`].\
    /// Web args are from [`cliw::url_args::UrlArgs`].\
    ///
    /// # Panics
    ///
    /// May panic if contradictory arguments or settings exist (debug builds).
    /// This is normal clap behaviour.
    ///
    /// # Exit
    ///
    /// This functon may call [`std::process::exit()`] after printing messages
    /// if command line arguments are wrong or a help or version type argument
    /// is given.  This is normal clap behaviour.
    ///
    /// # Example
    /// ``` rust
    /// use clap::Command; // Use clap to parse the arguments.
    /// use we_clap::WeCommand; // Use we_clap to provide the arguments.
    ///
    /// let mut cli = Command::new("Native and Web Program");
    ///
    /// // Use WeCommand function instead of clap get_matches
    /// let matches = &cli.we_get_matches();
    /// ```
    #[must_use]
    fn we_get_matches(self) -> ArgMatches;

    /// # Wrapper for [`clap::Command::get_matches_mut()`]
    ///
    /// Like [`we_get_matches`](crate::WeCommand::we_get_matches()) but
    /// doesn't consume the `Command`.
    ///
    /// Gets command line arguments on native or the web.\
    /// Native args are from [`std::env::ArgsOs`].\
    /// Web args are from [`cliw::url_args::UrlArgs`].\
    ///
    /// # Panics
    ///
    /// May panic if contradictory arguments or settings exist (debug builds).
    /// This is normal clap behaviour.
    ///
    /// # Exit
    ///
    /// This functon may call [`std::process::exit()`] after printing messages if
    /// command line arguments are wrong or a help or version type argument is given.
    /// This is normal clap behaviour.
    ///
    /// # Example
    /// ``` rust
    /// use clap::Command; // Use clap to parse the arguments
    /// use we_clap::WeCommand; // Use we_clap to provide the arguments to clap.
    ///
    /// let mut cli = Command::new("Native and Web Program");
    ///
    /// // use WeCommand function instead of clap get_matches_mut
    /// let matches = cli.we_get_matches_mut(); // use WeCommand function instead of get_matches
    /// ```
    #[must_use]
    fn we_get_matches_mut(&mut self) -> ArgMatches;

    /// # Wrapper for [`clap::Command::try_get_matches()`]
    ///
    /// Gets command line arguments on native or the web.\
    /// Native args are from [`std::env::ArgsOs`].\
    /// Web args are from [`cliw::url_args::UrlArgs`].\
    ///
    /// # Panics
    ///
    /// May panic if contradictory arguments or settings exist (debug builds).
    /// This is normal clap behaviour.
    ///
    /// # Errors
    ///
    /// If help or version type arguments are entered than [`clap::error`]
    /// will be returned instead of [`clap::ArgMatches`].
    /// This is normal clap behaviour.
    ///
    /// # Example
    ///
    /// ``` rust
    /// use clap::Command; // Use clap to parse the arguments
    /// use we_clap::WeCommand; // Use we_clap to provide the arguments.
    ///
    /// let mut cli = Command::new("Native and Web Program");
    ///
    /// // use WeCommand function instead of clap try_get_matches
    /// match cli.we_try_get_matches() {
    ///     Ok(cli) => {} // handle cli
    ///     Err(err) => {} // handle error
    /// }
    /// ```
    fn we_try_get_matches(self) -> error::Result<ArgMatches>;

    /// # Print help message
    /// Prints a help message on native or the web.\
    /// Native output is to stdout/stderr.\
    /// Web output is to console or popup alert
    ///
    /// # Errors
    /// On native a [`Result`] is returned from
    /// [`clap::Command::print_help()`].  This is normal clap behaviour.
    ///
    /// On the web ouput errors are ignored.  Ok(()) is always returned
    ///
    /// # Example
    /// ``` rust
    /// use clap::Command; // Use clap to parse the arguments
    /// use we_clap::WeCommand; // Use we_clap to provide the arguments to clap.
    ///
    /// let mut cli = Command::new("Native and Web Program");
    ///
    /// // use WeCommand print function instead of clap print_help
    /// let result = cli.we_print_help();
    /// ```
    fn we_print_help(&mut self) -> std::io::Result<()>;

    /// # Print long help message
    /// Prints a long help message on native or the web.\
    /// Native output is to stdout/stderr.\
    /// Web output is to console or popup alert
    ///
    /// # Errors
    /// On native the [`Result`] is returned from
    /// [`clap::Command::print_help()`].   This is normal clap behaviour.
    ///
    /// On the web ouput errors are ignored.  Ok(()) is always returned
    ///
    /// # Example
    /// ``` rust
    /// use clap::Command; // Use clap to parse the arguments
    /// use we_clap::WeCommand; // Use we_clap to provide the arguments.
    ///
    /// let mut cli = Command::new("Native and Web Program");
    ///
    /// // use WeCommand print function instead of clap print_help
    /// let result = cli.we_print_long_help();
    /// ```
    fn we_print_long_help(&mut self) -> std::io::Result<()>;
}

impl WeCommand for Command {
    fn we_get_matches(self) -> ArgMatches {
        #[cfg(not(target_arch = "wasm32"))]
        {
            self.get_matches()
        }
        #[cfg(target_arch = "wasm32")]
        {
            let command = self.try_get_matches_from(UrlArgs::new());
            match command {
                Ok(command) => command,
                Err(err) => {
                    let msg = format!("{err}");
                    match err.kind() {
                        ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => {
                            cliw::output::print(&msg);
                        }
                        _ => {
                            cliw::output::eprint(&msg);
                        }
                    }
                    std::process::exit(0); // Exit code meaningless on wasm.
                }
            }
        }
    }
    fn we_get_matches_mut(&mut self) -> ArgMatches {
        #[cfg(not(target_arch = "wasm32"))]
        {
            self.get_matches_mut()
        }
        #[cfg(target_arch = "wasm32")]
        {
            let command = self.try_get_matches_from_mut(UrlArgs::new());
            match command {
                Ok(command) => command,
                Err(err) => {
                    let msg = format!("{err}");
                    match err.kind() {
                        ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => {
                            cliw::output::print(&msg);
                        }
                        _ => {
                            cliw::output::eprint(&msg);
                        }
                    }
                    std::process::exit(0); // Exit code meaningless on wasm.
                }
            }
        }
    }
    fn we_try_get_matches(self) -> error::Result<ArgMatches> {
        #[cfg(not(target_arch = "wasm32"))]
        {
            self.try_get_matches()
        }
        #[cfg(target_arch = "wasm32")]
        {
            self.try_get_matches_from(UrlArgs::new())
        }
    }

    fn we_print_help(&mut self) -> std::io::Result<()> {
        #[cfg(not(target_arch = "wasm32"))]
        {
            self.print_help()
        }

        #[cfg(target_arch = "wasm32")]
        {
            let styled = &self.render_help();
            cliw::output::print(&format!("{styled}"));
            Ok(())
        }
    }

    fn we_print_long_help(&mut self) -> std::io::Result<()> {
        #[cfg(not(target_arch = "wasm32"))]
        {
            self.print_long_help()
        }

        #[cfg(target_arch = "wasm32")]
        {
            let styled = &self.render_long_help();
            cliw::output::print(&format!("{styled}"));
            Ok(())
        }
    }
}

/// # Wrapper trait for [`clap::Parser`]
///
/// Functions to parse your Opt with clap on native or the web.\
/// On native the arguments come from [`std::env::ArgsOs`].\
/// On the web the arguments come from [`cliw::url_args::UrlArgs`].
///
/// Use these instead of their clap counterparts to make your command
/// line program work on native or the web.
///
/// # Example
/// ``` rust
/// use clap::Parser; // Use clap to parse the arguments
/// use we_clap::WeParser; // Use we_clap to provide the arguments.
///
/// #[derive(Parser, Debug, Default)]
/// #[command(author, version, about, long_about)]
/// pub struct Opts {}
///
/// // Implement web enabled parser for your struct
/// impl we_clap::WeParser for Opts {}
///
/// // Use we_parse and it works on native or web.
/// // Type annotations needed.
/// let opts: Opts = Opts::we_parse();
/// ```
pub trait WeParser {
    /// # Wrapper for [`clap::Parser::parse()`]
    ///
    /// Gets command line arguments on native or the web.\
    /// Native args are from [`std::env::ArgsOs`].\
    /// Web args are from [`cliw::url_args::UrlArgs`].\
    ///
    /// # Panics
    ///
    /// May panic if contradictory arguments or settings exist (debug builds).
    /// This is normal clap behaviour.
    ///
    /// # Exit
    ///
    /// This functon may call [`std::process::exit()`] after printing messages if
    /// command line arguments are wrong or a help or version type argument is given.
    /// This is normal clap behaviour.
    ///
    /// # Example
    /// ``` rust
    /// use clap::Parser; // Use clap to parse the arguments
    /// use we_clap::WeParser; // Use we_clap to provide the arguments to clap.
    ///
    /// #[derive(Parser, Debug, Default)]
    /// #[command(author, version, about, long_about)]
    /// pub struct Opts {}
    ///
    /// // Implement web enabled parser for your struct
    /// impl we_clap::WeParser for Opts {}
    ///
    /// // use we_parse and it works on native or web.
    /// // Type annotations needed
    /// let opts: Opts = Opts::we_parse();
    /// ```
    #[must_use]
    fn we_parse<T>() -> T
    where
        T: Parser,
    {
        #[cfg(not(target_arch = "wasm32"))]
        {
            Parser::parse()
        }
        #[cfg(target_arch = "wasm32")]
        {
            let opts = Parser::try_parse_from(UrlArgs::new());
            match opts {
                Ok(opts) => opts,
                Err(err) => {
                    let msg = format!("{err}");
                    match err.kind() {
                        ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => {
                            cliw::output::print(&msg);
                        }
                        _ => {
                            cliw::output::eprint(&msg);
                        }
                    }

                    std::process::exit(0); // Exit code meaningless on wasm.
                }
            }
        }
    }
    /// # Wrapper for [`clap::Parser::try_parse()`]
    ///
    /// Gets command line arguments on native or the web.\
    /// Native args are from [`std::env::ArgsOs`].\
    /// Web args are from [`cliw::url_args::UrlArgs`].\
    ///
    /// # Panics
    ///
    /// May panic if contradictory arguments or settings exist (debug builds).
    /// This is normal clap behaviour.
    ///
    /// # Errors
    ///
    /// If help or version type arguments are entered than [`clap::error`]
    /// will be returned.  This is normal clap behaviour.
    ///
    /// # Example
    /// ``` rust
    /// use clap::Parser; // Use clap to parse the arguments
    /// use we_clap::WeParser; // Use we_clap to provide the arguments.
    ///
    /// #[derive(Parser, Debug, Default)]
    /// #[command(author, version, about, long_about)]
    /// pub struct Opts {}
    ///
    /// // Implement web enabled parser for your struct
    /// impl we_clap::WeParser for Opts {}
    ///
    /// // use we_parse and it works on native or web.
    /// // Type annotations needed
    /// match Opts::we_try_parse::<Opts>() {
    ///     Ok(opts) => {} // handle opts
    ///     Err(err) => {} // handle error
    /// }
    fn we_try_parse<T>() -> Result<T, error::Error>
    where
        T: Parser,
    {
        #[cfg(target_arch = "wasm32")]
        {
            Parser::try_parse_from(UrlArgs::new())
        }
        #[cfg(not(target_arch = "wasm32"))]
        {
            Parser::try_parse()
        }
    }
}

#[cfg(test)]
mod tests {
    //   use super::*;
    //   lazy: let us only use what we need.
    //   Or maybe we do need to use everything because we test everything ?
    /*
        #[test]
        fn it_works() {
            let result = add(2, 2);
            assert_eq!(result, 4);
        }
    */
}