Skip to content

Commit 8e57ff7

Browse files
sirhceleldruin
authored andcommitted
Allow specifying two common options via env
Configuration files an symlink root are used in many subcommands and are expected to be fixed for a certain environment. Allow to conveniently pass them via environment variables. The symlink root has a somewhat special role as it might as well be specified via configuration files and we can't have a default value from the CLI in this case.
1 parent ed00508 commit 8e57ff7

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Rust applications or any other applications.
1717
"""
1818

1919
[dependencies]
20-
clap = { version = "4.5.48", features = ["derive"] }
20+
clap = { version = "4.5.48", features = ["derive", "env"] }
2121
sysfs_gpio = "0.6.2"
2222
toml = { version = "<=0.9.6", default-features = false, features = ["parse", "serde"] }
2323
glob = "0.3.3"

src/main.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ use gpio_utils::config::{self, GpioConfig};
1212
use gpio_utils::options::*;
1313
use std::process;
1414

15+
pub const CONFIG_ENV_VAR: &str = "GPIO_UTILS_CONFIG";
16+
pub const SYMLINK_ROOT_ENV_VAR: &str = "GPIO_UTILS_SYMLINK_ROOT";
17+
1518
#[derive(Debug, Parser)]
1619
#[command(
1720
name = "GPIO Utils",
1821
version,
1922
about = "Read, Write, and Configure GPIOs"
2023
)]
2124
struct Cli {
22-
/// additional configuration to use
23-
#[arg(short, long = "config", value_name = "FILE")]
25+
/// additional configuration to use (separator ':')
26+
#[arg(short, long = "config", value_name = "FILE", num_args = 0.., env = CONFIG_ENV_VAR, value_delimiter = ':')]
2427
configs: Vec<String>,
2528
#[command(subcommand)]
2629
command: Commands,
@@ -56,27 +59,27 @@ enum Commands {
5659
/// The pin name (or number)
5760
pin: String,
5861
/// root directory for export symlinks
59-
#[arg(short = 'r', long)]
62+
#[arg(short = 'r', long, env = SYMLINK_ROOT_ENV_VAR)]
6063
symlink_root: Option<String>,
6164
},
6265
/// Export all configured GPIOs
6366
ExportAll {
6467
/// Export all configured GPIOs
65-
#[arg(short = 'r', long)]
68+
#[arg(short = 'r', long, env = SYMLINK_ROOT_ENV_VAR)]
6669
symlink_root: Option<String>,
6770
},
6871
/// Export all configured GPIOs
6972
Unexport {
7073
/// The pin name (or number)
7174
pin: String,
7275
/// root directory for export symlinks
73-
#[arg(short = 'r', long)]
76+
#[arg(short = 'r', long, env = SYMLINK_ROOT_ENV_VAR)]
7477
symlink_root: Option<String>,
7578
},
7679
/// Unexport all configured, exported GPIOs
7780
UnexportAll {
7881
/// root directory for export symlinks
79-
#[arg(short = 'r', long)]
82+
#[arg(short = 'r', long, env = SYMLINK_ROOT_ENV_VAR)]
8083
symlink_root: Option<String>,
8184
},
8285
/// Output status of a GPIO or all GPIOs if no pin is specified

0 commit comments

Comments
 (0)