mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-21 22:32:41 +01:00
Add an ArgumentParser helper for integers
Summary: Ref T13676. With increased PHP8.1 strictness around null, it seems reasonable to add some convenience parsing to "PhutilArgumentParser". Add a helper function for parsing integers. Test Plan: See next change. Tried these arguments: ``` --count '' --count asdf --count 0 --count -3 --count 999999999999999999999999999999999999999999999234 --count 5 ``` Got sensible parsing and appropriate feedback in all cases. Maniphest Tasks: T13676 Differential Revision: https://secure.phabricator.com/D21799
This commit is contained in:
parent
e5b92735c6
commit
2969f24961
1 changed files with 27 additions and 0 deletions
|
@ -625,6 +625,33 @@ final class PhutilArgumentParser extends Phobject {
|
|||
return $this->specs[$name]->getDefault();
|
||||
}
|
||||
|
||||
public function getArgAsInteger($name) {
|
||||
$value = $this->getArg($name);
|
||||
|
||||
if ($value === null) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (!preg_match('/^-?\d+\z/', $value)) {
|
||||
throw new PhutilArgumentUsageException(
|
||||
pht(
|
||||
'Parameter provided to argument "--%s" must be an integer.',
|
||||
$name));
|
||||
}
|
||||
|
||||
$intvalue = (int)$value;
|
||||
|
||||
if (phutil_string_cast($intvalue) !== phutil_string_cast($value)) {
|
||||
throw new PhutilArgumentUsageException(
|
||||
pht(
|
||||
'Parameter provided to argument "--%s" is too large to '.
|
||||
'parse as an integer.',
|
||||
$name));
|
||||
}
|
||||
|
||||
return $intvalue;
|
||||
}
|
||||
|
||||
public function getUnconsumedArgumentVector() {
|
||||
return $this->argv;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue