Python Click包子命令中获取token参数问题求助
Hey there! Let's work through this Click CLI token issue together. Since I can't view your attached image, I'll cover the most common scenarios for passing a token to subcommands—this should align with what you're trying to build.
1. Global Token Option (For All Subcommands)
If every subcommand needs the token, define it as a global option on your root group and store it in the Click context so subcommands can access it:
import click @click.group() @click.option('--token', required=True, help='Authentication token for API access') @click.pass_context def cli(ctx, token): # Save the token to the context object ctx.obj = {'auth_token': token} @cli.command() @click.pass_context def fetch_user(ctx): # Pull the token from the context token = ctx.obj['auth_token'] click.echo(f"Fetching user data with token: {token[:5]}...") @cli.command() @click.pass_context def update_settings(ctx): token = ctx.obj['auth_token'] click.echo(f"Updating settings using token: {token[:5]}...") if __name__ == '__main__': cli()
Usage:
python your_script.py --token "your-secret-token" fetch-user
2. Token Option for Specific Subcommands
If only certain subcommands need the token, define the option directly on those subcommands:
import click @click.group() def cli(): pass @cli.command() @click.option('--token', required=True, help='Token required for data export') def export_data(token): click.echo(f"Exporting data with token: {token[:5]}...") @cli.command() def list_resources(): click.echo("Listing public resources (no token needed)") if __name__ == '__main__': cli()
Usage:
python your_script.py export-data --token "your-token"
3. Token from Environment Variable (More Secure)
To avoid passing the token as plaintext in commands, let users set it via an environment variable (with an optional --token override):
import click import os @click.group() @click.option('--token', help='Authentication token (or set via MY_APP_TOKEN env var)', default=lambda: os.environ.get('MY_APP_TOKEN', None)) @click.pass_context def cli(ctx, token): if not token: raise click.ClickException("Token required! Use --token or set MY_APP_TOKEN environment variable.") ctx.obj = {'token': token} @cli.command() @click.pass_context def sync(ctx): token = ctx.obj['token'] click.echo(f"Syncing data with token: {token[:5]}...") if __name__ == '__main__': cli()
Usage:
# Set the env var first export MY_APP_TOKEN="your-secret-token" # Then run the command without passing --token python your_script.py sync
Quick Troubleshooting Checks
- Make sure you're using
@click.pass_contexton both the root group and any subcommands that need access to the context-stored token. - Double-check that option names match (e.g., don't define
--auth-tokenin the group but try to accesstokenin the subcommand). - If your subcommand isn't receiving the token, print the context object (
click.echo(ctx.obj)) to verify the token is being stored correctly.
If none of these match your intended functionality, share a snippet of your current code or more details about how you want the token to work, and I can adjust the solution!
内容的提问来源于stack exchange,提问作者saivasanth528

