clap 0.4.0

A Command Line Argument Parser written in Rust
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
extern crate libc;

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
use std::vec::IntoIter;

use argmatches::ArgMatches;
use Arg;
use args::OptArg;
use args::FlagArg;
use args::PosArg;
use subcommand::SubCommand;

/// Used to create a representation of the program and all possible command line arguments
/// for parsing at runtime.
///
///
/// Stores a list of all posisble arguments, as well as information displayed to the user such as
/// help and versioning information.
///
/// Example:
///
/// ```no_run
/// # use clap::{App, Arg};
/// let myprog = App::new("myprog")
///                   .author("Me, me@mail.com")
///	                  .version("1.0.2")
///                   .about("Explains in brief what the program does")
///                   .arg(
///							Arg::new("in_file").index(1)
///                        // Add other possible command line argument options here...
///                    )
///                   .get_matches();
///
/// // Your pogram logic starts here...
/// ```
pub struct App {
	/// The name displayed to the user when showing version and help/usage information
	pub name: &'static str,
	/// A string of author(s) if desired. Displayed when showing help/usage information
	pub author: Option<&'static str>,
	/// The version displayed to the user
	pub version: Option<&'static str>,
	/// A brief explaination of the program that gets displayed to the user when shown help/usage information
	pub about: Option<&'static str>,
	flags: HashMap<&'static str, FlagArg>,
	opts: HashMap<&'static str, OptArg>,
	positionals_idx: BTreeMap<u8, PosArg>,
	subcommands: HashMap<&'static str, Box<App>>,
	// positionals_name: HashMap<&'static str, PosArg>,
	needs_long_help: bool,
	needs_long_version: bool,
	needs_short_help: bool,
	needs_short_version: bool,
	needs_subcmd_help: bool,
	required: HashSet<&'static str>,
	arg_list: HashSet<&'static str>,
	short_list: HashSet<char>,
	long_list: HashSet<&'static str>,
	blacklist: HashSet<&'static str>,

}

impl App {
	/// Creates a new instance of an application requiring a name (such as the binary). Will be displayed
	/// to the user when they print version or help and usage information.
	///
	/// Example:
	///
	/// ```no_run
	/// # use clap::{App, Arg};
	/// let prog = App::new("myprog")
	/// # .get_matches();
	/// ```
	pub fn new(n: &'static str) -> App {
		App {
			name: n,
			author: None,
			about: None,
			version: None,
			flags: HashMap::new(),
			opts: HashMap::new(),
			positionals_idx: BTreeMap::new(),
			subcommands: HashMap::new(),
			// positionals_name: HashMap::new(),
			needs_long_version: true,
			needs_long_help: true,
			needs_short_help: true,
			needs_subcmd_help: true,
			needs_short_version: true,
			required: HashSet::new(), 
			arg_list: HashSet::new(),
			short_list: HashSet::new(),
			long_list: HashSet::new(),
			blacklist: HashSet::new(),
		}
	}

	/// Sets a string of author(s)
	///
	/// Example:
	///
	/// ```no_run
	/// # use clap::{App, Arg};
	/// # let app = App::new("myprog")
	/// .author("Kevin <kbknapp@gmail.com>")
	/// # .get_matches();
	/// ```
	pub fn author(mut self, a: &'static str) -> App {
		self.author = Some(a);
		self
	}

	/// Sets a string briefly describing what the program does
	///
	/// Example:
	///
	/// ```no_run
	/// # use clap::{App, Arg};
	/// # let app = App::new("myprog")
	/// .about("Does really amazing things to great people")
	/// # .get_matches();
	/// ```
	pub fn about(mut self, a: &'static str) -> App {
		self.about = Some(a);
		self
	}

	/// Sets a string of the version number
	///
	/// Example:
	///
	/// ```no_run
	/// # use clap::{App, Arg};
	/// # let app = App::new("myprog")
	/// .version("v0.1.24")
	/// # .get_matches();
	/// ```
	pub fn version(mut self, v: &'static str)-> App  {
		self.version = Some(v);
		self
	}

