Skip to content

Instantly share code, notes, and snippets.

@obriencj obriencj/cli_stub.py

Created Dec 31, 2013
Embed
What would you like to do?
Stub that I find useful for writing CLI utilities
#!/usr/bin/env python2
"""
Given two tags, do absolutely nothing.
This script is just a stub illustrating the bits I re-use for every
CLI utility I write.
You should always post what you want it
to do up here, so you can remind yourself later what it was you were
working on.
"""
import sys
from optparse import OptionParser
from socket import gaierror
from xmlrpclib import Fault, ServerProxy
KOJI_SERVICE="http://redacted/"
def _debug_on(msg):
print >> sys.stderr, msg
def _debug_off(msg):
pass
def _verbose_on(msg):
print msg
def _verbose_off(msg):
pass
class ForgotMyWork(Exception):
pass
def do_work():
raise ForgotMyWork("I forgot to make this script do anything")
# Do the important stuff here.
def cli(options, release_tag, candidate_tag):
kojihub = ServerProxy(options.koji)
debug = _debug_on if options.debug else _debug_off
verbose = _verbose_on if options.verbose else _verbose_off
debug("starting to work on secret stuff")
verbose("Did Zero Work!")
do_work()
def create_optparser():
parse = OptionParser("%prog [OPTIONS] TAG_A TAG_B",
description="Do something with two tags")
parse.add_option("--reverse", action="store_true", default=False,
help="reverse behavior")
parse.add_option("--inherit", action="store_true", default=False,
help="look through the entire inheritance")
parse.add_option("--koji", action="store", default=KOJI_SERVICE,
help="URI for the koji XMLRPC service")
parse.add_option("--debug", action="store_true",
help="print debugging output")
parse.add_option("--verbose", action="store_true",
help="print verbose messages")
return parse
def main(args):
parser = create_optparser()
options, args = parser.parse_args(args)
if len(args) > 3:
parser.error("Too many arguments!")
elif len(args) < 3:
parser.error("Not enough arguments!")
# this is a good place to check any argument and options for
# sanity, so you can barf out the parser help message.
# unpack the real arguments that you want here
name, release, candidate = args
try:
# if there are "expected" exceptions, such as NoSuchFile,
# NoSuchTag, make up your own exception classes and raise them
# from the cli function, and catch them here, then return the
# appropriate code if any.
cli(options, release, candidate)
except KeyboardInterrupt, ki:
return 130
except Fault, xmlf:
print >> sys.stderr, xmlf.faultString
return -1
except gaierror, dns:
print >> sys.stderr, dns.message
print >> sys.stderr, "Try using --koji with an IP address"
return -2
except ForgotMyWork, fml:
print >> sys.stderr, fml
else:
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))
#
# The end.
@obriencj

This comment has been minimized.

Copy link
Owner Author

obriencj commented Dec 31, 2013

Some things worth considering; if there is a lot of output, it could be worth setting the debug and verbose functions to be at the top level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.