ali-eltaweel / command-parser
Shell-style Command-line Parser.
Installs: 35
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/ali-eltaweel/command-parser
Requires
- php: ^8.1
This package is auto-updated.
Last update: 2025-11-02 01:26:05 UTC
README
Shell-style Command-line Parser
Installation
Install command-parser via Composer:
composer require ali-eltaweel/command-parser
Usage
Defining Commands
use CommandParser\Specs\{ Command, Operand, Option, OptionToken, OptionTokenType }; $git = new Command( name: 'git', description: 'Git command-line interface', options: [ new Option( name: 'help', description: 'Display help information', isRepeatable: false, isFlag: true, tokens: [ new OptionToken(token: 'help', type: OptionTokenType::Extended) ] ) ], operands: [], subCommands: [ new Command( name: 'commit', description: 'Record changes to the repository', options: [ new Option( name: 'message', description: 'Use the given <msg> as the commit message', isRepeatable: false, isFlag: false, tokens: [ new OptionToken(token: 'm', type: OptionTokenType::Short), new OptionToken(token: 'message', type: OptionTokenType::Long) ] ) ], operands: [], subCommands: [] ), new Command( name: 'push', description: 'Update remote refs along with associated objects', options: [ new Option( name: 'force', description: 'Force update of the remote ref', isRepeatable: false, isFlag: true, tokens: [ new OptionToken(token: 'f', type: OptionTokenType::Short), new OptionToken(token: 'force', type: OptionTokenType::Long) ] ) ], operands: [ new Operand(index: 0, name: 'remote', description: 'The remote repository to push to'), new Operand(index: 1, name: 'branch', description: 'The branch to push') ], subCommands: [] ) ] );
Parsing Commands
use CommandParser\CommandLineParser; $parser = new CommandLineParser(); $commandLine = $parser->parse(['git', 'commit', '-m', 'Initial commit'], $git); $commandLine = $parser->parse(['git', 'push', 'origin', 'main'], $git);