	/// Adds an argument to the list of valid possibilties
	///
	/// Example:
	///
	/// ```no_run
	/// # use clap::{App, Arg};
	/// # let app = App::new("myprog")
	/// .arg(Arg::new("config")
	///				.short("c")
	///     		// Additional argument configuration goes here...
	/// )
	/// # .get_matches();
	/// ```
	pub fn arg(mut self, a: Arg) -> App {
		if self.arg_list.contains(a.name) {
			panic!("Argument name must be unique, \"{}\" is already in use", a.name);
		} else {
			self.arg_list.insert(a.name);
		}
		if let Some(ref s) = a.short {
			if self.short_list.contains(s) {
				panic!("Argument short must be unique, -{} is already in use", s);
			} else {
				self.short_list.insert(*s);
			}
		}
		if let Some(ref l) = a.long {
			if self.long_list.contains(l) {
				panic!("Argument long must be unique, --{} is already in use", l);
			} else {
				self.long_list.insert(l);
			}
		}
		if a.required {
			self.required.insert(a.name);
		}
		if let Some(i) = a.index {
			self.positionals_idx.insert(i, PosArg {
				name: a.name,
				index: i,
				required: a.required,
				blacklist: a.blacklist,
				requires: a.requires,
				help: a.help,
				value: None
			});
		} else if a.takes_value {
			if a.short == None && a.long == None {
				panic!("An argument that takes a value must have either a .short() or .long() [or both] assigned");
			}
			self.opts.insert(a.name, OptArg {
				name: a.name,
				short: a.short,
				long: a.long,
				blacklist: a.blacklist,
				help: a.help,
				requires: a.requires,
				required: a.required,
				value: None
			});
		} else {
			if let Some(ref l) = a.long {
				if *l == "help" {
					self.needs_long_help = false;
				} else if *l == "version" {
					self.needs_long_version = false;
				}
			}
			if let Some(ref s) = a.short {
				if *s == 'h' {
					self.needs_short_help = false;
				} else if *s == 'v' {
					self.needs_short_version = false;
				}
			}
			if a.short == None && a.long == None {
				panic!("A flag argument must have either a .short() or .long() [or both] assigned");
			}
			// Flags can't be required
			if self.required.contains(a.name) {
				self.required.remove(a.name);
			}
			self.flags.insert(a.name, FlagArg{
				name: a.name,
				short: a.short,
				long: a.long,
				help: a.help,
				blacklist: a.blacklist,
				multiple: a.multiple,
				requires: a.requires,
				occurrences: 1
			});
		}
		self
	}

	/// Adds arguments to the list of valid possibilties
	///
	/// Example:
	///
	/// ```no_run
	/// # use clap::{App, Arg};
	/// # let app = App::new("myprog")
	/// .args( vec![Arg::new("config").short("c"),
	///				Arg::new("debug").short("d")])
	/// # .get_matches();
	/// ```
	pub fn args(mut self, args: Vec<Arg>) -> App {
		for arg in args.into_iter() {
			self = self.arg(arg);
		}
		self
	}

	pub fn subcommand(mut self, subcmd: App) -> App {
		if subcmd.name == "help" { self.needs_subcmd_help = false; }
		self.subcommands.insert(subcmd.name, Box::new(subcmd));
		self
	}

	pub fn subcommands(mut self, subcmds: Vec<App>) -> App {
		for subcmd in subcmds.into_iter() {
			self = self.subcommand(subcmd);
		}
		self
	}


	fn exit(&self) {
		unsafe { libc::exit(0); }
	}

	fn report_error(&self, msg: String, help: bool, quit: bool) {
		println!("{}", msg);
		if help { self.print_help(); }
		if quit { env::set_exit_status(1); self.exit(); }
	}

