toList method

List<String> toList({
  1. bool runInShell = false,
  2. int skipLines = 0,
  3. bool nothrow = false,
  4. String? workingDirectory,
  5. bool extensionSearch = true,
})

toList runs the contents of this String as a cli command and returns any output written to stdout and stderr as a List<String>.

DCli performs Glob expansion on command arguments. See run for details.

The skipLines argument tells toList to not return the first skipLines lines. This is useful if a command outputs a heading and you want to skip over the heading.

If runInShell is true (defaults to false) then the command will be run in a shell. This may be required if you are trying to run a command that is builtin to the shell.

If the command completes with a non-zero exit code then a RunException is thrown. The RunException includes the exit code and the cause contains all of the output the command wrote to stdout and stderr before it exited.

Any environment variables you set via env'xxx' will be passed to the new process.

EXPERIMENTAL argument. If nothrow is set to true then an exception will not be thrown on a non-zero exit code. Many applications output to stdout/stderr to display an error message when a non-zero exit code is returned. If you need to process these error messages then pass nothrow:true. The default for nothrow is false - i.e. we throw an exception on a non-zero exitCode.

List<String> logLines = 'tail -n 10 /var/log/syslog'.toList();

See forEach to capture output to stdout and stderr interactively toParagraph to concatenating the return lines into a single string. run to run the application without capturing its output start - to run the process fully detached. firstLine - returns just the first line written to stdout or stderr. lastLine - returns just the last line written to stdout or stderr. toParagraph to concatenating the return lines into a single string. parser - returns a parser with the captured output ready to be interpreted as one of several file types.

Implementation

List<String> toList({
  bool runInShell = false,
  int skipLines = 0,
  bool nothrow = false,
  String? workingDirectory,
  bool extensionSearch = true,
}) {
  final progress = cmd.start(
    this,
    runInShell: runInShell,
    progress: Progress.capture(),
    nothrow: nothrow,
    workingDirectory: workingDirectory,
    extensionSearch: extensionSearch,
  );

  return progress.lines.sublist(skipLines);
}