#[non_exhaustive]pub struct CommandContext {
pub credential: CredentialResolver,
pub args: ValueMap,
pub user_args: ValueMap,
pub command_path: String,
pub middleware: Middleware,
pub raw_matches: Arc<ArgMatches>,
}Expand description
Runtime context passed to advanced command handlers.
Most commands can use RuntimeCommandSpec::new and receive just the
credential and effective args. Use this context when a command needs the
colon path, user-supplied args, or a snapshot of middleware state.
This struct is constructed by the framework during command dispatch. Consumer code receives it in handler closures and should not construct it directly.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.credential: CredentialResolverLazy credential resolver.
args: ValueMapEffective arguments, including defaults and framework-injected values.
user_args: ValueMapArguments explicitly supplied by the user.
command_path: StringColon-separated command path such as project:list.
middleware: MiddlewareMiddleware snapshot for this invocation.
raw_matches: Arc<ArgMatches>Raw clap matches for typed argument deserialization via derive.
Implementations§
Source§impl CommandContext
impl CommandContext
Sourcepub fn config(&self) -> &ConfigFile
pub fn config(&self) -> &ConfigFile
Returns the per-application config file as loaded at startup.
Read a consumer-owned section with
ConfigFile::section, for example
ctx.config().section::<DeployConfig>("deploy")?. Engine-reserved
settings are available via
ConfigFile::engine.
Snapshot semantics: this is the config loaded once when
crate::cli::Cli::new was called. Changes made by config set during the same process
invocation (e.g. from a previous Cli::run) are not reflected here;
restart the CLI (a new Cli::new) to pick them up. For a one-shot CLI
process this is always the current on-disk state.
Sourcepub fn environment(&self) -> Result<Environment>
pub fn environment(&self) -> Result<Environment>
Resolves the active Environment for
this invocation.
The active environment name is self.middleware.env, seeded at startup
from the persisted active environment or configured default and
overridden per invocation by the global --env flag. Resolution merges
the compiled-in definition, the environments.toml file layer, and
<ENV>_* environment-variable overrides.
§Blocking
When the environments.toml file layer is enabled, this performs
synchronous filesystem I/O via
Environments::resolve.
Call it once per invocation and reuse the result rather than calling it
repeatedly inside an async handler on a latency-sensitive path.
§Errors
Returns an error if no environment system was registered via
CliConfig::with_environments or
if the active name does not resolve to a known environment.
Sourcepub fn typed_args<T: FromArgMatches>(&self) -> Result<T>
pub fn typed_args<T: FromArgMatches>(&self) -> Result<T>
Deserializes the raw argument matches into a typed args struct.
Use this with #[derive(clap::Args)] structs to get type-safe access
to command arguments instead of working with the ValueMap directly.
§Errors
Returns an error if the matches cannot be deserialized into T.
Sourcepub async fn credential(&self) -> Result<Credential>
pub async fn credential(&self) -> Result<Credential>
Resolves the credential for this command, triggering the auth flow on first use and memoizing the result.
Convenience wrapper over self.credential.resolve().
§Errors
Returns an error when the command is marked no_auth, or when the auth
provider fails to produce a credential.
Sourcepub async fn try_credential(&self) -> Result<Option<Credential>>
pub async fn try_credential(&self) -> Result<Option<Credential>>
Resolves the credential when one is available, returning Ok(None) for
no-auth commands.
Convenience wrapper over self.credential.try_resolve().
§Errors
Propagates the auth provider error when resolution is attempted and fails.
Sourcepub async fn credential_with_scopes(
&self,
extra: &[String],
) -> Result<Credential>
pub async fn credential_with_scopes( &self, extra: &[String], ) -> Result<Credential>
Resolves a credential that additionally covers extra scopes, on top of
the command’s declared scopes.
Use this when the required scopes are only known at runtime (for example a generic API caller that derives scopes from the target endpoint). A scope-aware auth provider re-authenticates when the cached token does not already cover the requested set.
Convenience wrapper over
self.credential.resolve_with_scopes().
If the handler also issues HTTP requests through the transport bearer
injector, call this before the first request: the injector resolves
and caches a scope-unaware token, so stepping up afterwards would not
affect requests it already authorized. See
CredentialResolver::resolve_with_scopes for the full ordering note.
§Errors
Returns an error when the command is marked no_auth, or when the auth
provider fails to produce a credential.
Trait Implementations§
Source§impl Clone for CommandContext
impl Clone for CommandContext
Source§fn clone(&self) -> CommandContext
fn clone(&self) -> CommandContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more