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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874

//! # [app](https://github.com/biluohc/app-rs)
//!  A easy-to-use command-line-parser.
//!

//! ## Usage
//!
//! On Cargo.toml:
//!
//! ```toml
//!  [dependencies]
//!  app = "^0.3.1"
//! ```
//! or
//!
//! ```toml
//!  [dependencies]
//!  app = { git = "https://github.com/biluohc/app-rs",branch = "master", version = "^0.3.1" }
//! ```
//!
//! ## Examples
//! * [fht2p](https://github.com/biluohc/app-rs/blob/master/examples/fht2p.rs)
//! * [zipcs](https://github.com/biluohc/zipcs)

#[macro_use]
extern crate stderr;
use stderr::Loger;

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::collections::BTreeMap as Map;
use std::process::exit;
use std::fmt::Debug;
use std::env;

static mut HELP: bool = false;
static mut HELP_SUBCMD: bool = false;
static mut VERSION: bool = false;

/// application struct
#[derive(Debug,Default)]
pub struct App<'app> {
    name: &'app str,
    version: &'app str,
    authors: Vec<(&'app str, &'app str)>, // (name,email)
    addrs: Vec<(&'app str, &'app str)>, // (addr_name,addr)
    // env_vars
    current_exe: Option<String>,
    current_dir: Option<String>,
    home_dir: Option<String>,
    temp_dir: String,
    //commands
    main: Cmd<'app>,
    current_cmd: Option<&'app mut String>, //main is None
    sub_cmds: Map<String, Cmd<'app>>, // name,_
}

impl<'app> App<'app> {
    pub fn new<'s: 'app>(name: &'s str) -> Self {
        init!();
        let mut app = Self::default();
        app.name = name;
        app = unsafe {
            app.opt(Opt::new("help", &mut HELP)
                        .short("h")
                        .long("help")
                        .help("show the help message"))
        };
        app = unsafe {
            app.opt(Opt::new("version", &mut VERSION)
                        .short("v")
                        .long("version")
                        .help("show the version message"))
        };
        app
    }
    pub fn version<'s: 'app>(mut self, version: &'s str) -> Self {
        self.version = version;
        self
    }
    pub fn desc<'s: 'app>(mut self, desc: &'s str) -> Self {
        self.main.desc = desc;
        self
    }
    pub fn author<'s: 'app>(mut self, name: &'s str, email: &'s str) -> Self {
        self.authors.push((name, email));
        self
    }
    pub fn addr<'s: 'app>(mut self, name: &'s str, url: &'s str) -> Self {
        self.addrs.push((name, url));
        self
    }
    pub fn opt(mut self, opt: Opt<'app>) -> Self {
        self.main = self.main.opt(opt);
        self
    }
    pub fn args<'s: 'app, S>(mut self, name: S, value: &'s mut Vec<String>) -> Self
        where S: Into<String>
    {
        self.main.args = Some(value);
        self.main.args_name = name.into();
        self
    }
    pub fn cmd(mut self, mut cmd: Cmd<'app>) -> Self {
        let name = cmd.name.to_string();
        cmd = unsafe {
            cmd.opt(Opt::new("help", &mut HELP_SUBCMD)
                        .short("h")
                        .long("help")
                        .help("show the help message"))
        };
        if self.sub_cmds.insert(name.clone(), cmd).is_some() {
            panic!("sub_command: \"{}\" already defined", name);
        }
        if self.current_cmd.is_none() {
            panic!("current_cmd's value no defined");
        }
        self
    }
    pub fn current_cmd<'s: 'app>(mut self, value: &'s mut String) -> Self {
        self.current_cmd = Some(value);
        self
    }
}
impl<'app> App<'app> {
    /// `parse_string(std::env::args()[1..])` and `exit(1)` if parse fails.
    pub fn parse(&mut self) {
        let mut args = env::args();
        args.next();
        let args: Vec<String> = args.collect();
        if let Err(e) = self.parse_strings(args) {
            errln!("ERROR:\n  {}\n", e);
            if self.current_cmd_get().is_some() && self.current_cmd_get() != Some(&mut String::new()) {
                if let Some(ref s) = self.current_cmd_get() {
                    self.help_cmd(s);
                }
            } else {
                self.help();
            }
            exit(1);
        }
    }
    pub fn parse_strings(&mut self, args: Vec<String>) -> Result<(), String> {
        dbstln!("args: {:?}", args);
        self.current_exe = env::current_exe()
            .map(|s| s.to_string_lossy().into_owned())
            .ok();
        self.current_dir = env::current_dir()
            .map(|s| s.to_string_lossy().into_owned())
            .ok();
        self.home_dir = env::home_dir().map(|s| s.to_string_lossy().into_owned());
        self.temp_dir = env::temp_dir().to_string_lossy().into_owned();
        let commands: Vec<String> = self.sub_cmds.keys().map(|s| s.to_string()).collect();
        let (mut idx, mut sub_cmd_name) = (std::usize::MAX, "");
        'out: for i in 0..args.len() {
            for cmd in &commands {
                if args[i] == **cmd {
                    idx = i;
                    sub_cmd_name = args[i].as_str();
                    break 'out;
                }
            }
        }
        if let Some(ref mut s) = self.current_cmd {
            s.clear();
            s.push_str(sub_cmd_name);
        }
        if idx != std::usize::MAX {
            self.main.parse(&args[0..idx])?;
            self.sub_cmds
                .get_mut(sub_cmd_name)
                .unwrap()
                .parse(&args[idx + 1..])?;
        } else {
            self.main.parse(&args[..])?;
        }
        if unsafe { HELP } {
            self.help();
            exit(0);
        } else if unsafe { HELP_SUBCMD } {
            self.help_cmd(sub_cmd_name);
            exit(0);
        } else if unsafe { VERSION } {
            self.ver();
            exit(0);
        }
        // check main.args
        if let Some(ref s) = self.main.args {
            if s.is_empty() {
                return Err(format!("Arguments({}) missing", self.main.args_name));
            }
        }
        // check main.opts
        for (_, opt) in &self.main.opts {
            opt.value.inner.check(opt.name_get())?;
        }
        // check current_cmd
        if sub_cmd_name != "" {
            let cmd = self.sub_cmds.get(sub_cmd_name).unwrap();
            if let Some(ref s) = cmd.args {
                if s.is_empty() {
                    return Err(format!("Arguments({}) missing", cmd.args_name));
                }
            }
            for (_, opt) in &cmd.opts {
                opt.value.inner.check(opt.name_get())?;
            }
        }
        // No Args
        if args.is_empty() && self.main.args.is_none() {
            if self.sub_cmds.is_empty() {
                return Err("OPTION missing".to_owned());
            } else {
                return Err("OPTION/COMMAND missing".to_owned());
            }
        }
        Ok(())
    }
    fn ver(&self) {
        println!("{}  {}", self.name.trim(), self.version.trim());
    }
    // NAME
    fn help_name(&self) -> String {
        format!("NAME:\n  {} - {}\n  {}\n",
                self.name.trim(),
                self.version.trim(),
                self.main.desc.trim())
    }
    fn help_global_opt(&self) -> String {
        // GLOBAL OPTIONS:
        let mut help = String::new();
        if !self.main.opts.is_empty() {
            help += &{
                         let mut tmp = "\nGLOBAL OPTIONS:\n".to_owned();
                         let mut vs: Vec<(String, &str)> = Vec::new();
                         let mut len = 0;
                         for (k, v) in &self.main.opts {
                             let s = v.short_get().unwrap_or_else(String::new);
                             let long = v.long_get().unwrap_or_else(String::new);
                             let tmp_ = if v.is_bool() {
                                 if s != "" && long != "" {
                                     format!("   {},{}  ", long, s)
                                 } else {
                                     format!("   {}{}  ", long, s)
                                 }
                             } else if s != "" && long != "" {
                        format!("   {} {},{} {}  ", long, k, s, k)
                    } else {
                        format!("   {}{} {}  ", s, long, k)
                    };
                             if tmp_.len() > len {
                                 len = tmp_.len();
                             }
                             vs.push((tmp_, v.help_get()));
                         }
                         for (k, v) in vs {
                             let mut tmp_ = k.clone();
                             for _ in tmp_.len()..len {
                                 tmp_.push(' ');
                             }
                             tmp += &format!("{}  {}\n", tmp_, v.trim());
                         }
                         tmp
                     }
        }
        help
    }
    pub fn help(&self) {
        // NAME
        let mut help = self.help_name();
        //Author
        if !self.authors.is_empty() {
            help += &{
                         let mut authors = String::new() + "\nAUTHOR:\n";
                         for &(author, email) in &self.authors {
                             authors += &format!("  {} <{}>\n", author, email);
                         }
                         authors
                     };
        }
        //ADDRESS
        if !self.addrs.is_empty() {
            help += &{
                         let mut authors = String::new() + "\nADDRESS:\n";
                         for &(author, email) in &self.addrs {
                             authors += &format!("  {}: {}\n", author, email);
                         }
                         authors
                     };
        }
        //USAGE
        //    zipcs [global options] [global arguments] command [command options] [arguments...]
        help += &{
                     let mut tmp = format!("\nUSAGE:\n  {}", self.name);
                     if !self.main.opts.is_empty() {
                         tmp += " [global options]";
                     }
                     if self.main.args.is_some() {
                         tmp += &format!(" [{}...]", self.main.args_name);
                     }
                     if !self.sub_cmds.is_empty() {
                         tmp += " command [command options] [arguments...]";
                     }
                     tmp + "\n"
                 };
        // GLOBAL OPTIONS:
        help += &self.help_global_opt();
        // SUBCOMMANDS
        if !self.sub_cmds.is_empty() {
            help += &{
                         let mut tmp = "\nCOMMANDS:\n".to_owned();
                         let mut vs: Vec<(String, &str)> = Vec::new();
                         let mut len = 0;
                         for (k, v) in &self.sub_cmds {
                             let tmp_ = format!("    {}  ", k);
                             if tmp_.len() > len {
                                 len = tmp_.len();
                             }
                             vs.push((tmp_, v.desc));
                         }
                         for (k, v) in vs {
                             let mut tmp_ = k.clone();
                             for _ in tmp_.len()..len {
                                 tmp_.push(' ');
                             }
                             tmp += &format!("{}  {}\n", tmp_, v.trim());
                         }
                         tmp
                     };
        }
        println!("{}", help.trim());
    }
    pub fn help_cmd(&self, sub_cmd_name: &str) {
        // NAME
        let mut help = self.help_name();
        //USAGE
        //    zipcs [global options] [global arguments] command [command options] [arguments...]
        help += &{
                     let mut tmp = format!("\nUSAGE:\n  {}", self.name);
                     if !self.main.opts.is_empty() {
                         tmp += " [global options]";
                     }
                     if self.main.args.is_some() {
                         tmp += &format!(" [{}...]", self.main.args_name);
                     }
                     if let Some(s) = self.sub_cmds.get(sub_cmd_name) {
                         if !s.opts.is_empty() && s.args.is_some() {
                             tmp += &format!(" {} [{} options] [{}...]", s.name, s.name, s.args_name);
                         } else if !s.opts.is_empty() && s.args.is_none() {
                    tmp += &format!(" {} [{} options]", s.name, s.name);
                } else if s.opts.is_empty() && s.args.is_some() {
                    tmp += &format!(" {} [{}...]", s.name, s.args_name);
                }
                     }
                     tmp + "\n"
                 };
        // GLOBAL OPTIONS:
        help += &self.help_global_opt();
        // SubCMD OPTIONS
        let cmd = self.sub_cmds.get(sub_cmd_name).unwrap();
        if !cmd.opts.is_empty() {
            help += &{
                         let mut tmp = "\nOPTIONS:\n".to_owned();
                         let mut vs: Vec<(String, &str)> = Vec::new();
                         let mut len = 0;
                         for (k, v) in &cmd.opts {
                             let s = v.short_get().unwrap_or_else(String::new);
                             let long = v.long_get().unwrap_or_else(String::new);
                             let tmp_ = if v.is_bool() {
                                 if s != "" && long != "" {
                                     format!("    {},{}  ", long, s)
                                 } else {
                                     format!("    {}{}  ", long, s)
                                 }
                             } else if s != "" && long != "" {
                        format!("    {} {},{} {}  ", long, k, s, k)
                    } else {
                        format!("    {}{} {}  ", s, long, k)
                    };
                             if tmp_.len() > len {
                                 len = tmp_.len();
                             }
                             vs.push((tmp_, v.help_get()));
                         }
                         for (k, v) in vs {
                             let mut tmp_ = k.clone();
                             for _ in tmp_.len()..len {
                                 tmp_.push(' ');
                             }
                             tmp += &format!("{}  {}\n", tmp_, v.trim());
                         }
                         tmp
                     }
        }
        println!("{}", help.trim());
    }
    pub fn current_cmd_get(&self) -> Option<&str> {
        if let Some(ref s) = self.current_cmd {
            Some(s)
        } else {
            None
        }
    }
    pub fn current_exe(&self) -> Option<&String> {
        self.current_exe.as_ref()
    }
    pub fn current_dir(&self) -> Option<&String> {
        self.current_dir.as_ref()
    }
    pub fn home_dir(&self) -> Option<&String> {
        self.home_dir.as_ref()
    }
    pub fn temp_dir(&self) -> &String {
        &self.temp_dir
    }
}

