str is already the default type of any variable parsed with argparse. Also the default metavar is just the name itself.
The cool thing about argparse is that it allows you to use custom variable parsing functions as type. So you'llyou could put the file type (or actually only file ending) check there:
import os
def csv_file(filepath):
if not filepath.endswith('.csv'):
raise ValueError("Input file must be a .csv file.")
if not os.path.isfile(filepath):
raise FileNotFoundError("Could not find file {}".Formatformat(filepath)
return filepath
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("filepath", type=csv_file, help="Filepath to the input csv file.")
return vars(parser.parse_args())
If an exception is raised in the type function, argparse will catch it, display it and exit the program.