-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
157 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[example] | ||
param1 = "from config" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::path::Path; | ||
|
||
use clap::Parser; | ||
use config::{self, File}; | ||
use hyperparameter::*; | ||
|
||
/// Defines the command-line arguments for the application. | ||
#[derive(Parser)] | ||
#[command(after_long_help=generate_params_help())] | ||
struct CommandLineArgs { | ||
/// Specifies hyperparameters in the format `-D key=value` via the command line. | ||
#[arg(short = 'D', long)] | ||
define: Vec<String>, | ||
|
||
/// Specifies the configuration file path. | ||
#[arg(short = 'C', long, default_value = "examples/rust/cfg.toml")] | ||
config: String, | ||
} | ||
|
||
fn foo(desc: &str) { | ||
with_params! { | ||
get param1 = example.param1 or "default".to_string(); | ||
|
||
println!("param1={} // {}", param1, desc); | ||
} | ||
} | ||
|
||
fn main() { | ||
let args = CommandLineArgs::parse(); | ||
let config_path = Path::new(&args.config); | ||
let config = config::Config::builder() | ||
.add_source(File::from(config_path)) | ||
.build() | ||
.expect("Failed to load configuration file."); // Improved error handling | ||
|
||
foo("Outside any specific scope"); | ||
|
||
with_params! { // Scope with configuration file parameters | ||
params config.param_scope(); | ||
|
||
foo("Within configuration file scope"); | ||
with_params! { // Scope with command-line arguments | ||
params ParamScope::from(&args.define); | ||
|
||
foo("Within command-line arguments scope"); | ||
with_params! { // User-defined scope | ||
set example.param1= "scoped".to_string(); | ||
|
||
foo("Within user-defined scope"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::path::Path; | ||
|
||
use clap::Parser; | ||
use config::{self, File}; | ||
use hyperparameter::*; | ||
|
||
/// Defines the command-line arguments for the application. | ||
#[derive(Parser)] | ||
#[command(after_long_help=generate_params_help())] | ||
struct CommandLineArgs { | ||
/// Specifies hyperparameters in the format `-D key=value` via the command line. | ||
#[arg(short = 'D', long)] | ||
define: Vec<String>, | ||
|
||
/// Specifies the configuration file path. | ||
#[arg(short = 'C', long, default_value = "examples/rust/cfg.toml")] | ||
config: String, | ||
} | ||
|
||
fn main() { | ||
let args = CommandLineArgs::parse(); | ||
let config_path = Path::new(&args.config); | ||
let config = config::Config::builder() | ||
.add_source(File::from(config_path)) | ||
.build() | ||
.expect("Failed to load configuration file."); // Improved error handling | ||
|
||
// No scope | ||
println!("param1={} // Outside any specific scope", get_param!(example.param1, "default".to_string())); | ||
|
||
with_params! { // Scope with configuration file parameters | ||
params config.param_scope(); | ||
|
||
println!("param1={} // Within configuration file scope", get_param!(example.param1, "default".to_string())); | ||
with_params! { // Scope with command-line arguments | ||
params ParamScope::from(&args.define); | ||
|
||
println!("param1={} // Within command-line arguments scope", get_param!(example.param1, "default".to_string(), "Example param1")); | ||
with_params! { // User-defined scope | ||
set example.param1= "scoped".to_string(); | ||
|
||
println!("param1={} // Within user-defined scope", get_param!(example.param1, "default".to_string())); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use clap::Parser; | ||
use hyperparameter::*; | ||
|
||
/// Defines the command-line arguments for the application. | ||
#[derive(Parser)] | ||
#[command(after_long_help=generate_params_help())] | ||
struct CommandLineArgs { | ||
/// Specifies hyperparameters in the format `-D key=value` via the command line. | ||
#[arg(short = 'D', long)] | ||
define: Vec<String>, | ||
} | ||
|
||
fn main() { | ||
let args = CommandLineArgs::parse(); | ||
with_params! { | ||
params ParamScope::from(&args.define); | ||
// Retrieves `example.param1` with a default value of `1` if not specified. | ||
println!("param1={}", get_param!(example.param1, 1)); | ||
// Displays a help message when `<app> --help` is executed. | ||
println!("param2={}", get_param!(example.param2, false, "Example param2")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters