[Python] Parsing a string using argparse

Discussão em 'Python' iniciado por Stack, Agosto 13, 2025.

  1. Stack

    Stack Membro Participativo

    So I want the input of argparse to be a string instead of the command line. for example:

    python3 some_script.py arg1 arg2 arg3


    I want to give the argparse the string "arg1 arg2 arg3"

    import argparse
    command = "arg1 arg2 arg3"
    parser = argparse.ArgumentParser()
    # add args here
    args = parser.parse_args()
    # process the command here and extract values

    Continue reading...

Compartilhe esta Página