"""
from argparse import ArgumentParser, ArgumentTypeError
-from os.path import isfile
+from os.path import isfile, isdir
class ArgParserWantsExit(Exception):
class NonExitingParser(ArgumentParser):
"""ArgumentParser that does not call sys.exit(2) on parse failure.
- Calling sys.exit also just raises a SystemExit exception. But that is not
+ Calling `sys.exit` also just raises a SystemExit exception. But that is not
a subclass of Exception and not as explicit and specific as this one.
Convenient e.g. for global try-except blocks e.g. in a daemon::
if not filename.strip():
return ''
return existing_file(filename)
+
+
+def existing_dir(path):
+ """
+ Function that raises ArgumentTypeError if argument is not an existing directory.
+
+ .. seealso:: :py:func:`existing_file`, :py:func:`existing_dir_or_empty`
+ """
+ if isdir(path):
+ return path
+ raise ArgumentTypeError('{} is not an existing directory'.format(path))
+
+
+def existing_dir_or_empty(path=''):
+ """
+ Like :py:func:`existing_dir` but accepts empty path (returns '' then).
+ """
+ if not path.strip():
+ return ''
+ return existing_dir(path)