-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Argument parsing refactor and urgent mode (#221)
* Coordinate command line on ArgvParser class * Urgent option to focus on avoiding or fixing translations manual builds * Command line option to ignore entity by name Co-authored-by: André L F S Bacci <[email protected]> Co-authored-by: Kamil Tekiela <[email protected]>
- Loading branch information
1 parent
c63809f
commit 80d1314
Showing
7 changed files
with
266 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php /* | ||
+----------------------------------------------------------------------+ | ||
| Copyright (c) 1997-2025 The PHP Group | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 3.01 of the PHP license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| https://www.php.net/license/3_01.txt. | | ||
| If you did not receive a copy of the PHP license and are unable to | | ||
| obtain it through the world-wide-web, please send a note to | | ||
| [email protected], so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Authors: André L F S Bacci <ae php.net> | | ||
+----------------------------------------------------------------------+ | ||
# Description | ||
This class coordinates and centrailzes control for $argv command line | ||
parameters, used between vairous classes. */ | ||
|
||
class ArgvParser | ||
{ | ||
private array $argv; | ||
private array $used; | ||
|
||
public function __construct( array $argv ) | ||
{ | ||
$this->argv = array_values( array_filter( $argv ) ); | ||
$this->used = []; | ||
$this->used = array_fill( 0 , count( $argv ) , false ); | ||
} | ||
|
||
public function use( string $arg ) : void | ||
{ | ||
foreach ( $this->argv as $pos => $value ) | ||
if ( $arg == $value && $this->used[ $pos ] == false ) | ||
{ | ||
$this->used[ $pos ] = true; | ||
return; | ||
} | ||
throw new Exception( "Unused '$arg' not found." ); | ||
} | ||
|
||
public function consume( string $equals = null , string $prefix = null , int $position = -1 ) : string|null | ||
{ | ||
$args = $this->argv; | ||
foreach ( $args as $pos => $arg ) | ||
{ | ||
if ( $arg == null ) | ||
continue; | ||
|
||
$foundByEquals = $equals != null && $arg == $equals; | ||
$foundByPrefix = $prefix != null && str_starts_with( $arg , $prefix ); | ||
$foundByPosition = $position == $pos; | ||
|
||
if ( $foundByEquals || $foundByPrefix || $foundByPosition ) | ||
{ | ||
$this->argv[ $pos ] = null; | ||
$this->used[ $pos ] = true; | ||
|
||
return $arg; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function complete() : void | ||
{ | ||
foreach ( $this->argv as $pos => $arg ) | ||
if ( $this->used[ $pos ] == false ) | ||
fwrite( STDERR , "Unknown argument: {$arg}\n\n" ); | ||
} | ||
|
||
public function residual() : array | ||
{ | ||
return array_filter( $this->argv ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.