Skip to content

Instantly share code, notes, and snippets.

@brantfaircloth
Created December 7, 2011 16:47
Show Gist options
  • Select an option

  • Save brantfaircloth/1443543 to your computer and use it in GitHub Desktop.

Select an option

Save brantfaircloth/1443543 to your computer and use it in GitHub Desktop.

Revisions

  1. Brant Faircloth created this gist Dec 7, 2011.
    19 changes: 19 additions & 0 deletions cool_argparse_stuff.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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)