aboutsummaryrefslogtreecommitdiff
path: root/examples/doc_comments.rs
blob: 3d22152911daa1cf43fe9a627c1cc9cb601a150d (plain)
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
//! How to use doc comments in place of `help/long_help`.
//!
//! Running this example with --help prints this message:
//! -----------------------------------------------------
//! basic 0.3.25
//! A basic example for the usage of doc comments as replacement of the arguments `help`, `long_help`, `about` and
//! `long_about`
//!
//! USAGE:
//!     doc_comments [FLAGS] <SUBCOMMAND>
//!
//! FLAGS:
//!     -f, --first-flag
//!             Just use doc comments to replace `help`, `long_help`, `about` or `long_about` input
//!
//!     -h, --help
//!             Prints help information
//!
//!     -s, --second-flag
//!             Split between `help` and `long_help`.
//!
//!             In the previous case structopt is going to present the whole comment both as text for the `help` and the
//!             `long_help` argument.
//!
//!             But if the doc comment is formatted like this example -- with an empty second line splitting the heading and
//!             the rest of the comment -- only the first line is used as `help` argument. The `long_help` argument will
//!             still contain the whole comment.
//!
//!             ## Attention
//!
//!             Any formatting next to empty lines that could be used inside a doc comment is currently not preserved. If
//!             lists or other well formatted content is required it is necessary to use the related structopt argument with
//!             a raw string as shown on the `third_flag` description.
//!     -t, --third-flag
//!             This is a raw string.
//!
//!             It can be used to pass well formatted content (e.g. lists or source
//!             code) in the description:
//!
//!              - first example list entry
//!              - second example list entry
//!
//!     -V, --version
//!             Prints version information
//!
//!
//! SUBCOMMANDS:
//!     first     The same rules described previously for flags. Are also true for in regards of sub-commands
//!     help      Prints this message or the help of the given subcommand(s)
//!     second    Applicable for both `about` an `help`
//! -----------------------------------------------------

use structopt::StructOpt;

/// A basic example for the usage of doc comments as replacement
/// of the arguments `help`, `long_help`, `about` and `long_about`.
#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
    /// Just use doc comments to replace `help`, `long_help`,
    /// `about` or `long_about` input.
    #[structopt(short, long)]
    first_flag: bool,

    /// Split between `help` and `long_help`.
    ///
    /// In the previous case structopt is going to present
    /// the whole comment both as text for the `help` and the
    /// `long_help` argument.
    ///
    /// But if the doc comment is formatted like this example
    /// -- with an empty second line splitting the heading and
    /// the rest of the comment -- only the first line is used
    /// as `help` argument. The `long_help` argument will still
    /// contain the whole comment.
    ///
    /// ## Attention
    ///
    /// Any formatting next to empty lines that could be used
    /// inside a doc comment is currently not preserved. If
    /// lists or other well formatted content is required it is
    /// necessary to use the related structopt argument with a
    /// raw string as shown on the `third_flag` description.
    #[structopt(short, long)]
    second_flag: bool,

    #[structopt(
        short,
        long,
        long_help = r"This is a raw string.

It can be used to pass well formatted content (e.g. lists or source
code) in the description:

 - first example list entry
 - second example list entry
 "
    )]
    third_flag: bool,

    #[structopt(subcommand)]
    sub_command: SubCommand,
}

#[derive(StructOpt, Debug)]
#[structopt()]
enum SubCommand {
    /// The same rules described previously for flags. Are
    /// also true for in regards of sub-commands.
    First,

    /// Applicable for both `about` an `help`.
    ///
    /// The formatting rules described in the comment of the
    /// `second_flag` also apply to the description of
    /// sub-commands which is normally given through the `about`
    /// and `long_about` arguments.
    Second,
}

fn main() {
    let opt = Opt::from_args();
    println!("{:?}", opt);
}