Last active
December 12, 2015 06:39
-
-
Save lambdamusic/4730628 to your computer and use it in GitHub Desktop.
Python: Shell Script With Options
This file contains 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
import sys | |
import time | |
import math | |
import optparse | |
__version__ = "0.1" | |
__copyright__ = "CopyRight (C) 2013 by Michele Pasin" | |
__license__ = "MIT" | |
__author__ = "Michele Pasin" | |
__author_email__ = "michele dot pasin at gmail dot com" | |
USAGE = "%prog [options] <arg>" | |
VERSION = "%prog v" + __version__ | |
AGENT = "%s/%s" % (__name__, __version__) | |
def parse_options(): | |
"""parse_options() -> opts, args | |
Parse any command-line options given returning both | |
the parsed options and arguments. | |
""" | |
parser = optparse.OptionParser(usage=USAGE, version=VERSION) | |
# JUST SOME EXAMPLES.... | |
parser.add_option("-d", "--depth", | |
action="store", type="int", default=1, dest="depth", | |
help="Maximum depth to traverse - by default is 1") | |
parser.add_option("-u", "--urlpattern", | |
action="store", type="string", default="", dest="urlpattern", | |
help="Extract links that match the url pattern provided only - by default no pattern is matched.") | |
parser.add_option("-q", "--quiet", | |
action="store_true", default=False, dest="quiet", | |
help="Enable quiet mode") | |
opts, args = parser.parse_args() | |
if len(args) < 1: | |
parser.print_help() | |
raise SystemExit, 1 | |
return opts, args | |
def main(): | |
# get parameters | |
opts, args = parse_options() | |
url = args[0] | |
depth = opts.depth | |
urlpattern = opts.urlpattern | |
quiet = opts.quiet | |
sTime = time.time() | |
print "Started ... %s (Max Depth: %d; Matching Pattern Url: \"%s\"; Quiet: \"%s\")" % (url, depth, urlpattern, quiet) | |
obj = SOMEOBJECT(url, depth, urlpattern, quiet) | |
obj.doSomething() | |
# print results.... | |
# print some stats.... | |
eTime = time.time() | |
tTime = eTime - sTime | |
print "Found: %d" % len(obj.allurls) | |
print "Followed: %d" % obj.followed | |
print "Stats: (%d/s after %0.2fs)" % ( | |
int(math.ceil(float(len(obj.allurls)) / tTime)), tTime) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment