Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: php-toolkit/pflag
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.1.1
Choose a base ref
...
head repository: php-toolkit/pflag
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref

Commits on Nov 26, 2021

  1. Copy the full SHA
    d733ce4 View commit details
  2. fix unit test error

    inhere committed Nov 26, 2021
    Copy the full SHA
    5a746ff View commit details

Commits on Dec 2, 2021

  1. Copy the full SHA
    3227051 View commit details

Commits on Dec 3, 2021

  1. fix: flag type check error

    inhere committed Dec 3, 2021
    Copy the full SHA
    2ad6fd5 View commit details
  2. Copy the full SHA
    36903c1 View commit details
  3. Copy the full SHA
    2987de9 View commit details

Commits on Dec 5, 2021

  1. Copy the full SHA
    65c727c View commit details
  2. fix: syntax error on php 7.4

    inhere committed Dec 5, 2021
    Copy the full SHA
    00be077 View commit details
  3. Copy the full SHA
    6bf883c View commit details
  4. Copy the full SHA
    1fcb150 View commit details

Commits on Dec 8, 2021

  1. Copy the full SHA
    c3ecac7 View commit details

Commits on Dec 9, 2021

  1. Copy the full SHA
    1b407de View commit details
  2. fix: unit tests error

    inhere committed Dec 9, 2021
    Copy the full SHA
    9bf54ac View commit details
  3. Copy the full SHA
    f085b16 View commit details

Commits on Dec 12, 2021

  1. up: optimize CliApp code

    inhere committed Dec 12, 2021
    Copy the full SHA
    b1099ef View commit details

Commits on Dec 14, 2021

  1. Copy the full SHA
    7a99f46 View commit details

Commits on Dec 27, 2021

  1. Copy the full SHA
    8e36450 View commit details

Commits on Jan 2, 2022

  1. Copy the full SHA
    24d17d5 View commit details

Commits on Jan 10, 2022

  1. Copy the full SHA
    96b4050 View commit details

Commits on Jan 19, 2022

  1. Copy the full SHA
    6822123 View commit details

Commits on Jan 25, 2022

  1. Copy the full SHA
    df8d27b View commit details
  2. Copy the full SHA
    6246541 View commit details

Commits on Feb 10, 2022

  1. Copy the full SHA
    b83477f View commit details

Commits on Mar 2, 2022

  1. chore(deps): bump actions/checkout from 2 to 3

    Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 3.
    - [Release notes](https://github.com/actions/checkout/releases)
    - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
    - [Commits](actions/checkout@v2...v3)
    
    ---
    updated-dependencies:
    - dependency-name: actions/checkout
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Mar 2, 2022
    Copy the full SHA
    d44eaf4 View commit details
  2. Copy the full SHA
    7bb6daf View commit details

Commits on Mar 22, 2022

  1. chore(deps): bump actions/cache from 2 to 3

    Bumps [actions/cache](https://github.com/actions/cache) from 2 to 3.
    - [Release notes](https://github.com/actions/cache/releases)
    - [Commits](actions/cache@v2...v3)
    
    ---
    updated-dependencies:
    - dependency-name: actions/cache
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Mar 22, 2022
    Copy the full SHA
    c275b6b View commit details
  2. Copy the full SHA
    2fe57cd View commit details

Commits on Apr 12, 2022

  1. Copy the full SHA
    190cf6f View commit details

Commits on Apr 14, 2022

  1. Copy the full SHA
    62bb1f2 View commit details

Commits on Jul 19, 2022

  1. Copy the full SHA
    315aac6 View commit details
  2. Copy the full SHA
    2772c34 View commit details
  3. Copy the full SHA
    6628805 View commit details

Commits on Aug 6, 2022

  1. Copy the full SHA
    1636bce View commit details

Commits on Nov 5, 2022

  1. Copy the full SHA
    6619dc8 View commit details

Commits on Sep 5, 2023

  1. chore(deps): bump actions/checkout from 3 to 4

    Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
    - [Release notes](https://github.com/actions/checkout/releases)
    - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
    - [Commits](actions/checkout@v3...v4)
    
    ---
    updated-dependencies:
    - dependency-name: actions/checkout
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Sep 5, 2023
    Copy the full SHA
    57f1b59 View commit details
  2. Merge pull request #4 from php-toolkit/dependabot/github_actions/acti…

    …ons/checkout-4
    
    chore(deps): bump actions/checkout from 3 to 4
    inhere authored Sep 5, 2023
    Copy the full SHA
    9bae68c View commit details

Commits on Jan 18, 2024

  1. chore(deps): bump actions/cache from 3 to 4

    Bumps [actions/cache](https://github.com/actions/cache) from 3 to 4.
    - [Release notes](https://github.com/actions/cache/releases)
    - [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
    - [Commits](actions/cache@v3...v4)
    
    ---
    updated-dependencies:
    - dependency-name: actions/cache
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Jan 18, 2024
    Copy the full SHA
    feff63a View commit details
  2. Merge pull request #6 from php-toolkit/dependabot/github_actions/acti…

    …ons/cache-4
    
    chore(deps): bump actions/cache from 3 to 4
    inhere authored Jan 18, 2024
    Copy the full SHA
    c420abc View commit details

Commits on Feb 8, 2024

  1. Copy the full SHA
    e5d0fa5 View commit details
  2. Copy the full SHA
    da84181 View commit details
  3. Copy the full SHA
    cec817f View commit details

Commits on Mar 11, 2024

  1. chore(deps): bump softprops/action-gh-release from 1 to 2

    Bumps [softprops/action-gh-release](https://github.com/softprops/action-gh-release) from 1 to 2.
    - [Release notes](https://github.com/softprops/action-gh-release/releases)
    - [Changelog](https://github.com/softprops/action-gh-release/blob/master/CHANGELOG.md)
    - [Commits](softprops/action-gh-release@v1...v2)
    
    ---
    updated-dependencies:
    - dependency-name: softprops/action-gh-release
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Mar 11, 2024
    Copy the full SHA
    6be561c View commit details
  2. Merge pull request #7 from php-toolkit/dependabot/github_actions/soft…

    …props/action-gh-release-2
    
    chore(deps): bump softprops/action-gh-release from 1 to 2
    inhere authored Mar 11, 2024
    Copy the full SHA
    8f5f4b2 View commit details
Showing with 2,395 additions and 626 deletions.
  1. +29 −20 .github/changelog.yml
  2. +5 −20 .github/workflows/php.yml
  3. +10 −35 .github/workflows/release.yml
  4. +1 −0 .gitignore
  5. +1 −0 .php-cs-fixer.php
  6. +134 −4 README.md
  7. +126 −0 README.zh-CN.md
  8. +4 −4 composer.json
  9. 0 example/changelog.md
  10. +52 −0 example/cliapp.php
  11. +45 −0 example/clicmd.php
  12. +3 −3 example/flags-demo.php
  13. BIN example/images/cli-app-cmd-help.png
  14. BIN example/images/cli-app-cmd-run.png
  15. BIN example/images/cli-app-help.png
  16. BIN example/images/cli-cmd-help.png
  17. BIN example/images/cli-cmd-run.png
  18. BIN example/images/flags-demo.png
  19. +8 −2 example/not-stop_on_first.php
  20. +9 −2 example/refer.php
  21. +3 −2 example/sflags-demo.php
  22. +11 −22 phpunit.xml
  23. +695 −0 src/CliApp.php
  24. +176 −0 src/CliCmd.php
  25. +60 −47 src/Concern/HelperRenderTrait.php
  26. +21 −18 src/Concern/RuleParserTrait.php
  27. +41 −0 src/Contract/CmdHandlerInterface.php
  28. +3 −3 src/Contract/FlagInterface.php
  29. +30 −34 src/Contract/ParserInterface.php
  30. +8 −1 src/Contract/ValidatorInterface.php
  31. +8 −1 src/Contract/ValueInterface.php
  32. +8 −2 src/Exception/FlagParseException.php
  33. +59 −40 src/Flag/AbstractFlag.php
  34. +3 −3 src/Flag/Argument.php
  35. +1 −3 src/Flag/Arguments.php
  36. +6 −7 src/Flag/Option.php
  37. +1 −3 src/Flag/Options.php
  38. +34 −68 src/FlagType.php
  39. +118 −5 src/FlagUtil.php
  40. +94 −114 src/Flags.php
  41. +74 −24 src/FlagsParser.php
  42. +7 −1 src/Helper/ValueBinding.php
  43. +7 −1 src/Helper/ValueCollector.php
  44. +87 −72 src/SFlags.php
  45. +9 −2 src/Validator/AbstractValidator.php
  46. +17 −12 src/Validator/CondValidator.php
  47. +9 −2 src/Validator/EmptyValidator.php
  48. +9 −2 src/Validator/EnumValidator.php
  49. +10 −4 src/Validator/FuncValidator.php
  50. +10 −3 src/Validator/LenValidator.php
  51. +9 −2 src/Validator/MultiValidator.php
  52. +8 −1 src/Validator/NameValidator.php
  53. +9 −2 src/Validator/RegexValidator.php
  54. +12 −3 test/BaseFlagsTestCase.php
  55. +58 −0 test/Cases/DemoCmdHandler.php
  56. +11 −4 test/{ → Cases}/RuleParser.php
  57. +78 −0 test/CliAppTest.php
  58. +8 −3 test/Concern/RuleParserTest.php
  59. +7 −0 test/Flag/ArgumentTest.php
  60. +7 −0 test/Flag/OptionTest.php
  61. +7 −0 test/FlagUtilTest.php
  62. +127 −21 test/FlagsParserTest.php
  63. +1 −3 test/FlagsTest.php
  64. +0 −1 test/SFlagsTest.php
  65. +7 −0 test/ValidatorTest.php
49 changes: 29 additions & 20 deletions .github/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
options:
title: '## Change Log'
style: gh-release
title: '## Change Log'
# style allow: simple, markdown(mkdown), ghr(gh-release)
style: gh-release
# group names
names: [Refactor, Fixed, Feature, Update, Other]
#repo_url: https://github.com/gookit/gcli

filters:
# message length >= 12
- name: msgLen
minLen: 12
# message words >= 3
- name: wordsLen
minLen: 3
# message length should >= 12
- name: msg_len
min_len: 12
# message words should >= 3
- name: words_len
min_len: 3
- name: keyword
keyword: format code
exclude: true
- name: keywords
keywords: ['format code']
keywords: format code, action test
exclude: true

# group match rules
# not matched will use 'Other' group.
groups:
- name: New
keywords: [add, new]
rules:
- name: Refactor
start_withs: [refactor, break]
contains: ['refactor:', 'break:']
- name: Fixed
startWiths: [add, new]
keywords: [add, new]
- name: Feat
startWiths: [feat]
keywords: [feature]
start_withs: [fix]
contains: ['fix:']
- name: Feature
start_withs: [feat, new]
contains: ['feat:', 'new:']
- name: Update
startWiths: [update, 'up:']
keywords: [update]
start_withs: [up]
contains: ['update:', 'up:']
25 changes: 5 additions & 20 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -16,23 +16,15 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.3, 7.4, '8.0'] # 7.2,
php: [8.3, 8.1, 8.2]
# os: [ubuntu-latest] # , macOS-latest, windows-latest,
coverage: ['none']
include:
- description: 'Log Code Coverage'
php: '8.0'
coverage: 'xdebug'
# include: # will not testing on php 7.2
# - os: 'ubuntu-latest'
# php: '7.2'
# phpunit: '8.5.13'

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: ~/.composer/cache/files
key: ${{ matrix.php }}
@@ -59,21 +51,14 @@ jobs:
- name: Install dependencies
run: composer update --no-progress

- name: Generate changelog file
id: changelog
if: ${{ matrix.php == '7.4' }}
run: |
wget -c -q https://github.com/inhere/kite/releases/latest/download/kite.phar
php kite.phar git cl last head --style gh-release --no-merges --fetch-tags --unshallow --file example/changelog.md
cat example/changelog.md
# phpunit -v --debug
# phpunit --coverage-clover ./test/clover.info
# phpdbg -dauto_globals_jit=Off-qrr $(which phpunit) --coverage-clover ./test/clover.info
- name: Run test suite
run: |
php example/flags-demo.php -h
php example/sflags-demo.php --help
phpunit --coverage-clover ./test/clover.info
phpunit
# - name: Coveralls parallel
# uses: coverallsapp/github-action@master
45 changes: 10 additions & 35 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -7,59 +7,34 @@ on:

jobs:
release:
name: Test on php ${{ matrix.php}}
name: Tag release
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
fail-fast: true
matrix:
php: [7.4]

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set ENV for github-release
# https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
run: |
echo "RELEASE_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV
echo "RELEASE_NAME=$GITHUB_WORKFLOW" >> $GITHUB_ENV
# usage refer https://github.com/shivammathur/setup-php
- name: Setup PHP
timeout-minutes: 5
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php}}
tools: pecl, php-cs-fixer, phpunit
extensions: mbstring, fileinfo, openssl # , swoole-4.4.19 #optional, setup extensions
ini-values: post_max_size=56M, short_open_tag=On #optional, setup php.ini configuration
coverage: none #optional, setup coverage driver: xdebug, none

- name: Install dependencies # eg: v1.0.3
run: |
echo $RELEASE_TAG
echo $RELEASE_NAME
tag1=${GITHUB_REF#refs/*/}
echo "release tag: ${tag1}"
composer update --no-progress
- name: Generate changelog file
id: changelog
- name: Generate changelog
run: |
wget -c -q https://github.com/inhere/kite/releases/latest/download/kite.phar
php kite.phar git cl prev last --style gh-release --no-merges --fetch-tags --unshallow --file changelog.md
cat changelog.md
curl https://github.com/gookit/gitw/releases/latest/download/chlog-linux-amd64 -L -o /usr/local/bin/chlog
chmod a+x /usr/local/bin/chlog
chlog -c .github/changelog.yml -o changelog.md prev last
# https://github.com/softprops/action-gh-release
- name: Create release and upload assets
uses: softprops/action-gh-release@v1
# if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v2
with:
name: ${{ env.RELEASE_TAG }}
tag_name: ${{ env.RELEASE_TAG }}
body_path: changelog.md
# files: kite-${{ env.RELEASE_TAG }}.phar
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# GITHUB_REPOSITORY: my_gh_org/my_gh_repo
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ vendor/
*.tgz
*.txt
*.cache
*.bak
.phpintel/
.env
.phpstorm.meta.php
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
PhpCsFixer\Finder::create()
->exclude('test')
->exclude('runtime')
->exclude('.github')
->exclude('vendor')
->in(__DIR__)
)
138 changes: 134 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -37,8 +37,15 @@ Generic PHP command line flags parse library
- Support binding named arguemnt
- Support define array argument

### Quick build command

- Use `Toolkit\PFlag\CliCmd` to quickly build a simple command application
- Use `Toolkit\PFlag\CliApp` to quickly build a command application that supports subcommands

## Install

- Require PHP 8.0+

**composer**

```bash
@@ -226,13 +233,136 @@ $arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}

-----------

## Flag Rule
## Build simple cli app

In the pflag, built in `CliApp` and `CliCmd` for quick create and run an simple console application.

### Create simple alone command

Build and run a simple command handler. see example file [example/clicmd.php](example/clicmd.php)

```php
use Toolkit\Cli\Cli;
use Toolkit\PFlag\CliCmd;
use Toolkit\PFlag\FlagsParser;

CliCmd::new()
->config(function (CliCmd $cmd) {
$cmd->name = 'demo';
$cmd->desc = 'description for demo command';

// config flags
$cmd->options = [
'age, a' => 'int;the option age, is int',
'name, n' => 'the option name, is string and required;true',
'tags, t' => 'array;the option tags, is array',
];
// or use property
// $cmd->arguments = [...];
})
->withArguments([
'arg1' => 'this is arg1, is string'
])
->setHandler(function (FlagsParser $fs) {
Cli::info('options:');
vdump($fs->getOpts());
Cli::info('arguments:');
vdump($fs->getArgs());
})
->run();
```

**Usage:**

```php
# show help
php example/clicmd.php -h
# run command
php example/clicmd.php --age 23 --name inhere value1
```

- Display help:

![cmd-demo-help](example/images/cli-cmd-help.png)

- Run command:

![cmd-demo-run](example/images/cli-cmd-run.png)

### Create an multi commands app

Create an multi commands application, run subcommand. see example file [example/cliapp.php](example/cliapp.php)

```php
use Toolkit\Cli\Cli;
use Toolkit\PFlag\CliApp;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlagTest\Cases\DemoCmdHandler;

$app = new CliApp();

$app->add('test1', fn(FlagsParser $fs) => vdump($fs->getOpts()), [
'desc' => 'the test 1 command',
'options' => [
'opt1' => 'opt1 for command test1',
'opt2' => 'int;opt2 for command test1',
],
]);

$app->add('test2', function (FlagsParser $fs) {
Cli::info('options:');
vdump($fs->getOpts());
Cli::info('arguments:');
vdump($fs->getArgs());
}, [
// 'desc' => 'the test2 command',
'options' => [
'opt1' => 'a string opt1 for command test2',
'opt2' => 'int;a int opt2 for command test2',
],
'arguments' => [
'arg1' => 'required arg1 for command test2;true',
]
]);

// fn - required php 7.4+
$app->add('show-err', fn() => throw new RuntimeException('test show exception'));

$app->addHandler(DemoCmdHandler::class);

$app->run();
```

**Usage:**

```php
# show help
php example/cliapp.php -h
# run command
php example/cliapp.php test2 --opt1 val1 --opt2 23 value1
```

- Display commands:

![cli-app-help](example/images/cli-app-help.png)

- Command help:

![cli-app-cmd-help](example/images/cli-app-cmd-help.png)

- Run command:

![cli-app-cmd-run](example/images/cli-app-cmd-run.png)

-----------

## Flag rule

The options/arguments rules. Use rule can quick define an option or argument.

- string value is rule(`type;desc;required;default;shorts`).
- array is define item `SFlags::DEFINE_ITEM`
- supportted type see `FlagType::*`
- supported type see `FlagType::*`

```php
use Toolkit\PFlag\FlagType;
@@ -258,7 +388,7 @@ $rules = [
**For arguments**

- arguemnt no alias/shorts
- argument no alias/shorts
- array value only allow defined at last

**Definition item**
@@ -283,7 +413,7 @@ public const DEFINE_ITEM = [

-----------

## Costom settings
## Custom settings

### Settings for parse

Loading