mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-11-04 00:50:18 +01:00 
			
		
		
		
	Add command help option
This commit is contained in:
		
							parent
							
								
									bbefc65888
								
							
						
					
					
						commit
						4c519dd3bc
					
				@ -26,6 +26,10 @@ abstract class AbstractCommand
 | 
			
		||||
     */
 | 
			
		||||
    protected $config = null;
 | 
			
		||||
 | 
			
		||||
    private $helpMessage;
 | 
			
		||||
    private $usageExamples = [];
 | 
			
		||||
    private $syntaxMessage;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Runs the Command
 | 
			
		||||
     * @return integer exit code
 | 
			
		||||
@ -52,4 +56,65 @@ abstract class AbstractCommand
 | 
			
		||||
    {
 | 
			
		||||
        return $this->config;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setHelpMessage($message)
 | 
			
		||||
    {
 | 
			
		||||
        $this->helpMessage = $message;
 | 
			
		||||
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function addUsageExample($snippet, $description = '')
 | 
			
		||||
    {
 | 
			
		||||
        array_push($this->usageExamples, [$snippet, $description]);
 | 
			
		||||
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setSyntaxMessage($message)
 | 
			
		||||
    {
 | 
			
		||||
        $this->syntaxMessage = $message;
 | 
			
		||||
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getInfoMessage()
 | 
			
		||||
    {
 | 
			
		||||
        $indent = str_repeat(" ", 4);
 | 
			
		||||
 | 
			
		||||
        $output = "";
 | 
			
		||||
 | 
			
		||||
        if (!empty($this->helpMessage)) {
 | 
			
		||||
            $output .= "\n";
 | 
			
		||||
            $output .= $this->helpMessage . "\n";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!empty($this->syntaxMessage)) {
 | 
			
		||||
            $output .= "\n";
 | 
			
		||||
            $output .= "Syntax:\n";
 | 
			
		||||
            $output .= $indent;
 | 
			
		||||
            $output .= $this->syntaxMessage;
 | 
			
		||||
            $output .= "\n";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!empty($this->usageExamples)) {
 | 
			
		||||
            $output .= "\n";
 | 
			
		||||
            $output .= "Usage examples:\n";
 | 
			
		||||
            foreach ($this->usageExamples as $example) {
 | 
			
		||||
                $snippet = $example[0];
 | 
			
		||||
                $description = $example[1];
 | 
			
		||||
                $output .= "$indent* ";
 | 
			
		||||
                if (!empty($description)) {
 | 
			
		||||
                    $description = rtrim($description, ': ') . ":";
 | 
			
		||||
                    $output .= $description;
 | 
			
		||||
                    $output .= "\n$indent$indent";
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                $output .= $snippet;
 | 
			
		||||
                $output .= "\n";
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $output;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -44,4 +44,175 @@ class AbstractCommandTest extends BaseTest
 | 
			
		||||
        $configMock = $this->getMock('Mage\Config');
 | 
			
		||||
        $this->doTestGetter($this->abstractCommand, 'config', $configMock);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function infoMessageProvider()
 | 
			
		||||
    {
 | 
			
		||||
        return [
 | 
			
		||||
            'happy_path' => [
 | 
			
		||||
                'helpMessage' => 'This command does everything you want to',
 | 
			
		||||
                'examples' => [
 | 
			
		||||
                    [
 | 
			
		||||
                        'snippet' => 'mage example',
 | 
			
		||||
                        'description' => 'Default command'
 | 
			
		||||
                    ],
 | 
			
		||||
                    [
 | 
			
		||||
                        'snippet' => 'mage example light',
 | 
			
		||||
                        'description' => 'Runs the command with lights'
 | 
			
		||||
                    ]
 | 
			
		||||
                ],
 | 
			
		||||
                'syntax' => 'mage example [light]',
 | 
			
		||||
                'output' => "\n"
 | 
			
		||||
                    . "This command does everything you want to\n"
 | 
			
		||||
                    . "\n"
 | 
			
		||||
                    . "Syntax:\n"
 | 
			
		||||
                    . "    mage example [light]\n"
 | 
			
		||||
                    . "\n"
 | 
			
		||||
                    . "Usage examples:\n"
 | 
			
		||||
                    . "    * Default command:\n"
 | 
			
		||||
                    . "        mage example\n"
 | 
			
		||||
                    . "    * Runs the command with lights:\n"
 | 
			
		||||
                    . "        mage example light\n"
 | 
			
		||||
            ],
 | 
			
		||||
            'no_help_message' => [
 | 
			
		||||
                'helpMessage' => '',
 | 
			
		||||
                'examples' => [
 | 
			
		||||
                    [
 | 
			
		||||
                        'snippet' => 'mage example',
 | 
			
		||||
                        'description' => 'Default command'
 | 
			
		||||
                    ],
 | 
			
		||||
                    [
 | 
			
		||||
                        'snippet' => 'mage example light',
 | 
			
		||||
                        'description' => 'Runs the command with lights'
 | 
			
		||||
                    ]
 | 
			
		||||
                ],
 | 
			
		||||
                'syntax' => 'mage example [light]',
 | 
			
		||||
                'output' => "\n"
 | 
			
		||||
                    . "Syntax:\n"
 | 
			
		||||
                    . "    mage example [light]\n"
 | 
			
		||||
                    . "\n"
 | 
			
		||||
                    . "Usage examples:\n"
 | 
			
		||||
                    . "    * Default command:\n"
 | 
			
		||||
                    . "        mage example\n"
 | 
			
		||||
                    . "    * Runs the command with lights:\n"
 | 
			
		||||
                    . "        mage example light\n"
 | 
			
		||||
            ],
 | 
			
		||||
            'no_examples' => [
 | 
			
		||||
                'helpMessage' => 'This command does everything you want to',
 | 
			
		||||
                'examples' => [],
 | 
			
		||||
                'syntax' => 'mage example [light]',
 | 
			
		||||
                'output' => "\n"
 | 
			
		||||
                    . "This command does everything you want to\n"
 | 
			
		||||
                    . "\n"
 | 
			
		||||
                    . "Syntax:\n"
 | 
			
		||||
                    . "    mage example [light]\n"
 | 
			
		||||
            ],
 | 
			
		||||
            "no_syntax" => [
 | 
			
		||||
                'helpMessage' => 'This command does everything you want to',
 | 
			
		||||
                'examples' => [
 | 
			
		||||
                    [
 | 
			
		||||
                        'snippet' => 'mage example',
 | 
			
		||||
                        'description' => 'Default command'
 | 
			
		||||
                    ],
 | 
			
		||||
                    [
 | 
			
		||||
                        'snippet' => 'mage example light',
 | 
			
		||||
                        'description' => 'Runs the command with lights'
 | 
			
		||||
                    ]
 | 
			
		||||
                ],
 | 
			
		||||
                'syntax' => '',
 | 
			
		||||
                'output' => "\n"
 | 
			
		||||
                    . "This command does everything you want to\n"
 | 
			
		||||
                    . "\n"
 | 
			
		||||
                    . "Usage examples:\n"
 | 
			
		||||
                    . "    * Default command:\n"
 | 
			
		||||
                    . "        mage example\n"
 | 
			
		||||
                    . "    * Runs the command with lights:\n"
 | 
			
		||||
                    . "        mage example light\n"
 | 
			
		||||
            ],
 | 
			
		||||
            "stripping_colons" => [
 | 
			
		||||
                'helpMessage' => 'This command does everything you want to',
 | 
			
		||||
                'examples' => [
 | 
			
		||||
                    [
 | 
			
		||||
                        'snippet' => 'mage example',
 | 
			
		||||
                        'description' => 'Default command:'
 | 
			
		||||
                    ],
 | 
			
		||||
                    [
 | 
			
		||||
                        'snippet' => 'mage example light',
 | 
			
		||||
                        'description' => 'Runs the command with lights:'
 | 
			
		||||
                    ]
 | 
			
		||||
                ],
 | 
			
		||||
                'syntax' => 'mage example [light]',
 | 
			
		||||
                'output' => "\n"
 | 
			
		||||
                    . "This command does everything you want to\n"
 | 
			
		||||
                    . "\n"
 | 
			
		||||
                    . "Syntax:\n"
 | 
			
		||||
                    . "    mage example [light]\n"
 | 
			
		||||
                    . "\n"
 | 
			
		||||
                    . "Usage examples:\n"
 | 
			
		||||
                    . "    * Default command:\n"
 | 
			
		||||
                    . "        mage example\n"
 | 
			
		||||
                    . "    * Runs the command with lights:\n"
 | 
			
		||||
                    . "        mage example light\n"
 | 
			
		||||
            ],
 | 
			
		||||
            "only_help" => [
 | 
			
		||||
                'helpMessage' => 'This command does everything you want to',
 | 
			
		||||
                'examples' => [],
 | 
			
		||||
                'syntax' => '',
 | 
			
		||||
                'output' => "\n"
 | 
			
		||||
                    . "This command does everything you want to\n"
 | 
			
		||||
            ],
 | 
			
		||||
            "only_examples" => [
 | 
			
		||||
                'helpMessage' => '',
 | 
			
		||||
                'examples' => [
 | 
			
		||||
                    [
 | 
			
		||||
                        'snippet' => 'mage example',
 | 
			
		||||
                        'description' => 'Default command'
 | 
			
		||||
                    ],
 | 
			
		||||
                    [
 | 
			
		||||
                        'snippet' => 'mage example light',
 | 
			
		||||
                        'description' => 'Runs the command with lights'
 | 
			
		||||
                    ]
 | 
			
		||||
                ],
 | 
			
		||||
                'syntax' => '',
 | 
			
		||||
                'output' => "\n"
 | 
			
		||||
                    . "Usage examples:\n"
 | 
			
		||||
                    . "    * Default command:\n"
 | 
			
		||||
                    . "        mage example\n"
 | 
			
		||||
                    . "    * Runs the command with lights:\n"
 | 
			
		||||
                    . "        mage example light\n"
 | 
			
		||||
            ],
 | 
			
		||||
            "only_syntax" => [
 | 
			
		||||
                'helpMessage' => '',
 | 
			
		||||
                'examples' => [],
 | 
			
		||||
                'syntax' => 'mage example [light]',
 | 
			
		||||
                'output' => "\n"
 | 
			
		||||
                    . "Syntax:\n"
 | 
			
		||||
                    . "    mage example [light]\n"
 | 
			
		||||
            ]
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @covers ::getInfoMessage
 | 
			
		||||
     * @covers ::setHelpMessage
 | 
			
		||||
     * @covers ::addUsageExample
 | 
			
		||||
     * @covers ::setSyntaxMessage
 | 
			
		||||
     *
 | 
			
		||||
     * @dataProvider infoMessageProvider
 | 
			
		||||
     */
 | 
			
		||||
    public function testGetInfoMessage($helpMessage, $examples, $syntax, $expectedMessage)
 | 
			
		||||
    {
 | 
			
		||||
        /** @var AbstractCommand $command */
 | 
			
		||||
        $command = $this->getMockForAbstractClass('Mage\Command\AbstractCommand');
 | 
			
		||||
 | 
			
		||||
        foreach ($examples as $example) {
 | 
			
		||||
            $command->addUsageExample($example['snippet'], $example['description']);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $command->setHelpMessage($helpMessage);
 | 
			
		||||
        $command->setSyntaxMessage($syntax);
 | 
			
		||||
 | 
			
		||||
        $actualMessage = $command->getInfoMessage();
 | 
			
		||||
        $this->assertEquals($expectedMessage, $actualMessage);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user