cli: Share a common write_cmd() implementation