1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] argparse parse arbitary number groups of arguments

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024 às 04:12.

  1. Stack

    Stack Membro Participativo

    I have this snippet for illustrative purpose that doesn't work at all.

    #!/usr/bin/env python3

    import argparse

    parser = argparse.ArgumentParser()

    parser.add_argument("--action", nargs="+")
    parser.add_argument("--number", nargs="+", default=1)
    parser.add_argument("--some-parameter-to-action", nargs="+", required=True)

    args = parser.parse_args()


    I'm trying to parse an arbitrary number of action parameters, each action should have an associated number and some-parameter-to-action, potentially with a default value. Passing another action should create a new one and the following --number and --some-parameter-to-action flags should be associated with the action that preceeded it.

    Examples, I want to be able to pass all or a subset of these in a single invocation of the script:

    --action say -n 3 --some-parameter-to-action foo
    --action bla --some-parameter-to-action no
    --action nothing # error


    Is something like this possible in python?

    Continue reading...

Compartilhe esta Página