plonknimbuzz
plonknimbuzz

Reputation: 2664

How to passing PHP CLI with custom argument mask

i wanna ask about run PHP via CLI with given parameter. yesterday i run some php script from CLI using parameter/argument, i wonder how to do that. since i cant see the code, so i search for it from google.

what i know until now

<?php
    if(count($argv) <= 1) die('please provide input'.PHP_EOL);

    echo "first input: $argv[1]".PHP_EOL;
    if(!empty($argv[2])) echo "second input: $argv[2]".PHP_EOL;
?>

run: php cli.php param1 param2

result:

first input: param1
second input: param2

when i miss input, the script will be error

run: php cli.php param2 param1

result:

first input: param2
second input: param1 

my question is: how to write code like this

run: php cli.php --input2 param2 --input1 param1

or: php cli.php -input2 param2 -input1 param1

result:

first input: param1
second input: param2

i already read 2 tutorial article and PHP docs getopt() but i still can't do it right

Upvotes: 0

Views: 94

Answers (1)

Edwin
Edwin

Reputation: 2278

You were on the right track. Based on getopt example 2 you can do:

getopt([], ['input2:','input1:']); 
//for longoptions or fill the 1st array for the shortoptions

Upvotes: 1

Related Questions