ali-eltaweel/command-parser

There is no license information available for the latest version (1.0.0) of this package.

Shell-style Command-line Parser.

1.0.0 2025-06-28 04:21 UTC

This package is auto-updated.

Last update: 2025-06-28 04:22:56 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);