#[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 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