Skip to content

Commit ecc0803

Browse files
committed
example: add cli_options.php
1 parent 82652ec commit ecc0803

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

examples/cli_options.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
*
5+
* SimpleFramework
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* @author iTX Technologies
13+
* @link https://itxtech.org
14+
*
15+
*/
16+
17+
require_once "../autoload.php";
18+
19+
use iTXTech\SimpleFramework\Console\Logger;
20+
use iTXTech\SimpleFramework\Console\Option\HelpFormatter;
21+
use iTXTech\SimpleFramework\Console\Option\OptionBuilder;
22+
use iTXTech\SimpleFramework\Console\Option\OptionGroup;
23+
use iTXTech\SimpleFramework\Console\Option\Options;
24+
use iTXTech\SimpleFramework\Console\Option\Parser;
25+
26+
Initializer::initTerminal(true);
27+
28+
$options = new Options();
29+
30+
try{
31+
$options->addOption((new OptionBuilder("b"))->desc("This is a long opt")
32+
->longOpt("long-opt")->required()->build());
33+
$options->addOption((new OptionBuilder("a"))->desc("You need to fill the arg")
34+
->required()->hasArg()->argName("something")->build());
35+
36+
$group = new OptionGroup();
37+
$group->addOption((new OptionBuilder("one"))->desc("This is the first opt in OG")
38+
->longOpt("first-opt")->build());
39+
$group->addOption((new OptionBuilder("two"))->desc("This is the second opt in OG")
40+
->longOpt("second-opt")->build());
41+
$group->addOption((new OptionBuilder("three"))->desc("This is the third opt in OG")
42+
->longOpt("third-opt")->build());
43+
$group->setRequired(false);
44+
$options->addOptionGroup($group);
45+
46+
$cmd = (new Parser())->parse($options, $argv);
47+
Logger::info("Got a: " . $cmd->getOptionValue("a"));
48+
}catch(Throwable $e){
49+
//print help
50+
$help = (new HelpFormatter())->generateHelp("cliopts", $options, true);
51+
echo $help;
52+
}

0 commit comments

Comments
 (0)