which function

Which which(
  1. String appname, {
  2. bool first = true,
  3. bool verbose = false,
  4. bool extensionSearch = true,
  5. void progress(
    1. WhichSearch
    )?,
})

Searches the PATH for the location of the application give by appname.

The search is conducted by searching each of the paths in the environment variable 'PATH' from left to right (start to end) as this is the same order the OS searches the path.

If the verbose flag is true then a line is output to the progress for each path searched.

It is possible that more than one copy of the appliation is found.

which returns a list of paths that contain appname in the order they were found.

The first path in the list is the one the OS will be using.

if the first flag is true then which will stop searching as soon as it finds a match. first is true by default.

which('ls', first: false, verbose: true);

To print the path to the command:

print(which('ls').path);

To check if an app is on the path use:

if (which('apt').found)
{
  print('found apt');
}

if extensionSearch is true and the passed appname doesn't have a file extension then when running on Windows the which command will search for appname plus appname with each of the extensions listed in the Windows environment variable PATHEX. This feature is intended to make it easier to implement cross platform command search. For example the dart will be 'dart' on Linux and 'dart.bat' on Windows. Using which('dart') will find dart on linux and dart.bat on Windows.

Implementation

Which which(
  String appname, {
  bool first = true,
  bool verbose = false,
  bool extensionSearch = true,
  void Function(WhichSearch)? progress,
}) =>
    _Which().which(
      appname,
      first: first,
      verbose: verbose,
      extensionSearch: extensionSearch,
      progress: progress,
    );