package Perinci::CmdLine::Manual::Examples; # just to make podweaver happy # DATE # VERSION 1; # ABSTRACT: Collection of examples __END__ =pod =encoding UTF-8 =head1 NAME Perinci::CmdLine::Manual::Examples - Collection of examples =head1 VERSION This document describes version 1.811.0 of Perinci::CmdLine::Manual::Examples (from Perl distribution Perinci-CmdLine), released on 2018-03-17. =head1 DESCRIPTION In the examples, L is used to show examples that are applicable to either L or L. For examples that are more appropriate or only applicable to specific implementation, the specific module will be used. Perinci::CmdLine::Classic is hereby referred to as C, while Perinci::CmdLine as C. =head1 BASICS =head2 Simplest application Since Perinci::CmdLine is function- and metadata-based, you need to create at least one function and add some metadata for it. And you'll need to return the result as an enveloped response. The simplest is something like: #!perl use strict; use warnings; use Perinci::CmdLine::Any; our %SPEC; $SPEC{hello} = { v => 1.1, summary => 'Say hello', }; sub hello { [200, "OK", "Hello, world!"]; } Perinci::CmdLine::Any->new(url => '/main/hello')->run; The C attribute specifies the location of the function in URL format (see L for more details on the syntax of URL). It is basically a fully-qualified function name, with C<::> replaced with C. With this URL-based syntax, it is possible to use a remote and/or non-Perl function for the CLI application. The hash in C<$SPEC{hello}> is called a L metadata. The keys are called properties. There are two properties: C (which is always required with the value of 1.1 to specify version) and C (which is actually optional, to describe the function). In this example, the function and its metadata is put inside the same script. You can of course put them in a separate Perl module, and use them with e.g. C<< url => '/Your/Module/func' >>. It is also worth mentioning that if you use the Perinci::CmdLine framework, your functions can also be used directly by other Perl modules/code since they are just regular Perl functions. The function returns a 3-element array containing HTTP-like status code, a status message, and the actual result. If you save the above script as C run it on the command-line: % ./hello Hello, world! Yup, not very interesting. You get help message for free: % ./hello --help % ./hello -h As well as some common options like C<--format> to return the result in a different format: % ./hello --json [200,"OK","Hello, world!"] % ./hello --format perl; # only in PC::Classic, not available in PC::Lite [200, "OK", "Hello, world!"] =head1 FUNCTION ARGUMENTS AND COMMAND-LINE OPTIONS =head2 Basics Function arguments map to command-line options. Example: #!perl use strict; use warnings; use Perinci::CmdLine::Any; our %SPEC; $SPEC{hello} = { v => 1.1, summary => 'Say hello', args => { name => { summary => 'Name to say hello to', }, }, }; sub hello { my %args = @_; [200, "OK", "Hello, $args{name}!"]; } Perinci::CmdLine::Any->new(url => '/main/hello')->run; When you run this: % ./hello --name Jimmy Hello, Jimmy! If you run C<./hello --help>, the option is now mentioned as well in the help message. Unknown arguments will result in an error: % ./hello --gender m ERROR 400: Unknown option '--gender' To specify that an argument is required, add C property to the argument specification with a true value: args => { name => { summary => 'Name to say hello to', req => 1, }, }, So when you run the app: % ./hello ERROR 400: Missing required argument 'name' To specify that an argument can also be specified via positional command-line I instead of just command-line I