Get arguments

Returned as a list, index 0 being the path + filename of the python file that has been run

    import sys
    command_line_arguments = sys.argv
    #print("This is the name of the program:", sys.argv[0])
    #print(command_line_arguments)            #E.g. ['C:\\MyFolder\\my_app.py', 'abc', 'xyz]
Get any arguments excluding the default index0 path + filename of the python file
    #Get any arguments
    command_line_arguments = parse_arguments(sys.argv[1:])

Get argument and its value

This example forces to lower case, remove .lower() if you don’t want that

    found_it = False
    for argument in command_line_arguments:
        if (argument.lower().find("my_argument_text=") == 0):        #0=does string start with, -1=not found
                found_it = True
                argument_value = argument[argument.lower().index("my_argument_text=")+17:]
                break

Handling key pair arguments

E.g. arg1=”True” arg2=1234

def parse_arguments(args):
    parsed_args = {}
    for arg in args:
        if '=' in arg:
            key, value = arg.split('=', 1)
            value = value.strip('"')        #Remove any quote marks around a string
            parsed_args[key] = value
        else:
            parsed_args[arg] = ''
    return parsed_args

def main():
    #Get any arguments
    command_line_arguments = parse_arguments(sys.argv[1:])

    if 'my_argument_name' in command_line_arguments:
        my_argument_name_= int(command_line_arguments['some_argument_name'])

    if command_line_arguments.get('arg1', '').lower() == 'true':
        #Do something...

Testing arguments when developing

DEBUG_RUN_ARGUMENTS = ['', 'my_argument_a', 'my_argument_b=abc', ]
#DEBUG_RUN_ARGUMENTS = []

    if (len(DEBUG_RUN_ARGUMENTS) > 0):          #Overide with debug arguments
        command_line_arguments = parse_arguments(DEBUG_RUN_ARGUMENTS[1:])