std::env (v0.2.2)
Process arguments, environment variables, and common system paths.
Latest
Overview
std::env gives access to command-line arguments, environment variables, and platform directories such as the home and temporary directories.
hasArgument and argument read positional arguments; getVariable reads environment variables. Typed accessors such as argumentAsS32 and getVariableAsTString return ready-to-use views.
Source: thrustc/std/v0.2.2/env.thrust
Public Signatures
Exported declarations for this module snapshot.
fn hasArgument(argc: s32, index: s32) bool @public;
fn argument(argc: s32, argv: ptr[array[char]], index: s32) CString @public;
fn argumentAsTString(argc: s32, argv: ptr[array[char]], index: s32) tstring::TString @public;
fn argumentAsS32(argc: s32, argv: ptr[array[char]], index: s32) s32 @public;
fn argumentAsS64(argc: s32, argv: ptr[array[char]], index: s32) s64 @public;
fn argumentAsU32(argc: s32, argv: ptr[array[char]], index: s32) u32 @public;
fn argumentAsU64(argc: s32, argv: ptr[array[char]], index: s32) u64 @public;
fn argumentAsF64(argc: s32, argv: ptr[array[char]], index: s32) f64 @public;
fn arguments(argc: s32, argv: ptr[array[char]]) vec::Vector[tstring::TString] @public;
fn getVariable(name: CString) CString @public;
fn getVariableAsTString(name: CString) tstring::TString @public;
fn hasVariable(name: CString) bool @public;
fn setVariable(name: CString, value: CString, overwrite: bool) s32 @public;
fn unsetVariable(name: CString) s32 @public;
fn clearVariables() s32 @public;
fn homeDirectory() CString @public;
fn temporaryDirectory() CString @public;
fn userName() CString @public;
fn hostName() CString @public;
fn currentDirectory() CString @public;
Behavior and Use
Use hasArgument and argument to inspect the command line, and getVariable or getVariableAsTString to read process environment variables by name.
Typed accessors such as argumentAsS32 and getVariableAsTString return converted values, so callers avoid re-parsing raw strings.
Examples
import std::env;
import std::io;
// Check whether a required environment variable is set.
fn main() s32 @public {
if env::hasVariable("PATH") {
io::print("PATH is configured\n");
}
return 0;
}
import std::env;
import std::io;
// Store a session variable and confirm it is visible.
fn main() s32 @public {
env::setVariable("THRUST_DOC_TEST", "hello", true);
if env::hasVariable("THRUST_DOC_TEST") {
io::print("variable stored\n");
}
env::unsetVariable("THRUST_DOC_TEST");
return 0;
}
import std::env;
import std::io;
// Resolve the user's home directory.
fn main() s32 @public {
var home := env::homeDirectory();
if home != nullptr {
io::print("home is set\n");
}
return 0;
}
Notes
- Environment variable access can fail; check the result before using it.
- Argument accessors are indexed starting from zero.