Browse Source

Added an example code

tags/v1.0.0
Cédric Belin 1 week ago
parent
commit
f11576c7d8
5 changed files with 53 additions and 6 deletions
  1. +5
    -5
      doc/usage/api.md
  2. +14
    -0
      example/main.hx
  3. +17
    -0
      example/main.js
  4. +16
    -0
      example/main.php
  5. +1
    -1
      lib/php/which.php

+ 5
- 5
doc/usage/api.md View File

@@ -21,8 +21,8 @@ This package provides a single function, `which()`, allowing to locate a command
class Main {
static function main() {
FinderTools.which("foobar").then(
(path: String) -> Sys.println('The command "foobar" is located at: $path'),
(e: FinderException) -> Sys.println('The command "${e.command}" was not found.')
path -> Sys.println('The command "foobar" is located at: $path'),
e -> Sys.println('The command "${(e: FinderException).command}" was not found.')
);
}
}
@@ -65,7 +65,7 @@ This package provides a single function, `which()`, allowing to locate a command
}

catch (FinderException $e) {
print "The command '{$e->getCommand()}' was not found.";
print "The command '{$e->command}' was not found.";
}
}

@@ -92,7 +92,7 @@ If you pass `true` as option value, the function will return an array of strings

=== "JavaScript"
:::js
import {which} from "@cedx/which";
import {which} from "@cedx/which.hx";

async function main() {
const paths = await which("foobar", {all: true});
@@ -149,7 +149,7 @@ By default, when the specified command cannot be located, a `FinderException` is

=== "JavaScript"
:::js
import {which} from "@cedx/which";
import {which} from "@cedx/which.hx";

async function main() {
const path = await which("foobar", {onError: command => ""});


+ 14
- 0
example/main.hx View File

@@ -0,0 +1,14 @@
import which.FinderException;
using which.FinderTools;

/** A sample class. **/
class Example {

/** Finds the first instance of an executable. */
static function main() {
"foobar".which().then(
path -> Sys.println('The command "foobar" is located at: $path'),
e -> Sys.println('The command "${(e: FinderException).command}" was not found.')
);
}
}

+ 17
- 0
example/main.js View File

@@ -0,0 +1,17 @@
import {which} from "@cedx/which.hx";

/**
* Finds the first instance of an executable.
* @return {Promise<void>} Completes when the program is terminated.
*/
async function main() {
try {
const path = await which("foobar");
console.log(`The command 'foobar' is located at: ${path}`);
}

catch (error) {
// `error` is an instance of `FinderException`.
console.log(`The command '${error.command}' was not found`);
}
}

+ 16
- 0
example/main.php View File

@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

use function which\which;
use which\FinderException;

/** Finds the first instance of an executable. */
function main(): void {
try {
$path = which("foobar");
print "The command 'foobar' is located at: $path";
}

catch (FinderException $e) {
print "The command '{$e->command}' was not found.";
}
}

+ 1
- 1
lib/php/which.php View File

@@ -8,7 +8,7 @@ namespace which;
* @return string|string[] A string, or an array of strings, specifying the path(s) of the found executable(s).
* @throws FinderException The specified command was not found.
*/
function which(string $command, array $options = null) {
function which(string $command, array $options = []) {
$executable = null;
$exception = null;



Loading…
Cancel
Save