Last active
August 29, 2015 14:00
-
-
Save ytoshima/11203790 to your computer and use it in GitHub Desktop.
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/env python | |
# | |
# windows batch file example | |
# c:\Python27\python %UserProfile%\local\bin\mypg.py %* | |
# | |
import sys | |
import os.path | |
DEFAULT_CHAR = ':' | |
def printHeader(fn, tchar): | |
fnlen = len(fn) | |
border = tchar * fnlen | |
print "%s\n%s\n%s" % (border, fn, border) | |
def printContent(fn): | |
try: | |
f = file(fn) | |
for l in f: | |
print l, | |
f.close() | |
except IOError as e: | |
print type(e) | |
print e.args | |
print e | |
def printFile(fn, tchar): | |
if os.path.isfile(fn): | |
printHeader(fn, tchar) | |
printContent(fn) | |
def printFiles(files, tchar): | |
for f in files: | |
printFile(f, tchar) | |
def usage(): | |
m = """usage: mypg [-c <c>] [-v] <files...> | |
mypg <-h | --help>: shows this message | |
""" | |
print m | |
if __name__ == '__main__' : | |
import getopt | |
char = DEFAULT_CHAR | |
opts, args = getopt.getopt(sys.argv[1:], 'hvc:', ['help', 'verbose', 'char=']) | |
# need to expand file names on windows... | |
import glob | |
import itertools | |
files = list(itertools.chain(*map(glob.glob, args))) | |
verbose = False | |
for opt, arg in opts: | |
if opt in ('-h', '--help'): | |
usage() | |
sys.exit(2) | |
elif opt in ('-v', '--verbose'): | |
verbose = True | |
elif opt in ('-c', '--char'): | |
char = arg | |
if verbose: | |
print "c=%c" % (char) | |
print "args: %s" % (", ".join(args)) | |
print "files: %s" % (", ".join(files)) | |
if len(files) > 0: | |
printFiles(files, char) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment