Skip to content

Instantly share code, notes, and snippets.

@pretzelhands
Created January 21, 2021 09:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pretzelhands/315d50eb7feb2082f2e1e425a94b5b0c to your computer and use it in GitHub Desktop.
Save pretzelhands/315d50eb7feb2082f2e1e425a94b5b0c to your computer and use it in GitHub Desktop.
A single file CLI app for Composer-based applications
{
"name": "pretzelhands/single-file-cli",
"description": "A single file CLI application",
"type": "cli-app",
"require": {
"symfony/console": "^5.2"
},
"authors": [
{
"name": "Richard Blechinger",
"email": "hello@pretzelhands.com"
}
],
}
#!/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