From 496e3a6f34d97e44e44340c1fbd2d3cbc262c05c Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Mon, 17 Mar 2014 23:03:53 +0100 Subject: completely new base for CLI scripts This introduces an abstract base class that command line tools need to inherit from. It provides a simple framework for registering accepted command line options and provides commonly needed things like help output and colored text. Existing CLI scripts still need to be converted. --- _test/tests/inc/cli_options.test.php | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 _test/tests/inc/cli_options.test.php (limited to '_test') diff --git a/_test/tests/inc/cli_options.test.php b/_test/tests/inc/cli_options.test.php new file mode 100644 index 000000000..1d121d7ce --- /dev/null +++ b/_test/tests/inc/cli_options.test.php @@ -0,0 +1,56 @@ +registerOption('exclude', 'exclude files', 'x', true); + + $args = array('-x', 'foo', 'bang'); + $options->parseOptions($args); + + $this->assertEquals('foo', $options->getOpt('exclude')); + $this->assertEquals(array('bang'), $args); + $this->assertFalse($options->getOpt('nothing')); + } + + function test_simplelong1() { + $options = new DokuCLI_Options(); + $options->registerOption('exclude', 'exclude files', 'x', true); + + $args = array('--exclude', 'foo', 'bang'); + $options->parseOptions($args); + + $this->assertEquals('foo', $options->getOpt('exclude')); + $this->assertEquals(array('bang'), $args); + $this->assertFalse($options->getOpt('nothing')); + } + + function test_simplelong2() { + $options = new DokuCLI_Options(); + $options->registerOption('exclude', 'exclude files', 'x', true); + + $args = array('--exclude=foo', 'bang'); + $options->parseOptions($args); + + $this->assertEquals('foo', $options->getOpt('exclude')); + $this->assertEquals(array('bang'), $args); + $this->assertFalse($options->getOpt('nothing')); + } + + function test_complex() { + $options = new DokuCLI_Options(); + + $options->registerOption('plugins', 'run on plugins only', 'p'); + $options->registerCommand('status', 'display status info'); + $options->registerOption('long', 'display long lines', 'l', false, 'status'); + + $args = array('-p', 'status', '--long', 'foo'); + $options->parseOptions($args); + + $this->assertEquals('status', $options->getCmd()); + $this->assertTrue($options->getOpt('plugins')); + $this->assertTrue($options->getOpt('long')); + $this->assertEquals(array('foo'), $args); + } +} \ No newline at end of file -- cgit v1.2.3