Created
August 12, 2013 11:20
-
-
Save jdu/6210030 to your computer and use it in GitHub Desktop.
A python command-line tool template
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
#!/usr/bin/python | |
import getopt, sys, os, shutil | |
import subprocess, codecs | |
def main(): | |
METHOD = None | |
args = [] | |
kwargs = {} | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "hc", [ | |
"help", | |
"copy", | |
"src=", | |
"dest="]) | |
except getopt.GetOptError as err: | |
print str(err) | |
usage() | |
sys.exit(2) | |
for o, a in opts: | |
if o in ("--src"): | |
kwargs['src'] = a | |
if o in ("--dest"): | |
kwargs['dest'] = a | |
if o in ["-h", "--help"]: | |
METHOD = "usage" | |
elif o in ['-c', '--copy']: | |
METHOD = "copier" | |
if METHOD is None: | |
usage() | |
assert False, "Unknown Command." | |
sys.exit(2) | |
globals()[METHOD](*args, **kwargs) | |
sys.exit(0) | |
def usage(*args, **kwargs): | |
print "-----------------------------------------------------" | |
print " My Custom Tool " | |
print "-----------------------------------------------------" | |
print " " | |
print " --help Display this help" | |
print " --copy Copy files" | |
print " --src Source of files" | |
print " --dest Destination for files" | |
print " " | |
print "-----------------------------------------------------" | |
def copier(*args, **kwargs): | |
if 'src' not in kwargs: | |
assert False, "--src parameter missing" | |
sys.exit(1) | |
if 'dest' not in kwargs: | |
assert False, "--dest parameter missing" | |
sys.exit(1) | |
shutil.copytree(kwargs['src'], kwargs['dest']) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment