import subprocess def exec_command(cmd, env=None, stdin=None): """Execute CLI command :param cmd: Program and arguments :type cmd: [str] :param env: Environment variables :type env: dict :param stdin: File to use for stdin :type stdin: file :returns: A tuple with the returncode, stdout and stderr :rtype: (int, bytes, bytes) """ process = subprocess.Popen( cmd, stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) # This is needed to get rid of '\r' from Windows's lines endings. stdout, stderr = [std_stream.replace(b'\r', b'') for std_stream in process.communicate()] return (process.returncode, stdout, stderr) def command_info(doc): """Get description from doc text :param doc: command help text :type doc: str :returns: one line description of command :rtype: str """ return doc.split('\n')[1].strip(".").lstrip()