<?php // Clearer definition of Arguments vs. Flags require dirname(__DIR__) . '/vendor/autoload.php'; // v0.2.0 started to add a clearer definition between "flag" type options // and "argument" type options for those that may prefer it. // In Commando, flags are options that require a name when they are being // specified on the command line. Arguments are options that are not named in // this way. In the example below, '-f' and '--long' are described as "flags" // type options in Commando terms with the values 'value1' and 'value2' // respectively, whereas value3, value4, and value5 are described as "argument" // type options. // php argumentsVsFlags.php -f value1 --long value2 value3 value4 value5 $cmd = new Commando\Command(); $cmd->flag('f')->flag('l')->aka('long')->argument()->argument()->argument(); var_dump($cmd->getArgumentValues()); var_dump($cmd->getFlagValues()); // This is equivalent to... // $cmd = new Commando\Command(); // $cmd // ->option('f') // ->option('l') // ->aka('long') // ->option() // ->option() // ->option();
Command line options: --file [csv file name] – this is the name of the CSV to be parsed --create_table – this will cause the MySQL users table to be built (and no further action will be taken) --dry_run – this will be used with the --file directive in the instance that we want to run the script but not insert into the DB. All other functions will be executed, but the database won't be altered. -u – MySQL username -p – MySQL password -h – MySQL host --help – which will output the above list of directives with details. */ require_once 'vendor/autoload.php'; $cmd_options = new Commando\Command(); // Define option "--file" $cmd_options->option('file')->file()->describedAs('Input file with CSV data to be parced'); // Define flag "--create_table" $cmd_options->flag('create_table')->boolean()->needs('u', 'p', 'h')->describedAs('Instructs to create table in MySQL DB with name "users"'); // Define flag "--dry_run" $cmd_options->flag('dry_run')->boolean()->describedAs('No data will be added to DB. All other functions will be executed'); // Define option "u" $cmd_options->option('u')->needs('p', 'h')->describedAs('MySQL username'); // Define option "-p" $cmd_options->option('p')->needs('u', 'h')->describedAs('Password of MySQL user'); // Define option "-h" $cmd_options->option('h')->needs('u', 'p')->describedAs('MySQL hostname or IP'); //Compose help message $help = "Usage:\n\nCreating table:\n\tphp user_upload.php --create_table -u <MySQL user>\n\t\t-p <MySQL user password> -h <MySQL hostname>\n\t\t\nDry run, check CSV content and exit without importing to DB:\n\tphp user_upload.php --dru_run --file <CSV filename>\n\nImporting file:\n\tphp user_upload.php --file <CSV filename> -u <MySQL user> \n\t\t-p <MySQL user password> -h <MySQL hostname>\n\nIf invalid emails are found in CSV then no DB insert will be done.\n\t\t\nOnly above described sets of options could be used. E.g. you cannot use --create_table together with --dry_run \n"; $cmd_options->setHelp($help); //Aligning options and flags to be either set or unset. $csv_file = $cmd_options['file']; if ($cmd_options['create_table']) { $create_table = 1;