|
60 | 60 | //! |
61 | 61 | //! First, let's look at the example: |
62 | 62 | //! |
63 | | -//! ```should_panic |
| 63 | +//! ``` |
64 | 64 | //! use std::path::PathBuf; |
65 | 65 | //! use structopt::StructOpt; |
66 | 66 | //! |
|
95 | 95 | //! } |
96 | 96 | //! |
97 | 97 | //! fn main() { |
| 98 | +//! # /* |
98 | 99 | //! let opt = Opt::from_args(); |
| 100 | +//! # */ |
| 101 | +//! # let opt = Opt::from_iter(&["binary", "-o", "stdout", "input"]); |
99 | 102 | //! println!("{:?}", opt); |
100 | 103 | //! } |
101 | 104 | //! ``` |
|
134 | 137 | //! They are what used to be explicit `#[structopt(raw(...))]` attrs in pre-0.3 `structopt` |
135 | 138 | //! |
136 | 139 | //! Every `structopt attribute` looks like comma-separated sequence of methods: |
137 | | -//! ```rust,ignore |
| 140 | +//! ``` |
| 141 | +//! # #[derive(structopt::StructOpt)] struct S { |
| 142 | +//! # |
138 | 143 | //! #[structopt( |
139 | 144 | //! short, // method with no arguments - always magical |
140 | 145 | //! long = "--long-option", // method with one argument |
141 | 146 | //! required_if("out", "file"), // method with one and more args |
142 | 147 | //! parse(from_os_str = path::to::parser) // some magical methods have their own syntax |
143 | 148 | //! )] |
| 149 | +//! # |
| 150 | +//! # s: () } mod path { pub(crate) mod to { pub(crate) fn parser(_: &std::ffi::OsStr) {} }} |
144 | 151 | //! ``` |
145 | 152 | //! |
146 | 153 | //! `#[structopt(...)]` attributes can be placed on top of `struct`, `enum`, |
|
174 | 181 | //! They are the reason why `structopt` is so flexible. **Every and each method from |
175 | 182 | //! `clap::App/Arg` can be used this way!** |
176 | 183 | //! |
177 | | -//! ```ignore |
| 184 | +//! ``` |
| 185 | +//! # #[derive(structopt::StructOpt)] struct S { |
| 186 | +//! # |
178 | 187 | //! #[structopt( |
179 | 188 | //! global = true, // name = arg form, neat for one-arg methods |
180 | 189 | //! required_if("out", "file") // name(arg1, arg2, ...) form. |
181 | 190 | //! )] |
| 191 | +//! # |
| 192 | +//! # s: String } |
182 | 193 | //! ``` |
183 | 194 | //! |
184 | 195 | //! The first form can only be used for methods which take only one argument. |
|
435 | 446 | //! /// for its type (in this case 0). |
436 | 447 | //! #[structopt(skip)] |
437 | 448 | //! skipped: u32, |
438 | | -//! |
439 | 449 | //! } |
440 | 450 | //! |
441 | 451 | //! # Opt::from_iter( |
|
452 | 462 | //! #[derive(StructOpt)] |
453 | 463 | //! struct Opt { |
454 | 464 | //! #[structopt(default_value = "", long)] |
455 | | -//! prefix: String |
| 465 | +//! prefix: String, |
456 | 466 | //! } |
457 | 467 | //! ``` |
458 | 468 | //! |
|
474 | 484 | //! struct Opt { |
475 | 485 | //! // just leave the `= "..."` part and structopt will figure it for you |
476 | 486 | //! #[structopt(default_value, long)] |
477 | | -//! prefix: String // `String` implements both `Default` and `ToString` |
| 487 | +//! prefix: String, // `String` implements both `Default` and `ToString` |
478 | 488 | //! } |
479 | 489 | //! ``` |
480 | 490 | //! |
|
499 | 509 | //! #[derive(StructOpt)] |
500 | 510 | //! #[structopt(about = "I am a program and I work, just pass `-h`")] |
501 | 511 | //! struct Foo { |
502 | | -//! #[structopt(short, help = "Pass `-h` and you'll see me!")] |
503 | | -//! bar: String |
| 512 | +//! #[structopt(short, help = "Pass `-h` and you'll see me!")] |
| 513 | +//! bar: String, |
504 | 514 | //! } |
505 | 515 | //! ``` |
506 | 516 | //! |
|
513 | 523 | //! #[derive(StructOpt)] |
514 | 524 | //! /// I am a program and I work, just pass `-h` |
515 | 525 | //! struct Foo { |
516 | | -//! /// Pass `-h` and you'll see me! |
517 | | -//! bar: String |
| 526 | +//! /// Pass `-h` and you'll see me! |
| 527 | +//! bar: String, |
518 | 528 | //! } |
519 | 529 | //! ``` |
520 | 530 | //! |
|
555 | 565 | //! /// until I'll have destroyed humanity. Enjoy your |
556 | 566 | //! /// pathetic existence, you mere mortals. |
557 | 567 | //! #[structopt(long)] |
558 | | -//! kill_all_humans: bool |
| 568 | +//! kill_all_humans: bool, |
559 | 569 | //! } |
560 | 570 | //! ``` |
561 | 571 | //! |
|
632 | 642 | //! Also, `structopt` will *still* remove leading and trailing blank lines so |
633 | 643 | //! these formats are equivalent: |
634 | 644 | //! |
635 | | -//! ```ignore |
| 645 | +//! ``` |
636 | 646 | //! /** This is a doc comment |
637 | 647 | //! |
638 | 648 | //! Hello! */ |
|
646 | 656 | //! /// This is a doc comment |
647 | 657 | //! /// |
648 | 658 | //! /// Hello! |
| 659 | +//! # |
| 660 | +//! # mod m {} |
649 | 661 | //! ``` |
650 | 662 | //! ______________ |
651 | 663 | //! |
|
665 | 677 | //! |
666 | 678 | //! #[derive(StructOpt)] |
667 | 679 | //! struct Foo { |
668 | | -//! #[structopt(short, long, env = "PARAMETER_VALUE")] |
669 | | -//! parameter_value: String |
| 680 | +//! #[structopt(short, long, env = "PARAMETER_VALUE")] |
| 681 | +//! parameter_value: String, |
670 | 682 | //! } |
671 | 683 | //! ``` |
672 | 684 | //! |
|
688 | 700 | //! |
689 | 701 | //! #[derive(StructOpt)] |
690 | 702 | //! struct Foo { |
691 | | -//! #[structopt(long = "secret", env = "SECRET_VALUE", hide_env_values = true)] |
692 | | -//! secret_value: String |
| 703 | +//! #[structopt(long = "secret", env = "SECRET_VALUE", hide_env_values = true)] |
| 704 | +//! secret_value: String, |
693 | 705 | //! } |
694 | 706 | //! ``` |
695 | 707 | //! |
|
707 | 719 | //! |
708 | 720 | //! #[derive(StructOpt)] |
709 | 721 | //! struct Foo { |
710 | | -//! #[structopt(long = "secret", env)] |
711 | | -//! secret_value: String |
| 722 | +//! #[structopt(long = "secret", env)] |
| 723 | +//! secret_value: String, |
712 | 724 | //! } |
713 | 725 | //! ``` |
714 | 726 | //! |
|
774 | 786 | //! #[structopt(short)] |
775 | 787 | //! patch: bool, |
776 | 788 | //! #[structopt(parse(from_os_str))] |
777 | | -//! files: Vec<PathBuf> |
| 789 | +//! files: Vec<PathBuf>, |
778 | 790 | //! }, |
779 | 791 | //! Fetch { |
780 | 792 | //! #[structopt(long)] |
781 | 793 | //! dry_run: bool, |
782 | 794 | //! #[structopt(long)] |
783 | 795 | //! all: bool, |
784 | | -//! repository: Option<String> |
| 796 | +//! repository: Option<String>, |
785 | 797 | //! }, |
786 | 798 | //! Commit { |
787 | 799 | //! #[structopt(short)] |
788 | 800 | //! message: Option<String>, |
789 | 801 | //! #[structopt(short)] |
790 | | -//! all: bool |
791 | | -//! } |
| 802 | +//! all: bool, |
| 803 | +//! }, |
792 | 804 | //! } |
793 | 805 | //! ``` |
794 | 806 | //! |
|
807 | 819 | //! supervising_faerie: String, |
808 | 820 | //! /// The faerie tree this cookie is being made in. |
809 | 821 | //! tree: Option<String>, |
810 | | -//! #[structopt(subcommand)] // Note that we mark a field as a subcommand |
811 | | -//! cmd: Command |
| 822 | +//! #[structopt(subcommand)] // Note that we mark a field as a subcommand |
| 823 | +//! cmd: Command, |
812 | 824 | //! } |
813 | 825 | //! |
814 | 826 | //! #[derive(StructOpt)] |
815 | 827 | //! enum Command { |
816 | 828 | //! /// Pound acorns into flour for cookie dough. |
817 | 829 | //! Pound { |
818 | | -//! acorns: u32 |
| 830 | +//! acorns: u32, |
819 | 831 | //! }, |
820 | 832 | //! /// Add magical sparkles -- the secret ingredient! |
821 | 833 | //! Sparkle { |
822 | 834 | //! #[structopt(short, parse(from_occurrences))] |
823 | 835 | //! magicality: u64, |
824 | 836 | //! #[structopt(short)] |
825 | | -//! color: String |
| 837 | +//! color: String, |
826 | 838 | //! }, |
827 | 839 | //! Finish(Finish), |
828 | 840 | //! } |
|
832 | 844 | //! struct Finish { |
833 | 845 | //! #[structopt(short)] |
834 | 846 | //! time: u32, |
835 | | -//! #[structopt(subcommand)] // Note that we mark a field as a subcommand |
836 | | -//! finish_type: FinishType |
| 847 | +//! #[structopt(subcommand)] // Note that we mark a field as a subcommand |
| 848 | +//! finish_type: FinishType, |
837 | 849 | //! } |
838 | 850 | //! |
839 | 851 | //! // subsubcommand! |
840 | 852 | //! #[derive(StructOpt)] |
841 | 853 | //! enum FinishType { |
842 | 854 | //! Glaze { |
843 | | -//! applications: u32 |
| 855 | +//! applications: u32, |
844 | 856 | //! }, |
845 | 857 | //! Powder { |
846 | 858 | //! flavor: String, |
847 | | -//! dips: u32 |
| 859 | +//! dips: u32, |
848 | 860 | //! } |
849 | 861 | //! } |
850 | 862 | //! ``` |
|
868 | 880 | //! struct Foo { |
869 | 881 | //! file: String, |
870 | 882 | //! #[structopt(subcommand)] |
871 | | -//! cmd: Option<Command> |
| 883 | +//! cmd: Option<Command>, |
872 | 884 | //! } |
873 | 885 | //! |
874 | 886 | //! #[derive(StructOpt)] |
875 | 887 | //! enum Command { |
876 | 888 | //! Bar, |
877 | 889 | //! Baz, |
878 | | -//! Quux |
| 890 | +//! Quux, |
879 | 891 | //! } |
880 | 892 | //! ``` |
881 | 893 | //! |
|
953 | 965 | //! BaseCli(BaseCli), |
954 | 966 | //! Dex { |
955 | 967 | //! arg2: i32, |
956 | | -//! } |
| 968 | +//! }, |
957 | 969 | //! } |
958 | 970 | //! ``` |
959 | 971 | //! |
|
1068 | 1080 | //! |
1069 | 1081 | //! // a struct with single custom argument |
1070 | 1082 | //! #[derive(StructOpt)] |
1071 | | -//! struct GenericArgs<T:FromStr> where <T as FromStr>::Err: fmt::Display + fmt::Debug { |
| 1083 | +//! struct GenericArgs<T: FromStr> where <T as FromStr>::Err: fmt::Display + fmt::Debug { |
1072 | 1084 | //! generic_arg_1: String, |
1073 | 1085 | //! generic_arg_2: String, |
1074 | | -//! custom_arg_1: T |
| 1086 | +//! custom_arg_1: T, |
1075 | 1087 | //! } |
1076 | 1088 | //! ``` |
1077 | 1089 | //! |
|
1081 | 1093 | //! # use structopt::StructOpt; |
1082 | 1094 | //! // a struct with multiple custom arguments in a substructure |
1083 | 1095 | //! #[derive(StructOpt)] |
1084 | | -//! struct GenericArgs<T:StructOpt> { |
| 1096 | +//! struct GenericArgs<T: StructOpt> { |
1085 | 1097 | //! generic_arg_1: String, |
1086 | 1098 | //! generic_arg_2: String, |
1087 | 1099 | //! #[structopt(flatten)] |
1088 | | -//! custom_args: T |
| 1100 | +//! custom_args: T, |
1089 | 1101 | //! } |
1090 | 1102 | //! ``` |
1091 | 1103 |
|
|
0 commit comments