Python Language Parsing Command Line arguments Hello world in argparse

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


The following program says hello to the user. It takes one positional argument, the name of the user, and can also be told the greeting.

import argparse

parser = argparse.ArgumentParser()

    help='name of user'

parser.add_argument('-g', '--greeting', 
    help='optional alternate greeting'

args = parser.parse_args()

print("{greeting}, {name}!".format(
$ python --help
usage: [-h] [-g GREETING] name

positional arguments:
  name                  name of user

optional arguments:
  -h, --help            show this help message and exit
  -g GREETING, --greeting GREETING
                        optional alternate greeting
$ python world
Hello, world!
$ python John -g Howdy
Howdy, John!

For more details please read the argparse documentation.

Got any Python Language Question?