PHP getopt Operations

后端 未结 3 1658
梦如初夏
梦如初夏 2021-01-11 19:19

This question is regarding getopt function in php. I need to pass two parameter to the php scripts like

php script.php -f filename -t filetype
相关标签:
3条回答
  • 2021-01-11 19:25

    If you just put something like this in your PHP script (called, on my machine, temp.php) :

    <?php
    $file = getopt("f:t:");
    var_dump($file);
    

    And call it from the command-line, passing those two parameters :

    php temp.php -f filename -t filetype
    

    You'll get this kind of output :

    array(2) {
      ["f"]=>
      string(8) "filename"
      ["t"]=>
      string(8) "filetype"
    }
    

    Which means that :

    • $file['f'] contains the value passed for -f
    • and $file['t'] contains the value passed for -t


    Starting from there, if you want your switch to depend on the option passed as the value of -t, you'll use something like this :

    switch ($file['t']) {
        case 'u':
                // ...
            break;
        case 'c':
                // ...
            break;
        case 'i':
                // ...
            break;
    }
    

    Basically, you put :

    • The variable that you want to test in the switch()
    • And the possible values in the cases


    And, for more informations, you can read, in the PHP manual :

    • The manual page of switch
    • And getopt()
    0 讨论(0)
  • 2021-01-11 19:44

    I implemented a getopt parser for this, support short,long option names, type constraint, option printing.. etc. and this library has been maintained over 6 years.

    Synopsis:

    <?php
    
    use GetOptionKit\OptionCollection;
    use GetOptionKit\OptionParser;
    
    $options = new OptionCollection;
    $spec = $options->add( 'f|foo:' , 'option require value' );  # returns spec object.
    
    $options->add( 'b|bar+' , 'option with multiple value' );
    $options->add( 'z|zoo?' , 'option with optional value' );
    
    $options->add( 'f|foo:=i' , 'option require value, with integer type' );
    $options->add( 'f|foo:=s' , 'option require value, with string type' );
    
    $options->add( 'v|verbose' , 'verbose flag' );
    $options->add( 'd|debug'   , 'debug flag' );
    
    $parser = new OptionParser;
    $result = $parser->parse( array( 'program' , '-f' , 'foo value' , '-v' , '-d' ) );
    
    $result = $parser->parse( $argv );
    
    $spec = $result->verbose;
    $spec = $result->debug;
    $spec->value;  # get value
    

    GetOptionKit\OptionPrinter can print options for you:

    * Available options:
                  -f, --foo   option requires a value.
                  -b, --bar   option with multiple value.
                  -z, --zoo   option with optional value.
              -v, --verbose   verbose message.
                -d, --debug   debug message.
                     --long   long option name only.
                         -s   short option name only.
    

    Demo

    Please check examples/demo.php.

    Run:

    % php examples/demo.php -f test -b 123 -b 333
    

    Print:

    * Available options:
          -f, --foo <value>    option requires a value.
         -b, --bar <value>+    option with multiple value.
        -z, --zoo [<value>]    option with optional value.
              -v, --verbose    verbose message.
                -d, --debug    debug message.
                     --long    long option name only.
                         -s    short option name only.
    Enabled options: 
    * key:foo      spec:-f, --foo <value>  desc:option requires a value.
        value => test
    
    * key:bar      spec:-b, --bar <value>+  desc:option with multiple value.
        Array
        (
            [0] => 123
            [1] => 333
        )
    

    GetOptionKit is on GitHub: https://github.com/c9s/GetOptionKit

    0 讨论(0)
  • 2021-01-11 19:44

    There is a problem in your approach. What if user gives following command line by mistake. the script will not be able to detect it and fail as $file['t'] will be an array.

    php temp.php -f filename -t filetype -f filename2
    
    0 讨论(0)
提交回复
热议问题