| Crates.io | zcfg_flag_parser |
| lib.rs | zcfg_flag_parser |
| version | 0.2.0 |
| created_at | 2017-07-15 03:56:11.525518+00 |
| updated_at | 2017-12-01 02:16:22.480312+00 |
| description | Populate all zcfg configured libraries in your binary using command line flags. |
| homepage | |
| repository | https://github.com/acmcarther/zcfg |
| max_upload_size | |
| id | 23427 |
| size | 6,064 |
zcfg is a simple configuration library. It lets you define configurable values for your Rust library, without coupling you to a particular parsing format, or requiring plumbing in the binary.
Unlike other common configuration or flag libraries, you do not need to propagate internal details of your application into main, and you do not need to force users to configure internal details of your library if they choose not to.
It is inspired by gflags and similar global configuration systems.
#![feature(used)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate zcfg;
define_pub_cfg!(net_protocol_timeout_ms, u32, 20000,
"How long the server or client should wait before considering \
this connection timed out.")
struct NetProtocolClient {
timeout_ms: u32,
}
impl Default for NetProtocolClient {
fn default() -> NetProtocolClient {
NetProtocolClient {
timeout_ms: net_protocol_timeout_ms::CONFIG.get_value(),
}
}
}
extern crate zcfg;
extern crate zcfg_flag_parser;
use zcfg_flag_parser::FlagParser;
fn main() {
// Parse flags for all linked crates via command line
FlagParser::new().parse_from_args(env::args().skip(1)).unwrap();
}
configurableuse zcfg::ConfigParsable;
enum BuildStrategy {
Local,
Remote {
addr: String,
}
}
impl ConfigParsable for BuildStrategy { ... }
define_cfg!(use_build_strategy, BuildStrategy, BuildStrategy::Local,
"Defines how the build planner performs compilation. Options are \
[Local] or [Remote(\"some_address\")].")
Limit access of configs to object initialization under default confitions to preserve testability
Consider define_pub_cfg if you'd like other modules to use the config.
Ensure that default values are useful -- users may not perform config population at all.
zcfg currently depends on the experimental used feature to prevent config initializers from being dropped by the compiler.Really dark stuff. zcfg uses a combination of linker flags and the used experimental feature to enqueue all flags defined in linked libraries in a global list, which main can populate without knowing about.