Key-Value CLI Arguments in Python
If you have a CommandLine application (CLI) written in Python, you have a number of ways that you can take arguments from the user.
You could take the order from the user and assign those to variables:
This will give you a list of all the space separated values.
So if your app is called like:
As you can see, sys.argv
is a list and the arguments start from the second index location.
How can we use Key-Value Arguments instead?
What if our app’s user decides to swap the order of var1 and var2? Or if they miss something out?
How about we give them a key as well?
|
|
That seems nicer, but how do we implement this in code?
We now have a dictionary that contains each of the key-value pairs.