[Python] How to activate only options when we use click.group()

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm currently working to create Command line arguments with click. I almost done the research, and everything is working fine. The issue is I want to use the only option while working with the click.group() other than sub commands.

    Lets say myCommand --version this should print my application's version but it's raising error saying Error: Missing command.

    My code is:

    import sys
    import os as _os
    import click
    import logging

    from myApp import __version__

    @click.group()
    @click.option('--version', is_flag=True, help="Displays project version")
    @click.pass_context
    def cli(context, version: bool):
    if version:
    print(__version__)

    @cli.command()
    @click.pass_context
    def init(context):
    click.echo(message="Starting initilization for the project" + str(context.obj))

    @cli.command()
    @click.pass_context
    def install(context):
    click.echo(message="Starting installing from the saved data")


    Here --version is only working when I call the command with option like cli --version init, But I want this to be cli --version to print the version.

    Can anyone help me with this?

    Continue reading...

Compartilhe esta Página