///Command strcut
#[derive(Debug,Default)]
pub struct Cmd<'app> {
    name: &'app str,
    desc: &'app str,
    opts: Map<String, Opt<'app>>,
    str_to_name: Map<String, String>, //-short/--long to name
    args_name: String,
    args: Option<&'app mut Vec<String>>,
}
impl<'app> Cmd<'app> {
    pub fn new<'s: 'app>(name: &'s str) -> Self {
        let mut c = Self::default();
        c.name = name;
        c
    }
    pub fn desc<'s: 'app>(mut self, desc: &'s str) -> Self {
        self.desc = desc;
        self
    }
    pub fn args<'s: 'app, S>(mut self, name: S, value: &'s mut Vec<String>) -> Self
        where S: Into<String>
    {
        self.args = Some(value);
        self.args_name = name.into();
        self
    }
    pub fn opt(mut self, opt: Opt<'app>) -> Self {
        let long = opt.long_get();
        let short = opt.short_get();
        let name = opt.name_get().to_string();
        if long.is_none() && short.is_none() {
            panic!("OPTION: \"{}\" don't have --{} and -{} all",
                   name,
                   name,
                   name);
        }
        if let Some(ref s) = long {
            if self.str_to_name
                   .insert(s.clone(), name.clone())
                   .is_some() {
                panic!("long: \"{}\" already defined", s);
            }
        }
        if let Some(ref s) = short {
            if self.str_to_name
                   .insert(s.clone(), name.clone())
                   .is_some() {
                panic!("short: \"{}\" already defined", s);
            }
        }
        if self.opts.insert(name.clone(), opt).is_some() {
            panic!("name: \"{}\" already defined", name);
        }
        self
    }
    fn parse(&mut self, args: &[String]) -> Result<(), String> {
        let mut i = 0;
        for _ in 0..args.len() {
            if i >= args.len() {
                break;
            }
            let arg = &args[i];
            // println!("i+1/args_len: {}/{}: {:?}", i + 1, args.len(), &args[i..]);
            match arg {
                s if s.starts_with("--") => {
                    if let Some(opt_name) = self.str_to_name.get(s.as_str()) {
                        let mut opt = self.opts.get_mut(opt_name).unwrap();
                        let opt_is_bool = opt.is_bool();
                        if !opt_is_bool && args.len() > i + 1 {
                            opt.parse(&args[i + 1])?;
                            i += 2;
                        } else if opt_is_bool {
                            opt.parse("")?;
                            i += 1;
                        } else {
                            return Err(format!("OPTION({})'s value missing", s));
                        }
                    } else {
                        return Err(format!("OPTION: \"{}\" not defined", s));
                    }
                }
                s if s.starts_with('-') => {
                    if let Some(opt_name) = self.str_to_name.get(s.as_str()) {
                        let mut opt = self.opts.get_mut(opt_name).unwrap();
                        let opt_is_bool = opt.is_bool();
                        if !opt_is_bool && args.len() > i + 1 {
                            opt.parse(&args[i + 1])?;
                            i += 2;
                        } else if opt_is_bool {
                            opt.parse("")?;
                            i += 1;
                        } else {
                            return Err(format!("OPTION({})'s value missing", s));
                        }
                    } else {
                        return Err(format!("OPTION: \"{}\" not defined", s));
                    }
                }
                s => {
                    if let Some(ref mut ss) = self.args {
                        ss.push(s.to_string());
                        i += 1;
                    } else {
                        return Err(format!("Argument: \"{}\" no need", s));
                    }
                }
            }
        }
        Ok(())
    }
}

/// Option struct
#[derive(Debug)]
pub struct Opt<'app> {
    name: &'app str,
    value: OptValue<'app>,
    short: Option<&'app str>,
    long: Option<&'app str>,
    help: &'app str,
}
impl<'app> Opt<'app> {
    pub fn new<'s: 'app, V>(name: &'app str, value: V) -> Self
        where V: OptValueParse<'app>
    {
        Opt {
            value: value.into_opt_value(),
            name: name,
            short: None,
            long: None,
            help: "",
        }
    }
    pub fn short(mut self, short: &'app str) -> Self {
        self.short = Some(short);
        self
    }
    pub fn long(mut self, long: &'app str) -> Self {
        self.long = Some(long);
        self
    }
    pub fn help(mut self, help: &'app str) -> Self {
        self.help = help;
        self
    }
    fn parse(&mut self, msg: &str) -> Result<(), String> {
        let name = self.name_get().to_string();
        self.value.inner.parse(name, msg)
    }
}

impl<'app> Opt<'app> {
    pub fn is_bool(&self) -> bool {
        self.value.inner.is_bool()
    }
    pub fn name_get(&self) -> &'app str {
        self.name
    }
    pub fn short_get(&self) -> Option<String> {
        self.short.map(|s| "-".to_owned() + s)
    }
    pub fn long_get(&self) -> Option<String> {
        self.long.map(|s| "--".to_owned() + s)
    }
    pub fn help_get(&self) -> &str {
        self.help
    }
}

/// `OptValue` struct
#[derive(Debug)]
pub struct OptValue<'app> {
    pub inner: Box<OptValueParse<'app> + 'app>,
}

/// ## You can use custom `OptValue` by `impl` it
///
/// ### Explain
///
/// * `into_opt_value(self)` convert it(`&mut T`)  to `OptValue`.
///
///
/// * `is_bool(&self)` like `--help/-h`,they not have value follows it.
///
///    so you should return `false` except value's type is `&mut bool`(it already defined).
///
///
/// * `parse(&mut self, opt_name: String, msg: &str)` maintains the value, and return message by `Result<(),String>`.
///
///   `opt_name` is current `Opt`'s name, `msg` is `&str` need to pasre.
///
/// * `check(&self, opt_name: &str)` check value  and return message by `Result<(),String>`.
///
/// ### Suggestion
///
/// * `T` is suitable for options with default values.
///
///     You can initialize it using the default value.
///
/// * `Option<T>` is suitable for necessary options.
///
///     `app` will `check` them, is `value.is_none()`, `app` will `exit(1)` after print error and help message.
///
/// * `Vec<T>` is suitable a grout of comma-separated values of the same type.
///
///     `app` will `check` them, is `value.is_empty()`, `app` will `exit(1)` after print error and help message.
///
///     You can initialize it using the default value.
///
/// ```fuckrs
/// "80" -> vec[80]
/// ",80," -> vec[80]
/// ",80,," -> vec[80]
/// "8080,8000,80," -> Vec[8080,8000,80]
/// ```

