Created
January 21, 2021 09:06
-
-
Save pretzelhands/315d50eb7feb2082f2e1e425a94b5b0c to your computer and use it in GitHub Desktop.
A single file CLI app for Composer-based applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "pretzelhands/single-file-cli", | |
"description": "A single file CLI application", | |
"type": "cli-app", | |
"require": { | |
"symfony/console": "^5.2" | |
}, | |
"authors": [ | |
{ | |
"name": "Richard Blechinger", | |
"email": "[email protected]" | |
} | |
], | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\SingleCommandApplication; | |
const JOKE_API_ENDPOINT = 'https://official-joke-api.appspot.com/jokes/%s/random'; | |
function command(InputInterface $input, OutputInterface $output) | |
{ | |
$topic = $input->getOption('topic'); | |
$jokeResponse = file_get_contents(sprintf(JOKE_API_ENDPOINT, $topic)); | |
[$joke] = json_decode($jokeResponse); | |
$output->writeln($joke->setup); | |
$output->writeln("<info>{$joke->punchline}</info>\n"); | |
} | |
(new SingleCommandApplication()) | |
->setName('Joke Fetcher') | |
->addOption( | |
name: 'topic', | |
mode: InputOption::VALUE_OPTIONAL, | |
default: 'general' | |
) | |
->setCode('command') | |
->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment