You may experience command line like bash (in UNIX) or powershell (in Windows). A command line to execute a given script with given parameters. We can also make Python to work like that, ya know?

There are several reasons we want to run a Python script with parameters added.

For instance, we already have a simple Python script and we want to connect it with another system to do some jobs and it requires some parameters in the streamline.

Or just another complex example. We want to dynamically calculate somethings with our existing Python files and the parameters must be varied based on environment variables, totally it should be run in CI/CD process. Like that.

Interesting? Let's see how can we do.


Old-school method

Because we run it like Bash, parameters will be a list of string there. We can retrieve them using sys.argv then manipulate each element in the program.

This is an example.

The program calculates sum of 2 input numbers. Let's try call this and add small numbers.

python3 src/argv/argv01_simple.py 1 2

It treats "1 2" as a list of string. All we need is to access each of them and cast to an integer then sum them up. You can see at the method get_params().

Output like this.

In case we want to get all parameters. Easy peasy. We make that input list and cast to integers then do sum them all. Like this.

Try call it and see.

But if we want to deal with "flags" or something more complex, for example.

python3 my-script.py 1 2 3 --flag1 ABC --flag2 XYZ

We may end up writing funtions to process input to sort out whether this value is an input or a flag.

Any ways simpler?


Good news. We are introduced to see a module to do this job.

argparse module

argparse is a module to help us dealing with inputs and flags. Besides, it helps handling tooltips and alerts for us. Link below is the official document of argparse.

argparse — Parser for command-line options, arguments and sub-commands
Source code: Lib/argparse.py Tutorial: This page contains the API reference information. For a more gentle introduction to Python command-line parsing, have a look at the argparse tutorial. The arg...

Let's see some examples.

create a simple adder

The main part are these lines.

parser = argparse.ArgumentParser()
parser.add_argument(...)
parsed_params = parser.parse_args(sys.argv[1:])

We parse the sys.argv[1:] as same as the old-school solution. Then create a parser and .add_argument() to manipulate the parameters.

There are 3 parts shown above:

  1. A string. This is a name of each parameter.
  2. type is the type of the parameter.
  3. help is a help message when user open help with -h flag. Example below.

Once argparse processed the parameters, it returns to an object and we can access each by using obj.name.

The program can be run like this.

create a sum of list

Look at line #9. there is nargs="+" which means the number of arguments can be any at least one. See the example run below.

create a sum of list with choices

We can define multiple flags to a single parameter at line 17-18, -o and --ops, which means this parameter can be assigned by any of them. However, we can access this parameter by the first flag name with double dashes, in this case it is ops.

choices requires a list of choices. And it will raise an error if the given value on run is not a member of the choices.

Try the choice add to compute a sum.

And try mult to compute a product.

create a web scraper with overwriting handler

The last example is a program to download a website into a file. If the file is already exist and not allow overwriting, it would return an error.

We define the flag --overwrite or -w as action="store_true". This mean the parameter overwrite will be true by adding this flag on given, and it's false by default.

Let's say we already have a file and we don't put -w on a run. It will be an error there.

And here we add -w then the file will be overwritten.


Here is my repo containing all scripts above with sample run commands.

GitHub - bluebirz/sample-argparse
Contribute to bluebirz/sample-argparse development by creating an account on GitHub.

There are plenty possibilities to design our program to deal with business problems and hope this be helpful for your effective Python to do your jobs.