Search code examples
pythonkeyword-argument

How do I use kwargs as flags without needing =True?


I'm working on a python module that fetches, downloads/updates and then automatically installes other modules. How do I use **kwargs (or an alternative) without needing =True on the end for a "flags" system?

I'm not very experienced in general so I'm not sure what do do here

Currently, for the silent flag to work, It has to look like this:

pyup.imp(["psutil","shutil","os","sys","pyinstaller"],silent=True)

but I want the call function to look like this:

pyup.imp(["psutil","shutil","os","sys","pyinstaller"],silent)

The function itself looks like this (in pyup.py):

def imp(libs = [], *args, **kwargs):

where libs[] is the array containing the wanted libraries.

How do I make the function call not require "=True"?


Solution

  • If you want to pass a series of string flags to your function (as if it were a terminal application), you coud pass them as unnamed positional arguments via *args.

    def imp(libs=[], *args):
        silent = '--silent' in args
    
    # somewhere else
    imp([...], '--silent')