Created
June 5, 2016 20:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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