Skip to content

Commit cce580c

Browse files
authored
Merge pull request #34 from WebFiori/dev
docs: Updated Documentation and Samples
2 parents ca2a9b7 + 0101875 commit cce580c

File tree

43 files changed

+3967
-2296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3967
-2296
lines changed

README.md

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Class library that can help in writing command line based applications with mini
33

44
<p align="center">
55
<a target="_blank" href="https://github.com/WebFiori/cli/actions/workflows/php84.yaml">
6-
<img src="https://github.com/WebFiori/cli/actions/workflows/php83.yaml/badge.svg?branch=main">
6+
<img src="https://github.com/WebFiori/cli/actions/workflows/php84.yaml/badge.svg?branch=main">
77
</a>
88
<a href="https://codecov.io/gh/WebFiori/cli">
99
<img src="https://codecov.io/gh/WebFiori/cli/branch/main/graph/badge.svg" />
@@ -104,13 +104,13 @@ exit(\$runner->start());
104104
A complete sample application with multiple examples can be found here: **[📁 View Sample Application](https://github.com/WebFiori/cli/tree/main/examples)**
105105

106106
The sample application includes:
107-
- **[Basic Commands](https://github.com/WebFiori/cli/tree/main/examples/01-basic-command)** - Simple command creation
108-
- **[Arguments Handling](https://github.com/WebFiori/cli/tree/main/examples/02-command-with-args)** - Working with command arguments
109-
- **[Interactive Mode](https://github.com/WebFiori/cli/tree/main/examples/03-interactive-mode)** - Building interactive applications
107+
- **[Basic Commands](https://github.com/WebFiori/cli/tree/main/examples/01-basic-hello-world)** - Simple command creation
108+
- **[Arguments Handling](https://github.com/WebFiori/cli/tree/main/examples/02-arguments-and-options)** - Working with command arguments
109+
- **[User Input](https://github.com/WebFiori/cli/tree/main/examples/03-user-input)** - Building interactive applications
110110
- **[Multi-Command Apps](https://github.com/WebFiori/cli/tree/main/examples/10-multi-command-app)** - Complex applications with multiple commands
111-
- **[Progress Bars](https://github.com/WebFiori/cli/tree/main/examples/05-progress-bars)** - Visual progress indicators
111+
- **[Progress Bars](https://github.com/WebFiori/cli/tree/main/examples/07-progress-bars)** - Visual progress indicators
112112
- **[Table Display](https://github.com/WebFiori/cli/tree/main/examples/06-table-display)** - Formatting data in tables
113-
- **[Testing Examples](https://github.com/WebFiori/cli/tree/main/examples/tests)** - Unit testing your commands
113+
- **[Database Operations](https://github.com/WebFiori/cli/tree/main/examples/09-database-ops)** - Database CLI commands
114114

115115
## Installation
116116

@@ -165,7 +165,7 @@ php app.php greet
165165
# Output: Hello from WebFiori CLI!
166166
```
167167

168-
**[📖 View Complete Example](https://github.com/WebFiori/cli/tree/main/examples/01-basic-command)**
168+
**[📖 View Complete Example](https://github.com/WebFiori/cli/tree/main/examples/01-basic-hello-world)**
169169

170170
### Command with Arguments
171171

@@ -210,7 +210,7 @@ php app.php greet-person --name=Sarah
210210
# Output: Hello Friend Sarah!
211211
```
212212

213-
**[📖 View Complete Example](https://github.com/WebFiori/cli/tree/main/examples/02-command-with-args)**
213+
**[📖 View Complete Example](https://github.com/WebFiori/cli/tree/main/examples/02-arguments-and-options)**
214214

215215
### Multi-Command Application
216216

@@ -394,7 +394,7 @@ This will show following output in terminal:
394394
>>
395395
```
396396

397-
**[📖 View Interactive Mode Example](https://github.com/WebFiori/cli/tree/main/examples/03-interactive-mode)**
397+
**[📖 View Interactive Mode Example](https://github.com/WebFiori/cli/tree/main/examples/05-interactive-commands)**
398398

399399
### Input and Output Streams
400400

@@ -411,7 +411,7 @@ $command->setInputStream(new FileInputStream('input.txt'));
411411
$command->setOutputStream(new FileOutputStream('output.txt'));
412412
```
413413

414-
**[📖 View Streams Example](https://github.com/WebFiori/cli/tree/main/examples/04-custom-streams)**
414+
**[📖 View Streams Example](https://github.com/WebFiori/cli/tree/main/examples/08-file-processing)**
415415

416416
### ANSI Colors and Formatting
417417

@@ -427,7 +427,7 @@ public function exec(): int {
427427
}
428428
```
429429

430-
**[📖 View Formatting Example](https://github.com/WebFiori/cli/tree/main/examples/07-ansi-formatting)**
430+
**[📖 View Formatting Example](https://github.com/WebFiori/cli/tree/main/examples/04-output-formatting)**
431431

432432
### Progress Bars
433433

@@ -449,7 +449,7 @@ public function exec(): int {
449449
}
450450
```
451451

452-
**[📖 View Progress Bar Example](https://github.com/WebFiori/cli/tree/main/examples/05-progress-bars)**
452+
**[📖 View Progress Bar Example](https://github.com/WebFiori/cli/tree/main/examples/07-progress-bars)**
453453

454454
### Table Display
455455

@@ -458,8 +458,8 @@ Display data in formatted tables:
458458
```php
459459
public function exec(): int {
460460
$data = [
461-
['John Doe', 30, 'New York'],
462-
['Jane Smith', 25, 'Los Angeles']
461+
['Ahmed Hassan', 30, 'Cairo'],
462+
['Sarah Johnson', 25, 'Los Angeles']
463463
];
464464
$headers = ['Name', 'Age', 'City'];
465465

@@ -593,34 +593,33 @@ class HelloCommandTest extends CommandTestCase {
593593

594594
```
595595

596-
**[📖 View Testing Examples](https://github.com/WebFiori/cli/tree/main/examples/tests)**
596+
**[📖 View Testing Examples](https://github.com/WebFiori/cli/tree/main/examples/10-multi-command-app)**
597597

598598
## Examples
599599

600600
Explore comprehensive examples to learn different aspects of WebFiori CLI:
601601

602602
### Basic Examples
603-
- **[📁 Basic Command](https://github.com/WebFiori/cli/tree/main/examples/01-basic-command)** - Create your first CLI command
604-
- **[📁 Command with Arguments](https://github.com/WebFiori/cli/tree/main/examples/02-command-with-args)** - Handle command-line arguments
605-
- **[📁 Interactive Mode](https://github.com/WebFiori/cli/tree/main/examples/03-interactive-mode)** - Build interactive CLI applications
603+
- **[📁 Basic Command](https://github.com/WebFiori/cli/tree/main/examples/01-basic-hello-world)** - Create your first CLI command
604+
- **[📁 Command with Arguments](https://github.com/WebFiori/cli/tree/main/examples/02-arguments-and-options)** - Handle command-line arguments
605+
- **[📁 User Input](https://github.com/WebFiori/cli/tree/main/examples/03-user-input)** - Read and validate user input
606+
- **[📁 Output Formatting](https://github.com/WebFiori/cli/tree/main/examples/04-output-formatting)** - Colors and text formatting
606607

607608
### Advanced Examples
608-
- **[📁 Custom Streams](https://github.com/WebFiori/cli/tree/main/examples/04-custom-streams)** - Custom input/output handling
609-
- **[📁 Progress Bars](https://github.com/WebFiori/cli/tree/main/examples/05-progress-bars)** - Visual progress indicators
609+
- **[📁 Interactive Commands](https://github.com/WebFiori/cli/tree/main/examples/05-interactive-commands)** - Build interactive CLI applications
610610
- **[📁 Table Display](https://github.com/WebFiori/cli/tree/main/examples/06-table-display)** - Format data in tables
611-
- **[📁 ANSI Formatting](https://github.com/WebFiori/cli/tree/main/examples/07-ansi-formatting)** - Colors and text formatting
611+
- **[📁 Progress Bars](https://github.com/WebFiori/cli/tree/main/examples/07-progress-bars)** - Visual progress indicators
612612
- **[📁 File Processing](https://github.com/WebFiori/cli/tree/main/examples/08-file-processing)** - File manipulation commands
613613
- **[📁 Database Operations](https://github.com/WebFiori/cli/tree/main/examples/09-database-ops)** - Database CLI commands
614614

615615
### Complete Applications
616616
- **[📁 Multi-Command Application](https://github.com/WebFiori/cli/tree/main/examples/10-multi-command-app)** - Full-featured CLI application
617-
- **[📁 Testing Suite](https://github.com/WebFiori/cli/tree/main/examples/tests)** - Unit testing examples
618617

619618
### Quick Links
620619
- **[📖 All Examples](https://github.com/WebFiori/cli/tree/main/examples)** - Browse all available examples
621-
- **[🧪 Test Examples](https://github.com/WebFiori/cli/tree/main/examples/tests/HelloCommandTest.php)** - See how to test your commands
622-
- **[🚀 Sample App](https://github.com/WebFiori/cli/tree/main/examples/10-multi-command-app/app.php)** - Ready-to-run sample application
620+
- **[🚀 Sample App](https://github.com/WebFiori/cli/tree/main/examples/10-multi-command-app/main.php)** - Ready-to-run sample application
621+
623622

624623
---
625624

626-
**Ready to build amazing CLI applications? Start with the [📁 Basic Command Example](https://github.com/WebFiori/cli/tree/main/examples/01-basic-command) and work your way up!**
625+
**Ready to build amazing CLI applications? Start with the [📁 Basic Command Example](https://github.com/WebFiori/cli/tree/main/examples/01-basic-hello-world) and work your way up!**

WebFiori/Cli/Command.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,24 @@ public function getInput(string $prompt, ?string $default = null, ?InputValidato
588588
public function getInputStream() : InputStream {
589589
return $this->inputStream;
590590
}
591+
592+
/**
593+
* Check if the current input stream supports interactive input.
594+
*
595+
* @return bool True if the input stream supports interactive input (real-time user interaction),
596+
* false otherwise (files, pipes, arrays, etc.)
597+
*/
598+
public function supportsInteractiveInput(): bool {
599+
$stream = $this->getInputStream();
600+
601+
// Only StdIn with tty supports true interaction
602+
if ($stream instanceof Streams\StdIn) {
603+
return function_exists('posix_isatty') && posix_isatty(STDIN);
604+
}
605+
606+
// All other stream types are non-interactive
607+
return false;
608+
}
591609
/**
592610
* Returns the name of the command.
593611
*
@@ -1017,12 +1035,18 @@ public function removeArgument(string $name) : bool {
10171035
* @param int $defaultIndex The index of the default value in case no value
10181036
* is selected and the user hit enter.
10191037
*
1038+
* * @param int $maxTrials The maximum number of trials the user can do to select
1039+
* a value. If -1 is passed, the user can select a value forever.
1040+
*
10201041
* @return string|null The method will return the value which is selected by
1021-
* the user. If choices array is empty, null is returned.
1042+
* the user. If choices array is empty, null is returned. Also, null is returned
1043+
* if max trials is reached and it is not -1.
1044+
*
10221045
*
10231046
*/
1024-
public function select(string $prompt, array $choices, int $defaultIndex = -1): ?string {
1047+
public function select(string $prompt, array $choices, int $defaultIndex = -1, int $maxTrials = -1): ?string {
10251048
if (count($choices) != 0) {
1049+
$currentTry = 0;
10261050
do {
10271051
$this->println($prompt, [
10281052
'color' => 'gray',
@@ -1037,6 +1061,11 @@ public function select(string $prompt, array $choices, int $defaultIndex = -1):
10371061
if ($check !== null) {
10381062
return $check;
10391063
}
1064+
$currentTry++;
1065+
1066+
if ($currentTry == $maxTrials && $maxTrials > 0) {
1067+
return null;
1068+
}
10401069
} while (true);
10411070
}
10421071

WebFiori/Cli/KeysMap.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ class KeysMap {
5353
*
5454
* </ul>
5555
*/
56-
public static function map(string $ch) : string {
56+
public static function map($ch) : string {
57+
// Handle end-of-stream case for any input type
58+
if ($ch === false || $ch === null) {
59+
return "\n"; // Treat as Enter key
60+
}
61+
62+
// Ensure we have a string
63+
if (!is_string($ch)) {
64+
$ch = (string)$ch;
65+
}
66+
5767
$keyMap = self::KEY_MAP;
5868

5969
if (isset($keyMap[$ch])) {

examples/01-basic-hello-world/HelloCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function exec(): int {
4848
// Special greeting for WebFiori
4949
if (strtolower($name) === 'webfiori') {
5050
$this->success("🎉 Hello, $name! Welcome to the CLI world!");
51-
$this->info('You\'re using the WebFiori CLI library - great choice!');
51+
$this->info('You\'re using the WebFiori CLI library.');
5252
} else {
5353
// Standard greeting
5454
$this->println("Hello, $name! 👋");

0 commit comments

Comments
 (0)