https://github.com/dalance/structopt-toml
An default value loader from TOML for structopt
https://github.com/dalance/structopt-toml
argument-parsing command-line-parser rust
Last synced: 3 months ago
JSON representation
An default value loader from TOML for structopt
- Host: GitHub
- URL: https://github.com/dalance/structopt-toml
- Owner: dalance
- License: apache-2.0
- Created: 2018-03-19T09:02:40.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-11-07T20:11:07.000Z (about 2 years ago)
- Last Synced: 2024-12-28T16:47:26.176Z (about 1 year ago)
- Topics: argument-parsing, command-line-parser, rust
- Language: Rust
- Homepage:
- Size: 54.7 KB
- Stars: 28
- Watchers: 3
- Forks: 7
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# structopt-toml
An default value loader from TOML for structopt.
It combinates with [structopt](https://github.com/TeXitoi/structopt).
[](https://github.com/dalance/structopt-toml/actions)
[](https://crates.io/crates/structopt-toml)
[](https://docs.rs/structopt-toml)
[](https://codecov.io/gh/dalance/structopt-toml)
## Usage
This crate must be used with `serde`, `serde_derive`, `structopt`, and `toml` explicitly.
```Cargo.toml
[dependencies]
serde = "1.0.104"
serde_derive = "1.0.104"
structopt = "0.3.11"
structopt-toml = "0.5.1"
toml = "0.5.6"
```
## Example
If `derive(Deserialize)`, `derive(StructOptToml)` and `serde(default)` are added to the struct with `derive(StructOpt)`, some functions like `from_args_with_toml` can be used.
```rust
use serde_derive::Deserialize;
use structopt::StructOpt;
use structopt_toml::StructOptToml;
#[derive(Debug, Deserialize, StructOpt, StructOptToml)]
#[serde(default)]
struct Opt {
#[structopt(default_value = "0", short = "a")] a: i32,
#[structopt(default_value = "0", short = "b")] b: i32,
}
fn main() {
let toml_str = r#"
a = 10
"#;
let opt = Opt::from_args_with_toml(toml_str).expect("toml parse failed");
println!("a:{}", opt.a);
println!("b:{}", opt.b);
}
```
The execution result is below.
```console
$ ./example
a:10 // value from TOML string
b:0 // value from default_value of structopt
$ ./example -a 20
a:20 // value from command line argument
b:0
```
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.