|
16 | 16 | $endpoints = []; |
17 | 17 | $domjudge_config = []; |
18 | 18 |
|
| 19 | +function dj_getopt(string $short_options, array $long_options = []): array |
| 20 | +{ |
| 21 | + global $argv; |
| 22 | + define('GETOPT_REGEX', "/^([a-zA-Z0-9]:{0,2})*$/"); |
| 23 | + $options = getopt($short_options, $long_options); |
| 24 | + if (preg_match(GETOPT_REGEX, $short_options) !== 1) { |
| 25 | + echo "Error: short options format specified is invalid.\n"; |
| 26 | + usage(); |
| 27 | + } |
| 28 | + if ($options===false || !is_array($argv)) { |
| 29 | + echo "Error: parsing options failed.\nPlease check: `register_argc_arg` in php.ini.\n"; |
| 30 | + usage(); |
| 31 | + } |
| 32 | + check_unknown_cli_options($options); |
| 33 | + return $options; |
| 34 | +} |
| 35 | + |
| 36 | +function check_unknown_cli_options(array $options): void |
| 37 | +{ |
| 38 | + global $argc; |
| 39 | + global $argv; |
| 40 | + // Cleanup the original arguments |
| 41 | + $t_argv = $argv; |
| 42 | + // The first is the script name |
| 43 | + $t_argc = $argc-1; |
| 44 | + unset($t_argv[0]); |
| 45 | + $t_argv = array_values($t_argv); |
| 46 | + |
| 47 | + foreach ($options as $option_key => $option_value) { |
| 48 | + $short = '-'.$option_key; |
| 49 | + $long = '--'.$option_key; |
| 50 | + $possible = [$short, $long]; |
| 51 | + |
| 52 | + $values = []; |
| 53 | + if ($option_value !== false) { |
| 54 | + if (is_string($option_value)) { |
| 55 | + $values = [$option_value]; |
| 56 | + } else { |
| 57 | + $values = $option_value; |
| 58 | + } |
| 59 | + foreach ($values as $value) { |
| 60 | + $possible[] = $short.$value; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + $t_remove = []; |
| 65 | + for ($i = 0; $i < $t_argc; $i++) { |
| 66 | + if (in_array($t_argv[$i], array_merge($possible, $values))) { |
| 67 | + $t_remove[] = $i; |
| 68 | + } |
| 69 | + } |
| 70 | + foreach ($t_remove as $t) { |
| 71 | + unset($t_argv[$t]); |
| 72 | + } |
| 73 | + $t_argv = array_values($t_argv); |
| 74 | + $t_argc -= count($t_remove); |
| 75 | + } |
| 76 | + |
| 77 | + if (count($t_argv) !== 0) { |
| 78 | + echo "Error: found unknown arguments: '" . implode(' ', $t_argv) . "'.\n"; |
| 79 | + usage(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
19 | 83 | function judging_directory(string $workdirpath, array $judgeTask) : string |
20 | 84 | { |
21 | 85 | if (filter_var($judgeTask['submitid'], FILTER_VALIDATE_INT) === false || |
@@ -483,16 +547,7 @@ function fetch_executable_internal( |
483 | 547 | return [$execrunpath, null, null]; |
484 | 548 | } |
485 | 549 |
|
486 | | -$shortoptions = "dv:n:hVe:j:t:"; |
487 | | -$regex = "/^([a-zA-Z0-9]:{0,2})*$/"; |
488 | | -if (preg_match($regex, $shortoptions) !== 1) { |
489 | | - echo "Error: short options format specified is invalid.\n"; |
490 | | -} |
491 | | -$options = getopt($shortoptions, ["diskspace-error"]); |
492 | | -if ($options===false || !is_array($argv)) { |
493 | | - echo "Error: parsing options failed.\nPlease check: `register_argc_arg` in php.ini.\n"; |
494 | | - usage(); |
495 | | -} |
| 550 | +$options = dj_getopt("dv:n:hVe:j:t:", ["diskspace-error"]); |
496 | 551 | if (isset($options['v'])) { |
497 | 552 | $options['verbose'] = $options['v']; |
498 | 553 | } |
|
0 commit comments