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
//! Command line flag library in the style of [gflags (formerly Google
//! Commandline Flags)][gflags].
//!
//! [gflags]: https://gflags.github.io/gflags/
//!
//! Quoting directly from the C++ gflags documentation, because the concept is
//! the same here:
//!
//! <br>
//!
//! > ---
//! >
//! > **Commandline flags** are flags that users specify on the command line
//! > when they run an executable. In the command
//! >
//! > ```text
//! > fgrep -l -f /var/tmp/foo johannes brahms
//! > ```
//! >
//! > `-l` and `-f /var/tmp/foo` are the two commandline flags. (`johannes` and
//! > `brahms`, which don't start with a dash, are **commandline arguments**.)
//! >
//! > Typically, an application lists what flags the user is allowed to pass in,
//! > and what arguments they take -- in this example, `-l` takes no argument,
//! > and `-f` takes a string (in particular, a filename) as an argument. Users
//! > can use a library to help parse the commandline and store the flags in
//! > some data structure.
//! >
//! > Gflags, the commandline flags library used within Google, differs from
//! > other libraries, such as `getopt()`, in that flag definitions can be
//! > scattered around the source code, and not just listed in one place such as
//! > `main()`. In practice, this means that a single source-code file will
//! > define and use flags that are meaningful to that file. Any application
//! > that links in that file will get the flags, and the gflags library will
//! > automatically handle that flag appropriately.
//! >
//! > There's significant gain in flexibility, and ease of code reuse, due to
//! > this technique.
//! >
//! > ---
//!
//! <br>
//!
//! This style of flag registration is better suited for large scale development
//! than maintaining a single central list of flags, as the central list would
//! become an endless source of merge conflicts in an application developed
//! simultaneously by hundreds of developers.
//!
//! # Defining flags
//!
//! Flags may be defined from any source file through the [`gflags::define!`]
//! macro. There is no central list of all the flags of the application. (That's
//! the point and advantage of gflags for large-scale development compared to
//! other flags libraries.)
//!
//! [`gflags::define!`]: macro.define.html
//!
//! ```
//! gflags::define! {
//!     /// Include 'advanced' options in the menu listing.
//!     --big_menu = true
//! }
//!
//! gflags::define! {
//!     /// Comma-separated list of languages to offer in the 'lang' menu.
//!     -l, --language <LANG> = "english,french,german"
//! }
//! #
//! # fn main() {}
//! ```
//!
//! Flags are required to have a long name (like `--verbose`) and may optionally
//! have a short name (like `-v`). Flags must have exactly one long name and at
//! most one short name; multiple different aliases for the same flag is not
//! supported.
//!
//! Flags of a type other than bool may have an optional value-placeholder like
//! `<LANG>`. This is optional and purely cosmetic. It appears in help text.
//!
//! # Accessing flags
//!
//! Somewhere early in your application, call [`gflags::parse()`] to parse the
//! command line. This call returns a `Vec<&str>` containing everything on the
//! command line which is not a flag (these are sometimes known as positional
//! arguments) in a vector.
//!
//! [`gflags::parse()`]: fn.parse.html
//!
//! After `gflags::parse()` has been called, the value of each flag is available
//! in the `.flag` field of the flag's long name.
//!
//! ```
//! gflags::define! {
//!     --print-args = false
//! }
//!
//! fn main() {
//!     let args = gflags::parse();
//!
//!     if PRINT_ARGS.flag {
//!         println!("args = {:?}", args);
//!     }
//! }
//! ```
//!
//! As shown in this snippet, flag names may contain hyphens, in which case the
//! variable through which the flag's value can be accessed has underscores in
//! place of the hyphens.
//!
//! Additionally every flag provides a method `.is_present()` to query whether
//! that flag was provided on the command line. When using flags for which a
//! default value is not provided, be sure to check `.is_present()` because
//! accessing `.flag` when not present will cause a panic. Note also that flags
//! without a default value must specify their data type, as below.
//!
//! ```
//! use std::path::Path;
//!
//! gflags::define! {
//!     /// Search for patterns from the given file, with one pattern per line.
//!     -f, --file: &Path
//! }
//!
//! fn main() {
//!     let patterns = gflags::parse();
//!
//!     if FILE.is_present() {
//!         let path = FILE.flag;
//!         println!("searching for patterns from file: {}", path.display());
//!     } else {
//!         println!("searching for patterns given on command line: {:?}", patterns);
//!     }
//! }
//! ```
//!
//! # Printing help
//!
//! There is no built-in `-h` flag for help, but you can define your own and
//! call [`gflags::print_help_and_exit()`] to render the documentation of all
//! flags.
//!
//! [`gflags::print_help_and_exit()`]: fn.print_help_and_exit.html
//!
//! ```
//! gflags::define! {
//!     -h, --help = false
//! }
//!
//! fn main() {
//!     gflags::parse();
//!     if HELP.flag {
//!         gflags::print_help_and_exit(0);
//!     }
//!
//!     /* ... */
//! }
//! ```
//!
//! For some of the flag definitions shown in this documentation, the help text
//! would be rendered as follows.
//!
//! ```text
//!         --big_menu
//!             Include 'advanced' options in the menu listing.
//!
//!     -f, --file
//!             Search for patterns from the given file, with one pattern per line.
//!
//!     -l, --language <LANG>
//!             Comma-separated list of languages to offer in the 'lang' menu.
//! ```
//!
//! The flags are listed in alphabetical order by long name.
//!
//! You will likely want to print your own content above this including the
//! application name, version, author, introductory explanation, and usage
//! strings.
//!
//! # Custom data types
//!
//! The `gflags::define!` macro is extensible to custom data types by providing
//! an impl of [`gflags::custom::Value`] for your type.
//!
//! [`gflags::custom::Value`]: custom/trait.Value.html
//!
//! ```
//! use gflags::custom::{Arg, Error, Result, Value};
//!
//! gflags::define! {
//!     --color <WHEN>: Color = Color::Auto
//! }
//!
//! enum Color {
//!     Never,
//!     Always,
//!     Auto,
//! }
//!
//! impl Value for Color {
//!     fn parse(arg: Arg) -> Result<Self> {
//!         match arg.get_str() {
//!             "never" => Ok(Color::Never),
//!             "always" => Ok(Color::Always),
//!             "auto" => Ok(Color::Auto),
//!             _ => Err(Error::new("invalid color")),
//!         }
//!     }
//! }
//! #
//! # fn main() {}
//! ```