pub trait OptValueParse<'app>: Debug {
    fn into_opt_value(self) -> OptValue<'app>;
    fn is_bool(&self) -> bool;
    fn parse(&mut self, opt_name: String, msg: &str) -> Result<(), String>;
    fn check(&self, opt_name: &str) -> Result<(), String>;
}

impl<'app, 's: 'app> OptValueParse<'app> for &'s mut bool {
    fn into_opt_value(self) -> OptValue<'app> {
        OptValue { inner: Box::new(self) }
    }
    fn is_bool(&self) -> bool {
        true
    }
    fn parse(&mut self, _: String, _: &str) -> Result<(), String> {
        **self = true;
        Ok(())
    }
    fn check(&self, _: &str) -> Result<(), String> {
        Ok(())
    }
}
impl<'app, 's: 'app> OptValueParse<'app> for &'s mut String {
    fn into_opt_value(self) -> OptValue<'app> {
        OptValue { inner: Box::new(self) }
    }
    fn is_bool(&self) -> bool {
        false
    }
    fn parse(&mut self, _: String, msg: &str) -> Result<(), String> {
        **self = msg.to_string();
        Ok(())
    }
    fn check(&self, opt_name: &str) -> Result<(), String> {
        if self.is_empty() {
            Err(format!("OPTION({})'s value missing", opt_name))
        } else {
            Ok(())
        }
    }
}

impl<'app, 's: 'app> OptValueParse<'app> for &'s mut char {
    fn into_opt_value(self) -> OptValue<'app> {
        OptValue { inner: Box::new(self) }
    }
    fn is_bool(&self) -> bool {
        false
    }
    fn parse(&mut self, opt_name: String, msg: &str) -> Result<(), String> {
        if msg.len() == 1 {
            **self = msg.chars().next().unwrap();
        } else {
            return Err(format!("OPTION({}) parse<char> fails: \"{}\"", opt_name, msg));
        }
        Ok(())
    }
    fn check(&self, _: &str) -> Result<(), String> {
        Ok(())
    }
}

macro_rules! add_impl {
    ($($t:ty)*) => ($(
        impl<'app, 's: 'app> OptValueParse<'app> for &'s mut $t {
        fn into_opt_value(self) -> OptValue<'app> {
        OptValue { inner: Box::new(self) }
    }
    fn is_bool(&self) -> bool {
        false
    }
    fn parse(&mut self, opt_name : String, msg: &str) -> Result<(), String> {
        **self = msg.parse::<$t>()
                .map_err(|_| format!("OPTION({}) parse<{}> fails: \"{}\"", opt_name, stringify!($t),msg))?;
                Ok(())
    }
    fn check(&self, _ :  &str) -> Result<(), String> {
        Ok(())
    }
        }
    )*)
}

add_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
add_impl! { IpAddr Ipv4Addr Ipv6Addr SocketAddr SocketAddrV4 SocketAddrV6 }

impl<'app, 's: 'app> OptValueParse<'app> for &'s mut Option<char> {
    fn into_opt_value(self) -> OptValue<'app> {
        OptValue { inner: Box::new(self) }
    }
    fn is_bool(&self) -> bool {
        false
    }
    fn parse(&mut self, opt_name: String, msg: &str) -> Result<(), String> {
        if msg.len() == 1 {
            **self = Some(msg.chars().next().unwrap());
        } else {
            return Err(format!("OPTION({}) parse<char> fails: \"{}\"", opt_name, msg));
        }
        Ok(())
    }
    fn check(&self, opt_name: &str) -> Result<(), String> {
        if self.is_none() {
            Err(format!("OPTION({})'s value missing", opt_name))
        } else {
            Ok(())
        }
    }
}

impl<'app, 's: 'app> OptValueParse<'app> for &'s mut Option<String> {
    fn into_opt_value(self) -> OptValue<'app> {
        OptValue { inner: Box::new(self) }
    }
    fn is_bool(&self) -> bool {
        false
    }
    fn parse(&mut self, _: String, msg: &str) -> Result<(), String> {
        **self = Some(msg.to_string());
        Ok(())
    }
    fn check(&self, opt_name: &str) -> Result<(), String> {
        if self.is_none() {
            Err(format!("OPTION({})'s value missing", opt_name))
        } else {
            Ok(())
        }
    }
}

macro_rules! add_option_impl {
    ($($t:ty)*) => ($(
impl<'app, 's: 'app> OptValueParse<'app> for &'s mut Option<$t> {
    fn into_opt_value(self) -> OptValue<'app> {
        OptValue { inner: Box::new(self) }
    }
    fn is_bool(&self) -> bool {
        false
    }
    fn parse(&mut self, opt_name: String, msg: &str) -> Result<(), String> {
        **self = Some(msg.parse::<$t>()
                          .map_err(|_| {
                                       format!("OPTION({}) parse<{}> fails: \"{}\"",
                                               opt_name,
                                               stringify!($t),
                                               msg)
                                   })?);
        Ok(())
    }
    fn check(&self, opt_name: &str) -> Result<(), String> {
        if self.is_none() {
            Err(format!("OPTION({})'s value missing", opt_name))
        } else {
            Ok(())
        }
    }
}
    )*)
}

add_option_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
add_option_impl! { IpAddr Ipv4Addr Ipv6Addr SocketAddr SocketAddrV4 SocketAddrV6 }

impl<'app, 's: 'app> OptValueParse<'app> for &'s mut Vec<char> {
    fn into_opt_value(self) -> OptValue<'app> {
        OptValue { inner: Box::new(self) }
    }
    fn is_bool(&self) -> bool {
        false
    }
    fn parse(&mut self, _: String, msg: &str) -> Result<(), String> {
        self.clear();
        for c in msg.chars() {
            self.push(c);
        }
        Ok(())
    }
    fn check(&self, _: &str) -> Result<(), String> {
        Ok(())
    }
}

impl<'app, 's: 'app> OptValueParse<'app> for &'s mut Vec<String> {
    fn into_opt_value(self) -> OptValue<'app> {
        OptValue { inner: Box::new(self) }
    }
    fn is_bool(&self) -> bool {
        false
    }
    fn parse(&mut self, _: String, msg: &str) -> Result<(), String> {
        self.clear(); // What due to ?
        let _ = msg.split(',')
            .filter(|s| !s.is_empty())
            .map(|ss| self.push(ss.to_string()));
        Ok(())
    }
    fn check(&self, opt_name: &str) -> Result<(), String> {
        if self.is_empty() {
            Err(format!("OPTION({})'s value missing", opt_name))
        } else {
            Ok(())
        }
    }
}

macro_rules! add_vec_impl {
    ($($t:ty)*) => ($(
        impl<'app, 's: 'app> OptValueParse<'app> for &'s mut Vec<$t> {
        fn into_opt_value(self) -> OptValue<'app> {
        OptValue { inner: Box::new(self) }
    }
    fn is_bool(&self) -> bool {
        false
    }
    fn parse(&mut self, opt_name: String, msg: &str) -> Result<(), String> {
                self.clear();
                let vs: Vec<&str> = msg.split(',').filter(|s| !s.is_empty()).collect();
                for str in &vs {
                    self.push(str.parse::<$t>()
                               .map_err(|_| {
                                            format!("OPTION({}) parse<Vec<{}>> fails: \"{}/{}\"",
                                                    opt_name,
                                                    stringify!($t),
                                                    str,
                                                    msg)
                                        })?)
                }
                Ok(())
    }
    fn check(&self, opt_name: &str) -> Result<(), String> {
        if self.is_empty() {
          Err(format!("OPTION({})'s value missing", opt_name))
        } else {
            Ok(())
        }
    }
        }
    )*)
}
add_vec_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
add_vec_impl! { IpAddr Ipv4Addr Ipv6Addr SocketAddr SocketAddrV4 SocketAddrV6 }