[Python] Python putting multiple arguments inside a mutually exclusive group

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    I want to achieve something like this,

    usage: main.py [-h] [--video_link video | --channel_link channel --num_vids num ]


    The option --num_vids is only needed if I'm using --channel_link option.

    I could only come with this,

    parser = argparse.ArgumentParser(
    description='A Python script to download Youtube videos')
    group_ = parser.add_mutually_exclusive_group()
    group_.add_argument('--video_link',
    metavar='video',
    type=str,
    help='Enter the video url')
    group_.add_argument('--channel_link',
    metavar='channel_link',
    type=str,
    help='Enter the name of the channel')
    args = parser.parse_args()
    video_link = args.video_link
    channel_link = args.channel_link
    print(video_link, channel_link)


    How do I implement the desired functionality?

    Continue reading...

Compartilhe esta Página