Skip to content

Instantly share code, notes, and snippets.

@gregnewman
Created May 7, 2009 11:31
Show Gist options
  • Save gregnewman/108058 to your computer and use it in GitHub Desktop.
Save gregnewman/108058 to your computer and use it in GitHub Desktop.
# Zed Shaw
# I've written an argument parsing lib for Python as part of my Lamson project.
# It's main advantage is that you don't define the options to commands in
# a giant nest of nasty functions. Instead, it just parses the command line
# using standard grammar/syntax for command line options, and then creates
# a command/options tuple for you to work with.
#
# Then it has various functions for running commands out of a module
# you specify, printing help, etc.
#
# How you run the argument parsing lib in Lamson:
from lamson import args, commands
import sys
# Notice you don't specify any option formats.
args.parse_and_run_command(sys.argv[1:], commands, default_command="help")
# How you write a command (the __doc__ becomes the help printed to the user).
def log_command(options):
"""
Runs a logging only server on the given hosts and port:
lamson log -port 8825 -host 127.0.0.1 -debug 1
"""
# This is how you setup defaults, which updates 'options' with
# anything that's not set. Set an option to None and it is required.
args.defaults(options, port=8825, host='127.0.0.1', debug=1)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
settings = utils.FakeSettings()
# configure up basic settings for logging
settings.handlers = [ (".*", handlers.LogHandler()) ]
settings.receiver = {"host" : options['host'], "port" : options['port'], "debug": options['debug']}
settings.relay = settings.receiver
settings.database = None
settings.templates = {"directories": "/tmp", "module_directory": "/tmp"}
logging.info("Logging mode enabled, will not send email to anyone, just log.")
utils.start_server(settings)
# Sample help output:
Lamson help:
help:
Prints out help for the commands.
lamson help
You can get help for one command with:
lamson help -for STR
create:
Creates the database and other stuff for Lamson.
lamson create
run:
Runs a lamson server out of the current directory:
lamson run
log:
Runs a logging only server on the given hosts and port:
lamson log -port 8825 -host 127.0.0.1 -debug 1
send:
Sends an email to someone as a test message:
lamson send -port 8825 -host 127.0.0.1 -debug 1 -sender EMAIL -to EMAIL -subject STR -body 'Test message.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment