stole cmdmain from camlistore.org/pkg/cmdmain