move CLI code into lib