Browse Source

Tweaks

master
J. King 2 years ago
parent
commit
bb5eafc1ff
  1. 37
      lib/Docopt.php

37
lib/Docopt.php

@ -22,13 +22,15 @@ namespace MensBeam\Docopt;
class Docopt { class Docopt {
# __version__ = "0.7.2" # __version__ = "0.7.2"
public const VERSION = "0.7.2"; public const VERSION = "0.7.2";
}
class Util {
# def levenshtein_norm(source: str, target: str) -> float: # def levenshtein_norm(source: str, target: str) -> float:
# """Calculates the normalized Levenshtein distance between two string # """Calculates the normalized Levenshtein distance between two string
# arguments. The result will be a float in the range [0.0, 1.0], with 1.0 # arguments. The result will be a float in the range [0.0, 1.0], with 1.0
# signifying the biggest possible distance between strings with these lengths # signifying the biggest possible distance between strings with these lengths
# """ # """
protected static function levenshtein_norm(string $source, string $target): float { public static function levenshtein_norm(string $source, string $target): float {
// NOTE: We split the strings into arrays of UTF-8 characters to match Python's behaviour (for UTF-8 input) // NOTE: We split the strings into arrays of UTF-8 characters to match Python's behaviour (for UTF-8 input)
// TODO: How is argv encoded in Windows and macOS? Does the Windows Command Prompt differ from Powershell? // TODO: How is argv encoded in Windows and macOS? Does the Windows Command Prompt differ from Powershell?
$s = @preg_split('//Su', $source, -1, \PREG_SPLIT_NO_EMPTY); $s = @preg_split('//Su', $source, -1, \PREG_SPLIT_NO_EMPTY);
@ -63,7 +65,7 @@ class Docopt {
# redundant computations by storing the distances between various prefixes in # redundant computations by storing the distances between various prefixes in
# a matrix that is filled in iteratively. # a matrix that is filled in iteratively.
# """ # """
protected static function levenshtein(array $source, array $target): int { public static function levenshtein(array $source, array $target): int {
// NOTE: Our implementation operates on character arrays rather than strings // NOTE: Our implementation operates on character arrays rather than strings
# # Create matrix of correct size (this is s_len + 1 * t_len + 1 so that the # # Create matrix of correct size (this is s_len + 1 * t_len + 1 so that the
@ -126,7 +128,7 @@ class Docopt {
# Quirks: [-a] => (-a), (-a...) => (-a -a) # Quirks: [-a] => (-a), (-a...) => (-a -a)
# #
# """ # """
protected static function transform(BranchPattern $pattern): Either { public static function transform(BranchPattern $pattern): Either {
# result = [] # result = []
# groups = [[pattern]] # groups = [[pattern]]
$result = []; $result = [];
@ -277,7 +279,6 @@ class LeafPattern extends Pattern {
} }
} }
# def match(self, left: List["LeafPattern"], collected: List["Pattern"] = None) -> Tuple[bool, List["LeafPattern"], List["Pattern"]]: # def match(self, left: List["LeafPattern"], collected: List["Pattern"] = None) -> Tuple[bool, List["LeafPattern"], List["Pattern"]]:
public function match(array $left, ?array $collected = null): array { public function match(array $left, ?array $collected = null): array {
# collected = [] if collected is None else collected # collected = [] if collected is None else collected
@ -413,17 +414,23 @@ class BranchPattern extends Pattern {
# def fix_repeating_arguments(self) -> "BranchPattern": # def fix_repeating_arguments(self) -> "BranchPattern":
# """Fix elements that should accumulate/increment values.""" # """Fix elements that should accumulate/increment values."""
# either = [list(child.children) for child in transform(self).children] public function fix_repeating_arguments(): self {
# for case in either: # either = [list(child.children) for child in transform(self).children]
# for e in [child for child in case if case.count(child) > 1]: $either = [];
# if type(e) is Argument or type(e) is Option and e.argcount: foreach (Util::transform($this)->children as $child) {
# if e.value is None: $either[] = (array) $child->children;
# e.value = [] }
# elif type(e.value) is not list: # for case in either:
# e.value = e.value.split() # for e in [child for child in case if case.count(child) > 1]:
# if type(e) is Command or type(e) is Option and e.argcount == 0: # if type(e) is Argument or type(e) is Option and e.argcount:
# e.value = 0 # if e.value is None:
# return self # e.value = []
# elif type(e.value) is not list:
# e.value = e.value.split()
# if type(e) is Command or type(e) is Option and e.argcount == 0:
# e.value = 0
# return self
}
# def __repr__(self) -> str: # def __repr__(self) -> str:
# return "%s(%s)" % (self.__class__.__name__, ", ".join(repr(a) for a in self.children)) # return "%s(%s)" % (self.__class__.__name__, ", ".join(repr(a) for a in self.children))

Loading…
Cancel
Save