refactor CLI code