python optional arguments argparse code example

Example 1: python optional arguments

def myfunc(a,b, *args, **kwargs):
      c = kwargs.get('c', None)
      d = kwargs.get('d', None)
      #etc
myfunc(a,b, c='nick', d='dog', ...)

Example 2: python argparse file argument

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()

print(args.file.readlines())

Example 3: python argument parser default value

parser.add_argument("-v", "--verbose", action="store_true",
                    default="your default value", help="verbose output")

Example 4: optional arguments python

def some_function (self, a, b, c, d = None, e = None, f = None, g = None, h = None):
    #code

Tags:

Misc Example