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 From d48bcdb42c1e80fa258ebbd70d9fe6f649237a78 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Thu, 20 Mar 2014 21:01:06 +0100 Subject: fixed tests --- _test/tests/inc/cli_options.test.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to '_test') diff --git a/_test/tests/inc/cli_options.test.php b/_test/tests/inc/cli_options.test.php index 1d121d7ce..ab03ee29b 100644 --- a/_test/tests/inc/cli_options.test.php +++ b/_test/tests/inc/cli_options.test.php @@ -4,37 +4,37 @@ class cli_options extends DokuWikiTest { function test_simpleshort() { $options = new DokuCLI_Options(); - $options->registerOption('exclude', 'exclude files', 'x', true); + $options->registerOption('exclude', 'exclude files', 'x', 'file'); - $args = array('-x', 'foo', 'bang'); - $options->parseOptions($args); + $options->args = array('-x', 'foo', 'bang'); + $options->parseOptions(); $this->assertEquals('foo', $options->getOpt('exclude')); - $this->assertEquals(array('bang'), $args); + $this->assertEquals(array('bang'), $options->args); $this->assertFalse($options->getOpt('nothing')); } function test_simplelong1() { $options = new DokuCLI_Options(); - $options->registerOption('exclude', 'exclude files', 'x', true); + $options->registerOption('exclude', 'exclude files', 'x', 'file'); - $args = array('--exclude', 'foo', 'bang'); - $options->parseOptions($args); + $options->args = array('--exclude', 'foo', 'bang'); + $options->parseOptions(); $this->assertEquals('foo', $options->getOpt('exclude')); - $this->assertEquals(array('bang'), $args); + $this->assertEquals(array('bang'), $options->args); $this->assertFalse($options->getOpt('nothing')); } function test_simplelong2() { $options = new DokuCLI_Options(); - $options->registerOption('exclude', 'exclude files', 'x', true); + $options->registerOption('exclude', 'exclude files', 'x', 'file'); - $args = array('--exclude=foo', 'bang'); - $options->parseOptions($args); + $options->args = array('--exclude=foo', 'bang'); + $options->parseOptions(); $this->assertEquals('foo', $options->getOpt('exclude')); - $this->assertEquals(array('bang'), $args); + $this->assertEquals(array('bang'), $options->args); $this->assertFalse($options->getOpt('nothing')); } @@ -45,12 +45,12 @@ class cli_options extends DokuWikiTest { $options->registerCommand('status', 'display status info'); $options->registerOption('long', 'display long lines', 'l', false, 'status'); - $args = array('-p', 'status', '--long', 'foo'); - $options->parseOptions($args); + $options->args = array('-p', 'status', '--long', 'foo'); + $options->parseOptions(); $this->assertEquals('status', $options->getCmd()); $this->assertTrue($options->getOpt('plugins')); $this->assertTrue($options->getOpt('long')); - $this->assertEquals(array('foo'), $args); + $this->assertEquals(array('foo'), $options->args); } } \ No newline at end of file -- cgit v1.2.3