Skip to content

Instantly share code, notes, and snippets.

@bsutton
Last active January 25, 2020 06:00
Show Gist options
  • Select an option

  • Save bsutton/47d8cd50cc6945a40d92062041fb3d84 to your computer and use it in GitHub Desktop.

Select an option

Save bsutton/47d8cd50cc6945a40d92062041fb3d84 to your computer and use it in GitHub Desktop.
which.dart
#! /usr/bin/env dshell
import 'dart:io';
import 'package:dshell/dshell.dart';
/// dwhich appname - searches for 'appname' on the path
void main(List<String> args) {
var parser = ArgParser();
parser.addFlag('verbose', abbr: 'v', defaultsTo: false, negatable: false);
var results = parser.parse(args);
var verbose = results['verbose'] as bool;
if (results.rest.length != 1) {
print(red('You must pass the name of the executable to search for.'));
print(green('Usage:'));
print(green(' which ${parser.usage}<exe>'));
exit(1);
}
var command = results.rest[0];
for (var path in PATH) {
if (verbose) {
print('Searching: ${truepath(path)}');
}
if (!exists(path))
{
printerr(red('The path $path does not exist.'));
continue;
}
if (exists(join(path, command))) {
print(red('Found at: ${truepath(path, command)}'));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment