mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
739 lines
54 KiB
HTML
739 lines
54 KiB
HTML
<!DOCTYPE html>
|
||
<html class="writer-html5" lang="en" >
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>CLI Library — CodeIgniter 4.4.3 documentation</title>
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/css/citheme_dark.css" type="text/css" />
|
||
<link rel="shortcut icon" href="../_static/favicon.ico"/>
|
||
<!--[if lt IE 9]>
|
||
<script src="../_static/js/html5shiv.min.js"></script>
|
||
<![endif]-->
|
||
|
||
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/js/citheme.js"></script>
|
||
<script src="../_static/js/carbon.js"></script>
|
||
<script src="../_static/js/theme.js"></script>
|
||
<link rel="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
<link rel="next" title="CLIRequest Class" href="cli_request.html" />
|
||
<link rel="prev" title="CLI Generators" href="cli_generators.html" />
|
||
</head>
|
||
|
||
<body class="wy-body-for-nav">
|
||
<div class="wy-grid-for-nav">
|
||
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
||
<div class="wy-side-scroll">
|
||
<div class="wy-side-nav-search" >
|
||
|
||
|
||
|
||
<a href="../index.html">
|
||
|
||
<img src="../_static/ci-logo-text.svg" class="logo" alt="Logo"/>
|
||
</a>
|
||
<div role="search">
|
||
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
|
||
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../license.html">License Agreement</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static Pages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News Section</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create News Items</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working with HTTP Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/controllers.html">Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/views.html">Views</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_cells.html">View Cells</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_renderer.html">View Renderer</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_layouts.html">View Layouts</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_parser.html">View Parser</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_decorators.html">View Decorators</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/table.html">HTML Table Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/response.html">HTTP Responses</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/api_responses.html">API Response Trait</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/localization.html">Localization</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/alternative_php.html">Alternate PHP Syntax for View Files</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working with Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/events.html">Database Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Manipulation with Database Forge</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/db_commands.html">Database Commands</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">Caching Driver</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/cookies.html">Cookies</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/curlrequest.html">CURLRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Service</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html">Working with Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/file_collections.html">File Collections</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/honeypot.html">Honeypot Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/images.html">Image Manipulation Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/publisher.html">Publisher</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/throttler.html">Throttler</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/time.html">Times and Dates</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/uploaded_files.html">Working with Uploaded Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">Working with URIs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/validation.html">Validation</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/response.html">Testing Responses</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/mocking.html">Mocking</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul class="current">
|
||
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Command Line Usage</a><ul class="current">
|
||
<li class="toctree-l2"><a class="reference internal" href="cli_overview.html">CLI Overview</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="cli_controllers.html">Running Controllers via CLI</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="spark_commands.html">Spark Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="cli_commands.html">Creating Spark Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="cli_generators.html">CLI Generators</a></li>
|
||
<li class="toctree-l2 current"><a class="current reference internal" href="#">CLI Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="cli_request.html">CLIRequest Class</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/composer_packages.html">Creating Composer Packages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../libraries/official_packages.html">Official Packages</a></li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
||
<a href="../index.html">CodeIgniter</a>
|
||
</nav>
|
||
|
||
<div class="wy-nav-content">
|
||
<div class="rst-content">
|
||
<a class="btn btn-neutral float-right" href="https://github.com/codeigniter4/CodeIgniter4/edit/develop/user_guide_src/source/cli/cli_library.rst">Edit this page</a>
|
||
|
||
<div role="navigation" aria-label="Page navigation">
|
||
<ul class="wy-breadcrumbs">
|
||
<li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
|
||
<li class="breadcrumb-item"><a href="index.html">Command Line Usage</a></li>
|
||
<li class="breadcrumb-item active">CLI Library</li>
|
||
<li class="wy-breadcrumbs-aside">
|
||
</li>
|
||
</ul>
|
||
<hr/>
|
||
</div>
|
||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
||
<div itemprop="articleBody">
|
||
|
||
<section id="cli-library">
|
||
<h1>CLI Library<a class="headerlink" href="#cli-library" title="Permalink to this headline"></a></h1>
|
||
<p>CodeIgniter’s CLI library makes creating interactive command-line scripts simple, including:</p>
|
||
<ul class="simple">
|
||
<li><p>Prompting the user for more information</p></li>
|
||
<li><p>Writing multi-colored text the terminal</p></li>
|
||
<li><p>Beeping (be nice!)</p></li>
|
||
<li><p>Showing progress bars during long tasks</p></li>
|
||
<li><p>Wrapping long text lines to fit the window.</p></li>
|
||
</ul>
|
||
<div class="contents local topic" id="contents">
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="#initializing-the-class" id="id1">Initializing the Class</a></p></li>
|
||
<li><p><a class="reference internal" href="#getting-input-from-the-user" id="id2">Getting Input from the User</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#prompt" id="id3">prompt()</a></p></li>
|
||
<li><p><a class="reference internal" href="#promptbykey" id="id4">promptByKey()</a></p></li>
|
||
<li><p><a class="reference internal" href="#promptbymultiplekeys" id="id5">promptByMultipleKeys()</a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p><a class="reference internal" href="#providing-feedback" id="id6">Providing Feedback</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#write" id="id7">write()</a></p></li>
|
||
<li><p><a class="reference internal" href="#print" id="id8">print()</a></p></li>
|
||
<li><p><a class="reference internal" href="#color" id="id9">color()</a></p></li>
|
||
<li><p><a class="reference internal" href="#error" id="id10">error()</a></p></li>
|
||
<li><p><a class="reference internal" href="#wrap" id="id11">wrap()</a></p></li>
|
||
<li><p><a class="reference internal" href="#newline" id="id12">newLine()</a></p></li>
|
||
<li><p><a class="reference internal" href="#clearscreen" id="id13">clearScreen()</a></p></li>
|
||
<li><p><a class="reference internal" href="#showprogress" id="id14">showProgress()</a></p></li>
|
||
<li><p><a class="reference internal" href="#table" id="id15">table()</a></p></li>
|
||
<li><p><a class="reference internal" href="#wait" id="id16">wait()</a></p></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
<section id="initializing-the-class">
|
||
<h2><a class="toc-backref" href="#id1">Initializing the Class</a><a class="headerlink" href="#initializing-the-class" title="Permalink to this headline"></a></h2>
|
||
<p>You do not need to create an instance of the CLI library, since all of it’s methods are static. Instead, you simply
|
||
need to ensure your controller can locate it via a <code class="docutils literal notranslate"><span class="pre">use</span></code> statement above your class:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">App\Commands</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\CLI\BaseCommand</span><span class="p">;</span>
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\CLI\CLI</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">MyCommand</span> <span class="k">extends</span> <span class="nx">BaseCommand</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
|
||
<span class="k">public</span> <span class="k">function</span> <span class="nf">run</span><span class="p">(</span><span class="k">array</span> <span class="nv">$params</span><span class="p">)</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The class is automatically initialized when the file is loaded the first time.</p>
|
||
</section>
|
||
<section id="getting-input-from-the-user">
|
||
<h2><a class="toc-backref" href="#id2">Getting Input from the User</a><a class="headerlink" href="#getting-input-from-the-user" title="Permalink to this headline"></a></h2>
|
||
<p>Sometimes you need to ask the user for more information. They might not have provided optional command-line
|
||
arguments, or the script may have encountered an existing file and needs confirmation before overwriting. This is
|
||
handled with the <code class="docutils literal notranslate"><span class="pre">prompt()</span></code> or <code class="docutils literal notranslate"><span class="pre">promptByKey()</span></code> method.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Since v4.3.0, you can write tests for these methods with <code class="docutils literal notranslate"><span class="pre">PhpStreamWrapper</span></code>.
|
||
See <a class="reference internal" href="../testing/overview.html#testing-cli-input"><span class="std std-ref">Testing CLI Input</span></a>.</p>
|
||
</div>
|
||
<section id="prompt">
|
||
<h3><a class="toc-backref" href="#id3">prompt()</a><a class="headerlink" href="#prompt" title="Permalink to this headline"></a></h3>
|
||
<p>You can provide a question by passing it in as the first parameter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$color</span> <span class="o">=</span> <span class="nx">CLI</span><span class="o">::</span><span class="na">prompt</span><span class="p">(</span><span class="s1">'What is your favorite color?'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can provide a default answer that will be used if the user just hits enter by passing the default in the
|
||
second parameter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$color</span> <span class="o">=</span> <span class="nx">CLI</span><span class="o">::</span><span class="na">prompt</span><span class="p">(</span><span class="s1">'What is your favorite color?'</span><span class="p">,</span> <span class="s1">'blue'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can restrict the acceptable answers by passing in an array of allowed answers as the second parameter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$overwrite</span> <span class="o">=</span> <span class="nx">CLI</span><span class="o">::</span><span class="na">prompt</span><span class="p">(</span><span class="s1">'File exists. Overwrite?'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'y'</span><span class="p">,</span> <span class="s1">'n'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Finally, you can pass <a class="reference internal" href="../libraries/validation.html#validation"><span class="std std-ref">validation</span></a> rules to the answer input as the third parameter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$email</span> <span class="o">=</span> <span class="nx">CLI</span><span class="o">::</span><span class="na">prompt</span><span class="p">(</span><span class="s1">'What is your email?'</span><span class="p">,</span> <span class="k">null</span><span class="p">,</span> <span class="s1">'required|valid_email'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Validation rules can also be written in the array syntax:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$email</span> <span class="o">=</span> <span class="nx">CLI</span><span class="o">::</span><span class="na">prompt</span><span class="p">(</span><span class="s1">'What is your email?'</span><span class="p">,</span> <span class="k">null</span><span class="p">,</span> <span class="p">[</span><span class="s1">'required'</span><span class="p">,</span> <span class="s1">'valid_email'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="promptbykey">
|
||
<h3><a class="toc-backref" href="#id4">promptByKey()</a><a class="headerlink" href="#promptbykey" title="Permalink to this headline"></a></h3>
|
||
<p>Predefined answers (options) for prompt sometimes need to be described or are too complex to select via their value.
|
||
<code class="docutils literal notranslate"><span class="pre">promptByKey()</span></code> allows the user to select an option by its key instead of its value:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$fruit</span> <span class="o">=</span> <span class="nx">CLI</span><span class="o">::</span><span class="na">promptByKey</span><span class="p">(</span><span class="s1">'These are your choices:'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'The red apple'</span><span class="p">,</span> <span class="s1">'The plump orange'</span><span class="p">,</span> <span class="s1">'The ripe banana'</span><span class="p">]);</span>
|
||
<span class="cm">/*</span>
|
||
<span class="cm"> * These are your choices:</span>
|
||
<span class="cm"> * [0] The red apple</span>
|
||
<span class="cm"> * [1] The plump orange</span>
|
||
<span class="cm"> * [2] The ripe banana</span>
|
||
<span class="cm"> *</span>
|
||
<span class="cm"> * [0, 1, 2]:</span>
|
||
<span class="cm"> */</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Named keys are also possible:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$fruit</span> <span class="o">=</span> <span class="nx">CLI</span><span class="o">::</span><span class="na">promptByKey</span><span class="p">([</span><span class="s1">'These are your choices:'</span><span class="p">,</span> <span class="s1">'Which would you like?'</span><span class="p">],</span> <span class="p">[</span>
|
||
<span class="s1">'apple'</span> <span class="o">=></span> <span class="s1">'The red apple'</span><span class="p">,</span>
|
||
<span class="s1">'orange'</span> <span class="o">=></span> <span class="s1">'The plump orange'</span><span class="p">,</span>
|
||
<span class="s1">'banana'</span> <span class="o">=></span> <span class="s1">'The ripe banana'</span><span class="p">,</span>
|
||
<span class="p">]);</span>
|
||
<span class="cm">/*</span>
|
||
<span class="cm"> * These are your choices:</span>
|
||
<span class="cm"> * [apple] The red apple</span>
|
||
<span class="cm"> * [orange] The plump orange</span>
|
||
<span class="cm"> * [banana] The ripe banana</span>
|
||
<span class="cm"> *</span>
|
||
<span class="cm"> * Which would you like? [apple, orange, banana]:</span>
|
||
<span class="cm"> */</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Finally, you can pass <a class="reference internal" href="../libraries/validation.html#validation"><span class="std std-ref">validation</span></a> rules to the answer input as the third parameter, the acceptable answers are automatically restricted to the passed options.</p>
|
||
</section>
|
||
<section id="promptbymultiplekeys">
|
||
<span id="prompt-by-multiple-keys"></span><h3><a class="toc-backref" href="#id5">promptByMultipleKeys()</a><a class="headerlink" href="#promptbymultiplekeys" title="Permalink to this headline"></a></h3>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 4.3.0.</span></p>
|
||
</div>
|
||
<p>This method is the same as <code class="docutils literal notranslate"><span class="pre">promptByKey()</span></code>, but it supports multiple value.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$hobbies</span> <span class="o">=</span> <span class="nx">CLI</span><span class="o">::</span><span class="na">promptByMultipleKeys</span><span class="p">(</span><span class="s1">'Select your hobbies:'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'Playing game'</span><span class="p">,</span> <span class="s1">'Sleep'</span><span class="p">,</span> <span class="s1">'Badminton'</span><span class="p">]);</span>
|
||
<span class="cm">/*</span>
|
||
<span class="cm"> * Select your hobbies:</span>
|
||
<span class="cm"> * [0] Playing game</span>
|
||
<span class="cm"> * [1] Sleep</span>
|
||
<span class="cm"> * [2] Badminton</span>
|
||
<span class="cm"> *</span>
|
||
<span class="cm"> * You can specify multiple values separated by commas.</span>
|
||
<span class="cm"> * [0, 1, 2]:</span>
|
||
<span class="cm"> *</span>
|
||
<span class="cm"> * if your answer is '0,2', the return is the key and the value of the options :</span>
|
||
<span class="cm"> * [</span>
|
||
<span class="cm"> * [0] => "Playing game",</span>
|
||
<span class="cm"> * [2] => "Badminton"</span>
|
||
<span class="cm"> * ]</span>
|
||
<span class="cm"> */</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">Important</p>
|
||
<p>The method <code class="docutils literal notranslate"><span class="pre">promptByMultipleKeys()</span></code>, unlike <code class="docutils literal notranslate"><span class="pre">promptByKey()</span></code>, does not support named keys or validation.</p>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="providing-feedback">
|
||
<h2><a class="toc-backref" href="#id6">Providing Feedback</a><a class="headerlink" href="#providing-feedback" title="Permalink to this headline"></a></h2>
|
||
<section id="write">
|
||
<h3><a class="toc-backref" href="#id7">write()</a><a class="headerlink" href="#write" title="Permalink to this headline"></a></h3>
|
||
<p>Several methods are provided for you to provide feedback to your users. This can be as simple as a single status update
|
||
or a complex table of information that wraps to the user’s terminal window. At the core of this is the <code class="docutils literal notranslate"><span class="pre">write()</span></code>
|
||
method which takes the string to output as the first parameter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">write</span><span class="p">(</span><span class="s1">'The rain in Spain falls mainly on the plains.'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can change the color of the text by passing in a color name as the second parameter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">write</span><span class="p">(</span><span class="s1">'File created.'</span><span class="p">,</span> <span class="s1">'green'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This could be used to differentiate messages by status, or create ‘headers’ by using a different color. You can
|
||
even set background colors by passing the color name in as the third parameter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">write</span><span class="p">(</span><span class="s1">'File overwritten.'</span><span class="p">,</span> <span class="s1">'light_red'</span><span class="p">,</span> <span class="s1">'dark_gray'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The following foreground colors are available:</p>
|
||
<ul class="simple">
|
||
<li><p>black</p></li>
|
||
<li><p>dark_gray</p></li>
|
||
<li><p>blue</p></li>
|
||
<li><p>dark_blue</p></li>
|
||
<li><p>light_blue</p></li>
|
||
<li><p>green</p></li>
|
||
<li><p>light_green</p></li>
|
||
<li><p>cyan</p></li>
|
||
<li><p>light_cyan</p></li>
|
||
<li><p>red</p></li>
|
||
<li><p>light_red</p></li>
|
||
<li><p>purple</p></li>
|
||
<li><p>light_purple</p></li>
|
||
<li><p>light_yellow</p></li>
|
||
<li><p>yellow</p></li>
|
||
<li><p>light_gray</p></li>
|
||
<li><p>white</p></li>
|
||
</ul>
|
||
<p>And a smaller number are available as background colors:</p>
|
||
<ul class="simple">
|
||
<li><p>black</p></li>
|
||
<li><p>blue</p></li>
|
||
<li><p>green</p></li>
|
||
<li><p>cyan</p></li>
|
||
<li><p>red</p></li>
|
||
<li><p>yellow</p></li>
|
||
<li><p>light_gray</p></li>
|
||
<li><p>magenta</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="print">
|
||
<h3><a class="toc-backref" href="#id8">print()</a><a class="headerlink" href="#print" title="Permalink to this headline"></a></h3>
|
||
<p>Print functions identically to the <code class="docutils literal notranslate"><span class="pre">write()</span></code> method, except that it does not force a newline either before or after.
|
||
Instead it prints it to the screen wherever the cursor is currently. This allows you to print multiple items all on
|
||
the same line, from different calls. This is especially helpful when you want to show a status, do something, then
|
||
print “Done” on the same line:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">for</span> <span class="p">(</span><span class="nv">$i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nv">$i</span> <span class="o"><=</span> <span class="mi">10</span><span class="p">;</span> <span class="nv">$i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">print</span><span class="p">(</span><span class="nv">$i</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="color">
|
||
<span id="cli-library-color"></span><h3><a class="toc-backref" href="#id9">color()</a><a class="headerlink" href="#color" title="Permalink to this headline"></a></h3>
|
||
<p>While the <code class="docutils literal notranslate"><span class="pre">write()</span></code> command will write a single line to the terminal, ending it with a EOL character, you can
|
||
use the <code class="docutils literal notranslate"><span class="pre">color()</span></code> method to make a string fragment that can be used in the same way, except that it will not force
|
||
an EOL after printing. This allows you to create multiple outputs on the same row. Or, more commonly, you can use
|
||
it inside of a <code class="docutils literal notranslate"><span class="pre">write()</span></code> method to create a string of a different color inside:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">write</span><span class="p">(</span><span class="s2">"fileA </span><span class="se">\t</span><span class="s2">"</span> <span class="o">.</span> <span class="nx">CLI</span><span class="o">::</span><span class="na">color</span><span class="p">(</span><span class="s1">'/path/to/file'</span><span class="p">,</span> <span class="s1">'white'</span><span class="p">),</span> <span class="s1">'yellow'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This example would write a single line to the window, with <code class="docutils literal notranslate"><span class="pre">fileA</span></code> in yellow, followed by a tab, and then
|
||
<code class="docutils literal notranslate"><span class="pre">/path/to/file</span></code> in white text.</p>
|
||
</section>
|
||
<section id="error">
|
||
<h3><a class="toc-backref" href="#id10">error()</a><a class="headerlink" href="#error" title="Permalink to this headline"></a></h3>
|
||
<p>If you need to output errors, you should use the appropriately named <code class="docutils literal notranslate"><span class="pre">error()</span></code> method. This writes light-red text
|
||
to STDERR, instead of STDOUT, like <code class="docutils literal notranslate"><span class="pre">write()</span></code> and <code class="docutils literal notranslate"><span class="pre">color()</span></code> do. This can be useful if you have scripts watching
|
||
for errors so they don’t have to sift through all of the information, only the actual error messages. You use it
|
||
exactly as you would the <code class="docutils literal notranslate"><span class="pre">write()</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">error</span><span class="p">(</span><span class="s1">'Cannot write to file: '</span> <span class="o">.</span> <span class="nv">$file</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="wrap">
|
||
<h3><a class="toc-backref" href="#id11">wrap()</a><a class="headerlink" href="#wrap" title="Permalink to this headline"></a></h3>
|
||
<p>This command will take a string, start printing it on the current line, and wrap it to a set length on new lines.
|
||
This might be useful when displaying a list of options with descriptions that you want to wrap in the current
|
||
window and not go off screen:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">color</span><span class="p">(</span><span class="s2">"task1</span><span class="se">\t</span><span class="s2">"</span><span class="p">,</span> <span class="s1">'yellow'</span><span class="p">);</span>
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">wrap</span><span class="p">(</span><span class="s1">'Some long description goes here that might be longer than the current window.'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>By default, the string will wrap at the terminal width. Windows currently doesn’t provide a way to determine
|
||
the window size, so we default to 80 characters. If you want to restrict the width to something shorter that
|
||
you can be pretty sure fits within the window, pass the maximum line-length as the second parameter. This
|
||
will break the string at the nearest word barrier so that words are not broken.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="c1">// Wrap the text at max 20 characters wide</span>
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">wrap</span><span class="p">(</span><span class="nv">$description</span><span class="p">,</span> <span class="mi">20</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You may find that you want a column on the left of titles, files, or tasks, while you want a column of text
|
||
on the right with their descriptions. By default, this will wrap back to the left edge of the window, which
|
||
doesn’t allow things to line up in columns. In cases like this, you can pass in a number of spaces to pad
|
||
every line after the first line, so that you will have a crisp column edge on the left:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$titles</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'task1a'</span><span class="p">,</span>
|
||
<span class="s1">'task1abc'</span><span class="p">,</span>
|
||
<span class="p">];</span>
|
||
<span class="nv">$descriptions</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'Lorem Ipsum is simply dummy text of the printing and typesetting industry.'</span><span class="p">,</span>
|
||
<span class="s2">"Lorem Ipsum has been the industry's standard dummy text ever since the"</span><span class="p">,</span>
|
||
<span class="p">];</span>
|
||
|
||
<span class="c1">// Determine the maximum length of all titles</span>
|
||
<span class="c1">// to determine the width of the left column</span>
|
||
<span class="nv">$maxlen</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">array_map</span><span class="p">(</span><span class="s1">'strlen'</span><span class="p">,</span> <span class="nv">$titles</span><span class="p">));</span>
|
||
|
||
<span class="k">for</span> <span class="p">(</span><span class="nv">$i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nv">$i</span> <span class="o"><</span> <span class="nb">count</span><span class="p">(</span><span class="nv">$titles</span><span class="p">);</span> <span class="nv">$i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">write</span><span class="p">(</span>
|
||
<span class="c1">// Display the title on the left of the row</span>
|
||
<span class="nb">substr</span><span class="p">(</span>
|
||
<span class="nv">$titles</span><span class="p">[</span><span class="nv">$i</span><span class="p">]</span> <span class="o">.</span> <span class="nb">str_repeat</span><span class="p">(</span><span class="s1">' '</span><span class="p">,</span> <span class="nv">$maxlen</span> <span class="o">+</span> <span class="mi">3</span><span class="p">),</span>
|
||
<span class="mi">0</span><span class="p">,</span>
|
||
<span class="nv">$maxlen</span> <span class="o">+</span> <span class="mi">3</span>
|
||
<span class="p">)</span> <span class="o">.</span>
|
||
<span class="c1">// Wrap the descriptions in a right-hand column</span>
|
||
<span class="c1">// with its left side 3 characters wider than</span>
|
||
<span class="c1">// the longest item on the left.</span>
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">wrap</span><span class="p">(</span><span class="nv">$descriptions</span><span class="p">[</span><span class="nv">$i</span><span class="p">],</span> <span class="mi">40</span><span class="p">,</span> <span class="nv">$maxlen</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span>
|
||
<span class="p">);</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Would create something like this:</p>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>task1a Lorem Ipsum is simply dummy
|
||
text of the printing and
|
||
typesetting industry.
|
||
task1abc Lorem Ipsum has been the
|
||
industry's standard dummy
|
||
text ever since the
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="newline">
|
||
<h3><a class="toc-backref" href="#id12">newLine()</a><a class="headerlink" href="#newline" title="Permalink to this headline"></a></h3>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">newLine()</span></code> method displays a blank line to the user. It does not take any parameters:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">newLine</span><span class="p">();</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="clearscreen">
|
||
<h3><a class="toc-backref" href="#id13">clearScreen()</a><a class="headerlink" href="#clearscreen" title="Permalink to this headline"></a></h3>
|
||
<p>You can clear the current terminal window with the <code class="docutils literal notranslate"><span class="pre">clearScreen()</span></code> method. In most versions of Windows, this will
|
||
simply insert 40 blank lines since Windows doesn’t support this feature. Windows 10 bash integration should change
|
||
this:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">clearScreen</span><span class="p">();</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="showprogress">
|
||
<h3><a class="toc-backref" href="#id14">showProgress()</a><a class="headerlink" href="#showprogress" title="Permalink to this headline"></a></h3>
|
||
<p>If you have a long-running task that you would like to keep the user updated with the progress, you can use the
|
||
<code class="docutils literal notranslate"><span class="pre">showProgress()</span></code> method which displays something like the following:</p>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>[####......] 40% Complete
|
||
</pre></div>
|
||
</div>
|
||
<p>This block is animated in place for a very nice effect.</p>
|
||
<p>To use it, pass in the current step as the first parameter, and the total number of steps as the second parameter.
|
||
The percent complete and the length of the display will be determined based on that number. When you are done,
|
||
pass <code class="docutils literal notranslate"><span class="pre">false</span></code> as the first parameter and the progress bar will be removed.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$totalSteps</span> <span class="o">=</span> <span class="nb">count</span><span class="p">(</span><span class="nv">$tasks</span><span class="p">);</span>
|
||
<span class="nv">$currStep</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
|
||
|
||
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$tasks</span> <span class="k">as</span> <span class="nv">$task</span><span class="p">)</span> <span class="p">{</span>
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">showProgress</span><span class="p">(</span><span class="nv">$currStep</span><span class="o">++</span><span class="p">,</span> <span class="nv">$totalSteps</span><span class="p">);</span>
|
||
<span class="nv">$task</span><span class="o">-></span><span class="na">run</span><span class="p">();</span>
|
||
<span class="p">}</span>
|
||
|
||
<span class="c1">// Done, so erase it...</span>
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">showProgress</span><span class="p">(</span><span class="k">false</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="table">
|
||
<h3><a class="toc-backref" href="#id15">table()</a><a class="headerlink" href="#table" title="Permalink to this headline"></a></h3>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$thead</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'ID'</span><span class="p">,</span> <span class="s1">'Title'</span><span class="p">,</span> <span class="s1">'Updated At'</span><span class="p">,</span> <span class="s1">'Active'</span><span class="p">];</span>
|
||
<span class="nv">$tbody</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="s1">'A great item title'</span><span class="p">,</span> <span class="s1">'2017-11-15 10:35:02'</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span>
|
||
<span class="p">[</span><span class="mi">8</span><span class="p">,</span> <span class="s1">'Another great item title'</span><span class="p">,</span> <span class="s1">'2017-11-16 13:46:54'</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
|
||
<span class="p">];</span>
|
||
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">table</span><span class="p">(</span><span class="nv">$tbody</span><span class="p">,</span> <span class="nv">$thead</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>+----+--------------------------+---------------------+--------+
|
||
| ID | Title | Updated At | Active |
|
||
+----+--------------------------+---------------------+--------+
|
||
| 7 | A great item title | 2017-11-16 10:35:02 | 1 |
|
||
| 8 | Another great item title | 2017-11-16 13:46:54 | 0 |
|
||
+----+--------------------------+---------------------+--------+
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="wait">
|
||
<h3><a class="toc-backref" href="#id16">wait()</a><a class="headerlink" href="#wait" title="Permalink to this headline"></a></h3>
|
||
<p>Waits a certain number of seconds, optionally showing a wait message and
|
||
waiting for a key press.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="c1">// wait for specified interval, with countdown displayed</span>
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">wait</span><span class="p">(</span><span class="nv">$seconds</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>
|
||
|
||
<span class="c1">// show continuation message and wait for input</span>
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">wait</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="k">false</span><span class="p">);</span>
|
||
|
||
<span class="c1">// wait for specified interval</span>
|
||
<span class="nx">CLI</span><span class="o">::</span><span class="na">wait</span><span class="p">(</span><span class="nv">$seconds</span><span class="p">,</span> <span class="k">false</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
|
||
<a href="cli_generators.html" class="btn btn-neutral float-left" title="CLI Generators" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
|
||
<a href="cli_request.html" class="btn btn-neutral float-right" title="CLIRequest Class" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
|
||
</div>
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>© Copyright 2019-2023 CodeIgniter Foundation.
|
||
<span class="lastupdated">Last updated on Nov 05, 2023.
|
||
</span></p>
|
||
</div>
|
||
|
||
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
|
||
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
|
||
provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
||
|
||
|
||
</footer>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
<script>
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(false);
|
||
});
|
||
</script>
|
||
|
||
</body>
|
||
</html> |