Python Language Parsing Command Line arguments Hello world in argparse


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.