Skip to content

Instantly share code, notes, and snippets.

@igorw
Last active December 16, 2015 12:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorw/5435074 to your computer and use it in GitHub Desktop.
Save igorw/5435074 to your computer and use it in GitHub Desktop.
Using symfony/console's ArgvInput standalone.
<?php
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
$input = new ArgvInput($argv, new InputDefinition([
new InputArgument('sql', InputArgument::REQUIRED),
new InputOption('update', 'u', InputOption::VALUE_NONE),
]));
$update = $input->getOption('update');
$sql = $input->getArgument('sql');
// do fun stuff ...
@igorw
Copy link
Author

igorw commented Apr 22, 2013

You call the script by running:

$ import/sql -u "DROP TABLE IF EXISTS foo"
$ import/sql "SELECT * FROM foo"

@till
Copy link

till commented Apr 22, 2013

@igorw You could use getopt() too.

@gnugat
Copy link

gnugat commented Apr 22, 2013

Or docopt :

<?php
$doc = <<<DOC
Usage:
  import/sql [-u | --update] sql
  import/sql (-h | --help)
  import/sql --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  -u --update  Fun stuff.

DOC;

require('path/to/src/docopt.php');
$args = Docopt\docopt($doc, array('version'=>'import SQL 2.0'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment