NAME
Perinci::Examples::CLI - Example for CLI help/usage generation
VERSION
This document describes version 0.824 of Perinci::Examples::CLI (from Perl distribution Perinci-Examples), released on 2023-07-09.
DESCRIPTION
The example(s) in this package are for demonstrating how the function metadata is converted into CLI help example (produced by --help
or --help --verbose
). Also for testing how it is converted into POD for scripts (i.e. the OPTIONS
section).
FUNCTIONS
demo_cli_opts
Usage:
demo_cli_opts(%args) -> [$status_code, $reason, $payload, \%result_meta]
Summary for `demo_cli_opts`.
Examples:
Summary for an example:
demo_cli_opts(str1 => "a value", array1 => ["elem1", "elem2"], int1 => 10);
Result:
[ 200, "OK", { array1 => ["elem1", "elem2"], full => 1, full2 => 1, hash1 => { default => 1 }, int1 => 10, int2 => 10, str1 => "a value", }, {}, ]
A second example:
demo_cli_opts(str1 => "x", array1 => [1, 2], int1 => 20);
Result:
[ 400, "Can't parse argv", undef, { logs => [ { file => "/home/u1/perl5/perlbrew/perls/perl-5.38.0/lib/site_perl/5.38.0/Perinci/Access/Schemeless.pm", func => "Perinci::Access::Schemeless::action_call", line => 500, package => "Perinci::Access::Schemeless", time => 1688898612, type => "create", }, ], prev => [500, "GetOptions failed"], }, ]
Description for demo_cli_opts
.
This is another paragraph from the description. Description by default is assumed to be marked up in Markdown (currently referring to CommonMark).
This paragraph should be set in verbatim.
This function is not exported.
Arguments ('*' denotes required arguments):
are_baz => bool
This demonstrate negation of --are-foo to --arent-foo.
array1* => array[str]
Positional, slurpy, and plural.
Argument with non-scalar types (like array or hash) can be specified in the CLI using
--ARG-json
or--ARG-yaml
. Arguments with type of array of string can also be specified using multiple--ARG
options.This option also links to another option.
bool1 => bool
A bool option.
CLI framework should generate
--nobool1
(and--nobool1
) automatically.This option has an alias,
-z
. Because the alias has its own summary/description, it will be displayed separately.flag1 => bool
A flag option.
A flag option is a bool option with the value of 1 (true). It is meant to activate something if specified and there is no notion of disabling by specifying the opposite. Thus the CLI framework should not generate a
--noflag1
option.This flag has an alias
-f
with no summary/description nor code. So the CLI framework should display the alias along with the option. Note that short (single-letter) options/aliases do not get--noX
.full => bool (default: 1)
Turn on full processing.
Another bool option with on default.
CLI framework should perhaps show
--nobool2
instead of--bool2
because--bool2
is active by default. And if it does so, it should also show the negative summary in thesummary.alt.bool.not
attribute instead of the normalsummary
property.full2 => bool (default: 1)
Use full processing (2).
Another bool option with on default. Because this option does not have
summary.alt.bool.not
, CLI framework should not show any summary, despite the existence ofsummary
.gender => str
A string option.
This option contains flag aliases that have code.
hash1 => hash (default: {default=>1})
Demonstrate hash argument with default value from schema.
input => buf
Specify input.
This option demonstrates the
cmdline_src
property. Also, since schema type isbuf
, we know that the value is binary data. CLI framework will provide--input-base64
to allow specifying binary data encoded in base64.int1* => int
Demonstrate an option with no summary. And a required option.
int2 => int (default: 10)
Another int option.
Demonstrate a scalar/simple default value.
is_bar => bool
This demonstrate negation of --is-foo to --isnt-foo.
output => filename
Specify output filename.
This option demonstrates how the option would be displayed in the help/usage. Due to the
schema
beingfilename
instead of juststr
, CLI framework can show:--output=filename
instead of the less informative:
--output=str
pass => str
(No description)
str1* => str
A required option as well as positional argument.
with_foo => bool
This demonstrate negation of --with-foo to --without-foo.
Returns an enveloped result (an array).
First element ($status_code) is an integer containing HTTP-like status code (200 means OK, 4xx caller error, 5xx function error). Second element ($reason) is a string containing error message, or something like "OK" if status is 200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth element (%result_meta) is called result metadata and is optional, a hash that contains extra information, much like how HTTP response headers provide additional metadata.
Return value: (any)
demo_cli_opts_shorter
Usage:
demo_cli_opts_shorter(%args) -> [$status_code, $reason, $payload, \%result_meta]
Function summary.
Examples:
Summary for an example:
demo_cli_opts_shorter(str1 => "a value", bool1 => 1); # -> [200, "OK", { bool1 => 1, str1 => "a value" }, {}]
This function is not exported.
Arguments ('*' denotes required arguments):
bool1 => bool
Another bool option.
flag1 => bool
(No description)
str1* => str
A required option as well as positional argument.
Returns an enveloped result (an array).
First element ($status_code) is an integer containing HTTP-like status code (200 means OK, 4xx caller error, 5xx function error). Second element ($reason) is a string containing error message, or something like "OK" if status is 200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth element (%result_meta) is called result metadata and is optional, a hash that contains extra information, much like how HTTP response headers provide additional metadata.
Return value: (any)
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Perinci-Examples.
SOURCE
Source repository is at https://github.com/perlancar/perl-Perinci-Examples.
SEE ALSO
AUTHOR
perlancar <perlancar@cpan.org>
CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
COPYRIGHT AND LICENSE
This software is copyright (c) 2023, 2022, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Examples
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.