<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def key(username, key, all):
 """Create an admin API key."""

    if username and username not in current_app.config['ADMIN_USERS']:
        raise click.UsageError('User {} not an admin'.format(username))

    def create_key(admin, key):
        key = ApiKey(
            user=admin,
            key=key,
            scopes=[Scope.admin, Scope.write, Scope.read],
            text='Admin key created by alertad script',
            expire_time=None
        )
        try:
            db.get_db()  # init db on global app context
            key = key.create()
        except Exception as e:
            click.echo('ERROR: {}'.format(e))
        else:
            click.echo('{} {}'.format(key.key, key.user))

    if all:
        for admin in current_app.config['ADMIN_USERS']:
            create_key(admin, key)
    elif username:
        create_key(username, key)
    else:
        raise click.UsageError("Must set '--username' or use '--all'")