rather than doing all this why not use argparse module from python. If the argument is not provided it will automatically print the usage statement as below
import argparse parser = argparse.ArgumentParser() parser.add_argument('file', type=argparse.FileType('r')) args = parser.parse_args() # returns data from the options specified (echo) print(args.file) type=argparse.FileType('r') argument is not necessary but it is better to use.
'r' represents and checks whether the file is readable or exists.
Similarly you can use 'w' to check if file that you are passing is writable.
Output:
$> python progargs.py
usage: progargs.py [-h] file
progargs.py: error: too few arguments
$> python progargs.py testanotherprog.py <open file
'testanotherprog.py', mode 'r' at 0x7fe8ee422270>