Skip to content

Instantly share code, notes, and snippets.

@harrisont
Created June 5, 2016 20:36
Show Gist options
  • Save harrisont/ecb340616ab6f7cf11f99364fd57ef7e to your computer and use it in GitHub Desktop.
Save harrisont/ecb340616ab6f7cf11f99364fd57ef7e to your computer and use it in GitHub Desktop.
Parse a working directory from the arguments with argparse, validating that the directory exists
import argparse
import os
def directory(raw_path):
if not os.path.isdir(raw_path):
raise argparse.ArgumentTypeError('"{}" is not an existing directory'.format(raw_path))
return os.path.abspath(raw_path)
parser = argparse.ArgumentParser()
parser.add_argument('--working-dir', type=directory, default=os.path.curdir)
args = parser.parse_args()
print(args.working_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment