Skip to content

Commit fb07e90

Browse files
authored
Create cli.php
1 parent 45cecd2 commit fb07e90

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

cli.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
/*
3+
* @Name: php-cli-apps (draft in PHP 7.4)
4+
* @Author: Max Base
5+
* @Repository: https://github.com/BaseMax/php-cli-apps
6+
* @Date: August 31, 2020
7+
*/
8+
$flags=[];
9+
if($argc === 1) {
10+
help();
11+
}
12+
else {
13+
if(startsWith($argv[1], "-")) {// && ($argv[1] === "-h" || $argv[1] === "--help")
14+
help();
15+
}
16+
else {
17+
$command=$argv[1];
18+
callCommand($command);
19+
}
20+
}
21+
22+
function startsWith($haystack, $needle ) {
23+
$length = strlen($needle);
24+
return substr($haystack, 0, $length) === $needle;
25+
}
26+
27+
function endsWith($haystack, $needle) {
28+
$length = strlen( $needle );
29+
if(!$length ) {
30+
return true;
31+
}
32+
return substr($haystack, -$length) === $needle;
33+
}
34+
35+
function help($command="") {
36+
// show special help for commands: if($command == "install") {}
37+
print "Usage:
38+
donya [command]
39+
Available Commands:
40+
help Help about any command
41+
install Installing package(s) in DonyaOS
42+
remove Removing package(s) in DonyaOS
43+
list Listing package(s) in DonyaOS
44+
search Search package(s) in the repository of DonyaOS
45+
Flags:
46+
-h, --help help for donya
47+
Use \"donya [command] --help\" for more information about a command.
48+
";
49+
}
50+
51+
function callCommand($command) {
52+
global $argv;
53+
$args=$argv;
54+
unset($args[0]); // software name. e.g: donya
55+
unset($args[1]); // command name
56+
$args=array_values($args); // start index of items from 0 in Array
57+
switch ($command) {
58+
case "i":
59+
case "install":
60+
commandInstall($args);
61+
break;
62+
case "r":
63+
case "remove":
64+
commandRemove($args);
65+
break;
66+
case "u":
67+
case "update":
68+
commandUpdate($args);
69+
break;
70+
case "l":
71+
case "list":
72+
commandList($args);
73+
break;
74+
case "s":
75+
case "search":
76+
commandSearch($args);
77+
break;
78+
case "h":
79+
case "help":
80+
default:
81+
help($command);
82+
break;
83+
}
84+
}
85+
86+
function commandInstall($args) {
87+
$flags = flags("install", $args);
88+
$args = $flags[1];
89+
$flags = $flags[0];
90+
print_r($args);
91+
print_r($flags);
92+
foreach($args as $arg) {
93+
// Read list of package from `cache` file and detect category/group name of packages!
94+
$group="core"; // WE need to set this from `cache` file
95+
$data = file_get_contents("https://donyaos.github.io/Packages/$group/$arg/package.donya");
96+
if(($data !== null || $data !== "") and strlen($data) > 0) {
97+
print $data;
98+
}
99+
}
100+
}
101+
102+
function commandRemove($args) {
103+
$flags = flags("remove", $args);
104+
$args = $flags[1];
105+
$flags = $flags[0];
106+
print_r($args);
107+
print_r($flags);
108+
}
109+
110+
function commandList($args) {
111+
$flags = flags("list", $args);
112+
$args = $flags[1];
113+
$flags = $flags[0];
114+
print_r($args);
115+
print_r($flags);
116+
}
117+
118+
function commandSearch($args) {
119+
$flags = flags("search", $args);
120+
$args = $flags[1];
121+
$flags = $flags[0];
122+
print_r($args);
123+
print_r($flags);
124+
}
125+
126+
function flags($command, $args) {
127+
$flags=[];
128+
foreach($args as $i=>$arg) {
129+
if(startsWith($arg, "-")) {
130+
if($arg === "-h" || $arg === "--help") {
131+
unset($args[$i]);
132+
help();
133+
}
134+
else if($command == "install" && ($arg == "-r" || $arg == '--rewrite')) {
135+
$flags[]="rewrite";
136+
}
137+
}
138+
}
139+
return [$flags, $args];
140+
}

0 commit comments

Comments
 (0)