PHP code example of jdwx / args

1. Go to this page and download the library: Download jdwx/args library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jdwx / args example snippets




declare( strict_types = 1 );

the script name from the arguments for our example.
$argv = array_slice( $argv, 1 );

$args = new Arguments( $argv );
echo $args->shiftString(), "\n"; # Echoes "Hello," in the given example.


$args = new JDWX\Args\Arguments( [ 'not-an-email-address' ] );
try {
    $email = $args->shiftEmailAddress();
} catch ( JDWX\Args\BadArgumentException $e) {
    echo "Not a valid email address: ", $e->getValue(), "\n";
}

$args = new Arguments( [ '1', '2', '3', '4', '5' ] );
while ( $i = $args->shiftInteger() ) {
    echo "Got integer: $i\n";
}

while ( $st = $args->shiftString() ) {
    $i = $args->shiftIntegerEx();
    assert( is_int( $i ) );
    echo "Got: $st => $i\n";
}


# If the next argument is "prefix_example," $st will be set to "example."
# In this example, the i_bConsume flag is not set, so the argument will not
# be consumed.
$args = new JDWX\Args\Arguments( [ 'prefix_example' ] );
if ( $st = $args->peekString( 'prefix_' ) ) {
    echo "Got: {$st}\n";
    $st2 = $args->shiftString();
    echo "Shifted: {$st2}\n"; # Argument was not consumed.
} else {
    echo "Nope!\n";
}


$rKeywords = [ 'example', 'demo', 'test' ];
# If the next argument is "example," $st will be set to "example." In this example,
# the consume flag is set so the argument is consumed if (and only if) it matches.
# The default is kept as false for consistency with peekString(), but usually
# you do want to consume keyword-matching arguments.
if ( $st = $args->peekKeywords( $rKeywords, i_bConsume: true ) ) {
    echo "Keyword: {$st}\n";
} else {
    echo "Nope!\n";
}

$args = new Arguments( 'Hello, "world!"' );
echo 'parsed arg 1 = ', $args->shiftString(), "\n"; # Echoes "Hello,"
echo 'parsed arg 2 = ', $args->shiftString(), "\n"; # Echoes "world!" (no quotes)

$options = new Options( [
    new Option( 'foo' ),
    new Option( 'bar', i_bstValue: true ),
    new Option( 'baz', i_bFlagOnly: false ),
    new Option( 'qux', '1', '0' ),
] );
$args = new JDWX\Args\Arguments( [ '--foo', '--no-bar', '--baz=quux', '--qux=3' ] );
$options->fromArguments( $args );
echo 'foo = ', $options[ 'foo' ]->asBool() ? 'true' : 'false', "\n";
echo 'bar = ', $options[ 'bar' ]->asBool() ? 'true' : 'false', "\n";
echo 'baz = ', $options[ 'baz' ]->asString(), "\n";
for ( $ii = 0 ; $ii < $options[ 'qux' ]->asInt() ; ++$ii ) {
    echo "qux\n";
}

$args = new Arguments( [ "--foo=bar" ] );
$option = new Option( 'foo', i_bFlagOnly: false );
$option->set( $args );
echo 'foo as bool = ', $option->asBool() ? 'true' : 'Nope!', "\n"; # Echoes "true" because flag was present.
echo 'foo as str = ', $option->asString(), "\n"; # Echoes "bar" in the given example.

$args = new Arguments( [ '--foo=bar', '--baz' ] );
echo "1-line foo = ", Option::simpleString( 'foo', $args ), "\n"; # Echoes "bar"
echo "1-line baz = ", Option::simpleBool( 'baz', $args ) ? 'true' : 'Nope!', "\n"; # Echoes "true"

$args = new Arguments( [ '--foo=bar', '--baz', '--no-qux', 'leftover' ] );
$rOptions = $args->handleOptions();
echo $rOptions[ 'foo' ], "\n"; # Echoes "bar"
echo ( $rOptions[ 'baz' ] === true ) ? 'true' : 'Nope!', "\n"; # Echoes "true"
echo ( $rOptions[ 'qux' ] === false ) ? 'false' : 'Nope!', "\n"; # Echoes "false"
echo $args->shiftString(), "\n"; # Echoes "leftover"
bash
YourPrompt$ ./example.php Hello, world.