#![doc(html_root_url = "https://docs.rs/gflags/0.3.0")]

macro_rules! eprintln {
    ($($tt:tt)*) => {{
        use std::io::Write;
        let _ = std::writeln!(std::io::stderr(), $($tt)*);
    }};
}

mod arg;
mod atomic;
mod dispatch;
mod error;
mod help;
mod name;
mod parse;
mod state;
mod token;
mod value;

pub mod custom;

pub use crate::help::print_help_and_exit;
pub use crate::parse::{parse, parse_os};
pub use crate::state::Flag;

// Not public API.
#[doc(hidden)]
pub mod registry;

#[doc(hidden)]
pub use inventory;

#[doc(hidden)]
pub use gflags_impl as r#impl;

/// Entry point for registering a flag from any source file.
///
/// # Examples
///
/// Please refer to the [crate level documentation](index.html) for several
/// usage examples.
///
/// # Grammar
///
/// The complete input grammar is as follows.
///
/// - Zero or more doc comments: `/// ...`. These are rendered into the
///   generated help text.
///
/// - Optional visibility specifier like `pub` or `pub(crate)`. This controls
///   the scope of code that is allowed to see the value of this flag. By
///   default flags have private visibility, which is the default in Rust.
///
/// - Optional short name for the flag, like `-v`, followed by a comma.
///
/// - Long name for the flag, like `--verbose`. Long name is mandatory.
///
/// - Optional value-placeholder in angle brackets, like `<FILE>`. This is
///   cosmetic and appears in generated help text.
///
/// - Optional value type preceded by colon, like `: &str`. Type is required if
///   there is no default value or the default value is not a Rust string or
///   boolean or integer literal.
///
/// - Optional default value preceded by equal-sign: `= "default"`.
///
/// Invocation containing as few of the above as possible:
///
/// ```
/// gflags::define! {
///     --minimal1: bool
/// }
/// #
/// # fn main() {}
/// ```
///
/// Another way to show as few as possible. Either type or default value must be
/// specified.
///
/// ```
/// gflags::define! {
///     --minimal2 = "default value"
/// }
/// #
/// # fn main() {}
/// ```
///
/// Showing everything at once:
///
/// ```
/// # mod path {
/// #     pub mod to {
/// #         pub const DEFAULT: u32 = 0;
/// #     }
/// # }
/// #
/// gflags::define! {
///     /// Documentation!
///     pub -m, --maximal <VALUE>: u32 = path::to::DEFAULT
/// }
/// #
/// # fn main() {}
/// ```
#[macro_export]
macro_rules! define {
    ($($flag:tt)*) => {
        gflags::r#impl::define_impl! {
            $($flag)*
        }
    };
}