#!/usr/bin/php <?php // This is an example demonstrating how to use the cliargs command-line argument // library. By Pete Warden, http://petewarden.typepad.com - freely reusable // with no restrictions. require_once 'cliargs.php'; $cliargs = array('filename' => array('short' => 'f', 'type' => 'required', 'description' => 'This is a required argument, the value can be specified as either -f/--filename=<value> or -f/--filename <value>. If the argument is not specified, the script will print this usage information and exit', 'default' => ''), 'queryurl' => array('short' => 'q', 'type' => 'optional', 'description' => 'This is an optional argument, if -q/--queryurl is not specified, the default value of "empty" will be set into the result array', 'default' => 'http://example.com'), 'dohost' => array('short' => 'h', 'type' => 'switch', 'description' => 'Switches default to false if they\'re not included on the command line, or true if they are present (you don\'t specify a value)')); $options = cliargs_get_options($cliargs); $filename = $options['filename']; $queryurl = $options['queryurl']; $dohost = $options['dohost']; if ($filename === 'bad') { print "You gave me a filename I didn't like: '{$filename}'\n"; cliargs_print_usage_and_exit(); } print "All arguments set: filename='{$filename}', queryurl='{$queryurl}', dohost='"; if ($dohost) { print "true"; } else { print "false"; } print "'\n"; $unnamed = $options['unnamed']; if (empty($unnamed)) { print "No unnamed arguments\n"; } else { print "Unnamed arguments: " . print_r($unnamed, true); }
$cliargs = array('inputfile' => array('short' => 'i', 'type' => 'optional', 'description' => 'The file to read the input OSM XML data from - if unset, will read from stdin', 'default' => 'php://stdout'), 'outputfile' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the output OSM XML data to - if unset, will write to stdout', 'default' => 'php://stdout'), 'top_left' => array('short' => 't', 'type' => 'required', 'description' => 'The top-left corner of the box to crop the ways to, as a latitude,longitude pair, eg 40,20'), 'bottom_right' => array('short' => 'b', 'type' => 'required', 'description' => 'The bottom-right corner of the box to crop the ways to, as a latitude,longitude pair, eg 40,20'), 'verbose' => array('short' => 'v', 'type' => 'switch', 'description' => 'Enables debugging output about the job\'s progress')); $options = cliargs_get_options($cliargs); $input_file = $options['inputfile']; $output_file = $options['outputfile']; $top_left = $options['top_left']; $bottom_right = $options['bottom_right']; $verbose = $options['verbose']; $top_left_parts = explode(',', $top_left); if (count($top_left_parts) !== 2) { print "Bad top_left parameter '{$top_left}'"; cliargs_print_usage_and_exit($cliargs); } $bottom_right_parts = explode(',', $bottom_right); if (count($bottom_right_parts) !== 2) { print "Bad bottom_right parameter '{$bottom_right}'"; cliargs_print_usage_and_exit($cliargs); } $min_lat = min($top_left_parts[0], $bottom_right_parts[0]); $max_lat = max($top_left_parts[0], $bottom_right_parts[0]); $min_lon = min($top_left_parts[1], $bottom_right_parts[1]); $max_lon = max($top_left_parts[1], $bottom_right_parts[1]); if ($verbose) { error_log("Starting load of '{$input_file}'"); } $input_osm_ways = new OSMWays(); $input_contents = file_get_contents($input_file) or die("Couldn't read file '{$input_file}'"); $input_osm_ways->deserialize_from_xml($input_contents); $output_osm_ways = crop_ways_to_box($input_osm_ways, $min_lat, $min_lon, $max_lat, $max_lon, $verbose); if ($verbose) { error_log("Starting save of '{$output_file}'"); }
function cliargs_get_options($cliargs) { global $argv; global $argc; $options = array('unnamed' => array()); for ($index = 1; $index < $argc; $index += 1) { $currentarg = strtolower($argv[$index]); $argparts = split('=', $currentarg); $namepart = $argparts[0]; if (cliargs_strstartswith($namepart, '--')) { $longname = substr($namepart, 2); } else { if (cliargs_strstartswith($namepart, '-')) { $shortname = substr($namepart, 1); $longname = $shortname; foreach ($cliargs as $name => $info) { if ($shortname === $info['short']) { $longname = $name; break; } } } else { $longname = 'unnamed'; } } if ($longname == 'unnamed') { $options['unnamed'][] = $namepart; } else { if (empty($cliargs[$longname])) { print "Unknown argument '{$longname}'\n"; cliargs_print_usage_and_exit($cliargs); } $arginfo = $cliargs[$longname]; $argtype = $arginfo['type']; if ($argtype === 'switch') { $value = true; } else { if (isset($argparts[1])) { $value = $argparts[1]; } else { if ($index + 1 < $argc) { $value = $argv[$index + 1]; $index += 1; } else { print "Missing value after '{$longname}'\n"; cliargs_print_usage_and_exit($cliargs); } } } $options[$longname] = $value; } } foreach ($cliargs as $longname => $arginfo) { $type = $arginfo['type']; if (!isset($options[$longname])) { if ($type == 'required') { print "Missing required value for '{$longname}'\n"; cliargs_print_usage_and_exit($cliargs); } else { if ($type == 'optional') { if (!isset($arginfo['default'])) { die('Missing default value for ' . $long); } $options[$longname] = $arginfo['default']; } else { if ($type == 'switch') { $options[$longname] = false; } } } } } return $options; }