mcandre edited this page Aug 3, 2011 · 4 revisions
Clone this wiki locally

It is convenient to bundle a library of functions with a command line interface. For example, a secure hash function library that can be either imported (import hashfunctions) or run over files (./hashfunctions.py file1.txt file2.txt...). The main function of the script should be called when the script is run by itself, but not when the script is imported by another script. I.e., the second script's main function should supersede the first script's main.

Perhaps the most famous example is Python's if __name__=="__main__" syntax:

#!/usr/bin/env python

def meaning_of_life():
	return 42

def main():
	print "Main: The meaning of life is %s" % meaning_of_life()

if __name__=="__main__":

For lack of a better term, this behavior can be called "scriptedmain". This repository contains a collection of scriptedmain programs in a variety of programming languages.

Many of these scripts are mirrored at Rosetta Code.