	fn print_help(&self) {
		self.print_version(false);
		let mut flags = false;
		let mut pos = false;
		let mut opts = false;
		let mut subcmds = false;

		if let Some(author) = self.author {
			println!("{}", author);
		}
		if let Some(about) = self.about {
			println!("{}", about);
		}
		println!("");
		println!("USAGE:");
		print!("\t{} {} {} {} {}", self.name,
			if ! self.subcommands.is_empty() {subcmds = true; "[SUBCOMMANDS]"} else {""},
			if ! self.flags.is_empty() {flags = true; "[FLAGS]"} else {""},
			if ! self.opts.is_empty() {opts = true; "[OPTIONS]"} else {""},
			if ! self.positionals_idx.is_empty() {pos = true; "[POSITIONAL]"} else {""});
		if flags || opts || pos || subcmds {
			println!("");
		}
		if flags {
			println!("");
			println!("FLAGS:");
			for v in self.flags.values() {
				println!("\t{}{}\t{}",
						if let Some(s) = v.short{format!("-{}",s)}else{format!("   ")},
						if let Some(l) = v.long {format!(",--{}",l)}else {format!("   \t")},
						if let Some(h) = v.help {h} else {"   "} );
			}
		}
		if opts {
			println!("");
			println!("OPTIONS:");
			for v in self.opts.values() {
				println!("\t{}{}{}\t\t{}",
						if let Some(ref s) = v.short{format!("-{}",s)}else{format!("   ")},
						if let Some(ref l) = v.long {format!(",--{}",l)}else {format!("   ")},
						format!(" <{}>", v.name),
						if let Some(ref h) = v.help {*h} else {"   "} );
			}
		}
		if pos {
			println!("");
			println!("POSITIONAL ARGUMENTS:");
			for v in self.positionals_idx.values() {
				println!("\t{}\t\t\t{}", v.name,
						if let Some(h) = v.help {h} else {"   "} );
			}
		}
		if subcmds {
			println!("");
			println!("SUBCOMMANDS:");
			for sc in self.subcommands.values() {
				println!("\t{}\t\t{}", sc.name,
					if let Some(a) = sc.about {a} else {"   "} );
			}
		}

		self.exit();
	}

	fn print_version(&self, quit: bool) {
		println!("{} {}", self.name, if let Some(v) = self.version {v} else {""} );
		if quit { self.exit(); }
	}

	fn check_for_help_and_version(&self, arg: char) {
		if arg == 'h' && self.needs_short_help {
			self.print_help();
		} else if arg == 'v' && self.needs_short_version {
			self.print_version(true);
		}
	}

	fn parse_long_arg(&mut self, matches: &mut ArgMatches ,full_arg: &String) -> Option<&'static str> {
		let mut arg = full_arg.as_slice().trim_left_matches(|c| c == '-');
		let mut found = false;

		if arg == "help" && self.needs_long_help {
			self.print_help();
		} else if arg == "version" && self.needs_long_version {
			self.print_version(true);
		}

		let mut arg_val: Option<String> = None;

		if arg.contains("=") {
			let arg_vec: Vec<&str> = arg.split("=").collect();
			arg = arg_vec[0];
			arg_val = Some(arg_vec[1].to_string());
		} 

		for (k, v) in self.opts.iter() {
			if let Some(ref l) = v.long {
				if *l == arg {
					if self.blacklist.contains(k) {
						self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg),
							false, true);
					}
					matches.opts.insert(k, OptArg{
						name: v.name,
					    short: v.short,
					    long: v.long, 
					    help: v.help,
					    required: v.required,
					    blacklist: None,
					    requires: None,
					    value: arg_val.clone() 
					});
					match arg_val {
						None => { return Some(v.name); },
						_ => { return None; }
					}	
				}
			}
		} 

		for (k, v) in self.flags.iter() {
			if let Some(ref l) = v.long {
				if *l != arg { continue; }
				found = true;
				let mut multi = false;
				if let Some(ref mut f) = matches.flags.get_mut(k) {
					f.occurrences = if f.multiple { f.occurrences + 1 } else { 1 };
					multi = true;
				}  
				if ! multi { 
					if self.blacklist.contains(k) {
						self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg),
							false, true);
					}
					matches.flags.insert(k, FlagArg{
					    name: v.name,
					    short: v.short,
					    long: v.long,
					    help: v.help,
					    multiple: v.multiple,
					    occurrences: v.occurrences,
					    blacklist: None, 
					    requires: None
					});
					if self.required.contains(k) {
						self.required.remove(k);
					}
					if let Some(ref bl) = v.blacklist {
						if ! bl.is_empty() {
							for name in bl.iter() {
								self.blacklist.insert(name);
							}
						}
					}
				}
				if let Some(ref reqs) = v.requires {
					if ! reqs.is_empty() {
						for n in reqs.iter() {
							if matches.opts.contains_key(n) { continue; }
							if matches.flags.contains_key(n) { continue; }
							if matches.positionals.contains_key(n) { continue; }
							self.required.insert(n);
						}
					}
				}
				break;
			}
		}

		if ! found {
			self.report_error(
				format!("Argument --{} isn't valid", arg),
				false, true);
		}
		None
	}

	fn parse_short_arg(&mut self, matches: &mut ArgMatches ,full_arg: &String) -> Option<&'static str> {
		let arg = full_arg.as_slice().trim_left_matches(|c| c == '-');
		if arg.len() > 1 { 
			// Multiple flags using short i.e. -bgHlS
			for c in arg.chars() {
				self.check_for_help_and_version(c);
				if ! self.parse_single_short_flag(matches, c) { 
					self.report_error(
						format!("Argument -{} isn't valid",c),
						false, true);
				}
			}
		} else {
			// Short flag or opt
			let arg_c = arg.char_at(0);
			self.check_for_help_and_version(arg_c);

			if ! self.parse_single_short_flag(matches, arg_c) { 
				for (k, v) in self.opts.iter() {
					if let Some(s) = v.short {
						if s == arg_c {
							return Some(k)
						}
					}
				} 

				self.report_error(
					format!("Argument -{} isn't valid",arg_c),
					false, true);
			}

		}
		None
	}

	fn parse_single_short_flag(&mut self, matches: &mut ArgMatches, arg: char) -> bool {
		for (k, v) in self.flags.iter() {
			if let Some(s) = v.short {
				if s != arg { continue; }

				if !matches.flags.contains_key(k) {
					if self.blacklist.contains(k) {
						self.report_error(format!("The argument -{} is mutually exclusive with one or more other arguments", arg),
							false, true);
					}
					matches.flags.insert(k, FlagArg{
					    name: v.name,
					    short: v.short,
					    long: v.long,
					    help: v.help,
					    multiple: v.multiple,
					    occurrences: v.occurrences,
					    blacklist: None, 
					    requires: None
					});
					if self.required.contains(k) {
						self.required.remove(k);
					}
					if let Some(ref reqs) = v.requires {
						if ! reqs.is_empty() {
							for n in reqs.iter() {
								if matches.opts.contains_key(n) { continue; }
								if matches.flags.contains_key(n) { continue; }
								if matches.positionals.contains_key(n) { continue; }
								self.required.insert(n);
							}
						}
					}
					if let Some(ref bl) = v.blacklist {
						if ! bl.is_empty() {
							for name in bl.iter() {
								self.blacklist.insert(name);
							}
						}
					}
				} else if matches.flags.get(k).unwrap().multiple { 
					matches.flags.get_mut(k).unwrap().occurrences += 1
				}

				return true;
			}
		}
		false
	}

	fn validate_blacklist(&self, matches: &ArgMatches) {
		if ! self.blacklist.is_empty() {
			for name in self.blacklist.iter() {
				for (k, v) in matches.flags.iter() {
					if k == name {
						self.report_error(format!("The argument {} is mutually exclusive with one or more other arguments",
							if let Some(s) = v.short {
								format!("-{}", s)
							} else if let Some(l) = v.long {
								format!("--{}", l)
							} else {
								format!("\"{}\"", v.name)
							}),
							false, true);
					}
				}
				for (k, v) in matches.opts.iter() {
					if k == name {
						self.report_error(format!("The argument {} is mutually exclusive with one or more other arguments",
							if let Some(s) = v.short {
								format!("-{}", s)
							} else if let Some(l) = v.long {
								format!("--{}", l)
							} else {
								format!("\"{}\"", v.name)
							}),
							false, true);
					}
				}
				for (k, v) in matches.positionals.iter() {
					if k == name {
						self.report_error(format!("The argument \"{}\" is mutually exclusive with one or more other arguments",v.name),
							false, true);
					}
				}
			}
		}
	}

	fn create_help_and_version(&mut self) {
		if self.needs_long_help {
			self.flags.insert("clap_help", FlagArg{
				name: "clap_help",
				short: if self.needs_short_help { Some('h') } else { None },
				long: Some("help"),
				help: Some("Prints this message"),
				blacklist: None,
				multiple: false,
				requires: None,
				occurrences: 1
			});
		}
		if self.needs_long_version {
			self.flags.insert("clap_version", FlagArg{
				name: "clap_version",
				short: if self.needs_short_help { Some('v') } else { None },
				long: Some("version"),
				help: Some("Prints version information"),
				blacklist: None,
				multiple: false,
				requires: None,
				occurrences: 1
			});
		}
		if self.needs_subcmd_help {
			self.subcommands.insert("help", Box::new(App::new("help").about("Prints this message")));
		}
	}

	fn get_matches_from(&mut self, matches: &mut ArgMatches, it: &mut IntoIter<String>) {
		self.create_help_and_version();

		// let mut needs_val = false;
		let mut subcmd_name: Option<&'static str> = None;
		let mut needs_val_of: Option<&'static str> = None; 
		let mut pos_counter = 1;
		while let Some(arg) = it.next() {
			let arg_slice = arg.as_slice();
			let mut skip = false;
			if let Some(nvo) = needs_val_of {
				if let Some(ref opt) = self.opts.get(nvo) {
					if self.blacklist.contains(opt.name) {
						self.report_error(
							format!("The argument {} is mutually exclusive with one or more other arguments", 
							if let Some(long) = opt.long {
								format!("--{}",long)
							}else{
								format!("-{}",opt.short.unwrap())
							}),false, true);
					}
					matches.opts.insert(nvo, OptArg{
						name: opt.name,
					    short: opt.short,
					    long: opt.long, 
					    help: opt.help,
					    requires: None,
					    blacklist: None,
					    required: opt.required,
					    value: Some(arg.clone()) 
					});
					if let Some(ref bl) = opt.blacklist {
						if ! bl.is_empty() {
							for name in bl.iter() {
								self.blacklist.insert(name);
							}
						}
					}
					if self.required.contains(opt.name) {
						self.required.remove(opt.name);
					}
					if let Some(ref reqs) = opt.requires {
						if ! reqs.is_empty() {
							for n in reqs.iter() {
								if matches.opts.contains_key(n) { continue; }
								if matches.flags.contains_key(n) { continue; }
								if matches.positionals.contains_key(n) { continue; }
								self.required.insert(n);
							}
						}
					}
					skip = true;
				}
			}
			if skip {
				needs_val_of = None;
				continue;
			}
			if arg_slice.starts_with("--") {
				// Single flag, or option long version
				needs_val_of = self.parse_long_arg(matches, &arg);

			} else if arg_slice.starts_with("-") {
				needs_val_of = self.parse_short_arg(matches, &arg);
			} else {
				// Positional or Subcommand
				if let Some(sca) = self.subcommands.get(arg_slice) {
					if sca.name == "help" {
						self.print_help();
					}
					subcmd_name = Some(sca.name);
					break;
				}

				if self.positionals_idx.is_empty() { // || self.positionals_name.is_empty() {
					self.report_error(
						format!("Found positional argument {}, but {} doesn't accept any", arg, self.name),
						false, true);
				}
				if let Some(ref p) = self.positionals_idx.get(&pos_counter) {
					if self.blacklist.contains(p.name) {
						self.report_error(format!("The argument \"{}\" is mutually exclusive with one or more other arguments", arg),
							false, true);
					}
					matches.positionals.insert(p.name, PosArg{
						name: p.name,
						help: p.help,
						required: p.required,
						blacklist: None,
						requires: None,
						value: Some(arg.clone()),
						index: pos_counter
					});
					if let Some(ref bl) = p.blacklist {
						if ! bl.is_empty() {
							for name in bl.iter() {
								self.blacklist.insert(name);
							}
						}
					}
					if self.required.contains(p.name) {
						self.required.remove(p.name);
					}
					if let Some(ref reqs) = p.requires {
						if ! reqs.is_empty() {
							for n in reqs.iter() {
								if matches.opts.contains_key(n) { continue; }
								if matches.flags.contains_key(n) { continue; }
								if matches.positionals.contains_key(n) { continue; }
								self.required.insert(n);
							}
						}
					}
					pos_counter += 1;
				} else {
					self.report_error(format!("Positional argument \"{}\" was found, but {} wasn't expecting any", arg, self.name), false, true);
				}
			}
		}

		match needs_val_of {
			Some(ref a) => {
				self.report_error(
					format!("Argument \"{}\" requires a value but none was supplied", a),
					false, true);
			}
			_ => {}
		}
		if ! self.required.is_empty() {
			self.report_error("One or more required arguments were not supplied".to_string(),
					false, true);
		}

		self.validate_blacklist(&matches);

		if let Some(sc_name) = subcmd_name {
			if let Some(ref mut sc) = self.subcommands.get_mut(sc_name) {
				let mut new_matches = ArgMatches::new(sc_name);
				sc.get_matches_from(&mut new_matches, it);
				matches.subcommand = Some((sc_name, Box::new(SubCommand{
					name: sc_name,
					matches: new_matches})));
			}
		}	
	}

	pub fn get_matches(mut self) -> ArgMatches {
		let mut matches = ArgMatches::new(self.name);

		let args = env::args().collect::<Vec<_>>();	

		self.get_matches_from(&mut matches, &mut args.into_iter());

		matches
	}
}