Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save unpluggedcoder/7457fa23763a9a39a6fa90da683e20dc to your computer and use it in GitHub Desktop.
Save unpluggedcoder/7457fa23763a9a39a6fa90da683e20dc to your computer and use it in GitHub Desktop.
Some cool argparse stuff
class FullPaths(argparse.Action):
"""Expand user- and relative-paths"""
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, os.path.abspath(os.path.expanduser(values)))
def is_dir(dirname):
"""Checks if a path is an actual directory"""
if not os.path.isdir(dirname):
msg = "{0} is not a directory".format(dirname)
raise argparse.ArgumentTypeError(msg)
else:
return dirname
def get_args():
"""Get CLI arguments and options"""
parser = argparse.ArgumentParser(description="""do something""")
parser.add_argument('alignments', help="The folder of alignments",
action=FullPaths, type=is_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment