
# The long options are the long-form name of the same variables if you want to use those instead, -foo and -bar # The short options are the single letter options for your command line application, in this case -f and -b # The argv variable is passed to getopt() as the first parameter so that the options/arguments can be read from it, followed by the short options and the long options # Retrieve the options and arguments from the argv variable using getopt.getopt() # So, we get all arguments from the first onwards (1:) # The first will always be the name of the script # The position of the arguments starts counting at 0 # Get all of the arguments passed to the script # Import required libraries - these should be available in a default Python 3 installation I’ll explain how it works in the comments: # This script is written for Python 3 Here’s an example Python 3 script which accepts 2 parameters.

#EXAMPLES OF COMMAND LINE ARGUMENTS FULL#
I’ve previously covered building a full command-line application in Python, which accepts command-line options and bundles the application into a distributable package with no external dependencies – why not give it a look when you’re done here? What are Command Line Arguments?Ĭommand-line arguments are information passed to an application or script via the command line – it looks something like this in the terminal: python myscript.py arg1 arg2
#EXAMPLES OF COMMAND LINE ARGUMENTS CODE#
This is much easier than re-editing your code every time you want to change the inputs it will be working on. Passing arguments to your script makes it easy to build multi-use scripts that can work on different inputs. This article will show you simple methods to pass command line arguments to your Python scripts, with some examples.
