.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Perinci::CmdLine::Manual 3pm" .TH Perinci::CmdLine::Manual 3pm "2022-10-13" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Perinci::CmdLine::Manual \- Perinci::CmdLine manual .SH "VERSION" .IX Header "VERSION" This document describes version 2.000.0 of Perinci::CmdLine::Manual (from Perl distribution Perinci-CmdLine), released on 2021\-12\-19. .SH "DESCRIPTION" .IX Header "DESCRIPTION" Perinci::CmdLine is a command-line application framework. It parses command-line options and dispatches to one of your specified Perl functions, passing the command-line options and arguments to the function. It accesses functions via Riap protocol (using the Perinci::Access Riap client library) so you can use remote functions transparently. Features: .IP "\(bu" 4 Command-line options parsing .Sp Non-scalar arguments (array, hash, other nested) can also be passed as \s-1JSON\s0 or \&\s-1YAML.\s0 For example, if the \f(CW\*(C`tags\*(C'\fR argument is defined as 'array', then all of below are equivalent: .Sp .Vb 3 \& % mycmd \-\-tags\-yaml \*(Aq[foo, bar, baz]\*(Aq \& % mycmd \-\-tags\-json \*(Aq["foo","bar","baz"]\*(Aq \& % mycmd \-\-tags foo \-\-tags bar \-\-tags baz .Ve .IP "\(bu" 4 Help message (utilizing information from metadata, supports translation) .Sp .Vb 3 \& % mycmd \-\-help \& % mycmd \-h \& % mycmd \-? .Ve .IP "\(bu" 4 Tab completion for various shells (including completion from remote code) .Sp Example for bash: .Sp .Vb 4 \& % complete \-C mycmd mycmd \& % mycmd \-\-he ; # \-\-help \& % mycmd s ; # sub1, sub2, sub3 (if those are the specified subcommands) \& % mycmd sub1 \- ; # list the options available for sub1 subcommand .Ve .IP "\(bu" 4 Undo/redo/history .Sp If the function supports transaction (see Rinci::Transaction, Riap::Transaction) the framework will setup transaction and provide command to do undo (\-\-undo) and redo (\-\-redo) as well as seeing the undo/transaction list (\-\-history) and clearing the list (\-\-clear\-history). .IP "\(bu" 4 Version (\-\-version, \-v) .IP "\(bu" 4 List available subcommands (\-\-subcommands) .IP "\(bu" 4 Configurable output format (\-\-format, \-\-format\-options) .Sp By default \f(CW\*(C`yaml\*(C'\fR, \f(CW\*(C`json\*(C'\fR, \f(CW\*(C`text\*(C'\fR, \f(CW\*(C`text\-simple\*(C'\fR, \f(CW\*(C`text\-pretty\*(C'\fR are recognized. .SH "CONCEPTS" .IX Header "CONCEPTS" Perinci::CmdLine is very function-oriented (and not object-oriented, on purpose). You write your \*(L"business logic\*(R" in a function (of course, you are free to subdivide or delegate to other functions, but there must be one main function for a single-subcommand \s-1CLI\s0 application, or one function for each subcommand in a multiple-subcommand \s-1CLI\s0 application. .PP .Vb 3 \& sub cliapp { \& ... \& } .Ve .PP You annotate the function with Rinci metadata, where you describe what arguments (and command-line aliases, if any) the function (program) accepts, the summary and description of those arguments, and several other aspects as necessary. .PP .Vb 10 \& $SPEC{cliapp} = { \& v => 1.1, \& summary => \*(AqA program to do blah blah\*(Aq, \& args => { \& foo => { \& summary => \*(Aqfoo argument\*(Aq, \& req => 1, \& pos => 0, \& cmdline_aliases => {f=>{}}, \& }, \& bar => { ... }, \& }, \& }; \& sub cliapp { \& ... \& } .Ve .PP Finally, you \*(L"run\*(R" your function: .PP .Vb 2 \& use Perinci::CmdLine::Any; \& Perinci::CmdLine::Any\->new(url => \*(Aq/main/cliapp\*(Aq)\->run; .Ve .PP For a multi-subcommand application: .PP .Vb 8 \& Perinci::CmdLine::Any\->new( \& url => \*(Aq/main/cliapp\*(Aq, \& subcommands => { \& sc1 => { url => \*(Aq/main/do_sc1\*(Aq }, \& sc2 => { url => \*(Aq/main/do_sc2\*(Aq }, \& ... \& }, \& )\->run; .Ve .PP That's it. Command-line option parsing, help message, as well as tab completion will work without extra effort. .PP To run a remote function, you can simply specify a remote \s-1URL,\s0 e.g. \&\f(CW\*(C`http://example.com/api/somefunc\*(C'\fR. All the features like options parsing, help/usage, as well as tab completion will work with remote functions as well. .SH "LOGGING" .IX Header "LOGGING" Logging is done with Log::ger (for producing). For displaying logs, Log::ger::App is used. .PP Initializing logging adds a bit to startup overhead time, so the framework defaults to no logging. To turn on logging from the code, set the \f(CW\*(C`log\*(C'\fR attribute to true when constructing Perinci::CmdLine object. Or, use something like: .PP .Vb 1 \& % PERL5OPT=\-MLog::ger::App TRACE=1 yourcli.pl .Ve .SH "COMMAND-LINE OPTION/ARGUMENT PARSING" .IX Header "COMMAND-LINE OPTION/ARGUMENT PARSING" This section describes how Perinci::CmdLine parses command-line options/arguments into function arguments. Command-line option parsing is implemented by Perinci::Sub::GetArgs::Argv. .PP For boolean function arguments, use \f(CW\*(C`\-\-arg\*(C'\fR to set \f(CW\*(C`arg\*(C'\fR to true (1), and \&\f(CW\*(C`\-\-noarg\*(C'\fR to set \f(CW\*(C`arg\*(C'\fR to false (0). A flag argument (\f(CW\*(C`[bool => {is=>1}]\*(C'\fR) only recognizes \f(CW\*(C`\-\-arg\*(C'\fR and not \f(CW\*(C`\-\-noarg\*(C'\fR. For single letter arguments, only \f(CW\*(C`\-X\*(C'\fR is recognized, not \f(CW\*(C`\-\-X\*(C'\fR nor \f(CW\*(C`\-\-noX\*(C'\fR. .PP For string and number function arguments, use \f(CW\*(C`\-\-arg VALUE\*(C'\fR or \f(CW\*(C`\-\-arg=VALUE\*(C'\fR (or \f(CW\*(C`\-X VALUE\*(C'\fR for single letter arguments) to set argument value. Other scalar arguments use the same way, except that some parsing will be done (e.g. for date type, \-\-arg 1343920342 or \-\-arg '2012\-07\-31' can be used to set a date value, which will be a DateTime object.) (Note that date parsing will be done by Data::Sah and currently not implemented yet.) .PP For arguments with type array of scalar, a series of \f(CW\*(C`\-\-arg VALUE\*(C'\fR is accepted, a la Getopt::Long: .PP .Vb 1 \& \-\-tags tag1 \-\-tags tag2 ; # will result in tags => [\*(Aqtag1\*(Aq, \*(Aqtag2\*(Aq] .Ve .PP For other non-scalar arguments, also use \f(CW\*(C`\-\-arg VALUE\*(C'\fR or \f(CW\*(C`\-\-arg=VALUE\*(C'\fR, but \&\s-1VALUE\s0 will be attempted to be parsed using \s-1JSON,\s0 and then \s-1YAML.\s0 This is convenient for common cases: .PP .Vb 2 \& \-\-aoa \*(Aq[[1],[2],[3]]\*(Aq # parsed as JSON \& \-\-hash \*(Aq{a: 1, b: 2}\*(Aq # parsed as YAML .Ve .PP For explicit \s-1JSON\s0 parsing, all arguments can also be set via \-\-ARG\-json. This can be used to input undefined value in scalars, or setting array value without using repetitive \f(CW\*(C`\-\-arg VALUE\*(C'\fR: .PP .Vb 3 \& \-\-str\-json \*(Aqnull\*(Aq # set undef value \& \-\-ary\-json \*(Aq[1,2,3]\*(Aq # set array value without doing \-\-ary 1 \-\-ary 2 \-\-ary 3 \& \-\-ary\-json \*(Aq[]\*(Aq # set empty array value .Ve .PP Likewise for explicit \s-1YAML\s0 parsing: .PP .Vb 3 \& \-\-str\-yaml \*(Aq~\*(Aq # set undef value \& \-\-ary\-yaml \*(Aq[a, b]\*(Aq # set array value without doing \-\-ary a \-\-ary b \& \-\-ary\-yaml \*(Aq[]\*(Aq # set empty array value .Ve .PP \&\fBSubmetadata\fR. Arguments from submetadata will also be given respective command-line options (and aliases) with prefixed names. For example this function metadata: .PP .Vb 10 \& { \& v => 1.1, \& args => { \& foo => {schema=>\*(Aqstr*\*(Aq}, \& bar => { \& schema => \*(Aqhash*\*(Aq, \& meta => { \& v => 1.1, \& args => { \& baz => {schema=>\*(Aqstr*\*(Aq}, \& qux => { \& schema=>\*(Aqstr*, \& }, \& }, \& }, \& }, \& quux => { \& schema => \*(Aqarray*\*(Aq, \& element_meta => { \& v => 1.1, \& args => { \& corge => {schema=>\*(Aqstr*\*(Aq, cmdline_aliases=>{C=>{}}, \& grault => {schema=>\*(Aqstr*\*(Aq}, \& }, \& }, \& }, \& }, \& } .Ve .PP You can specify on the command-line: .PP .Vb 5 \& % prog \-\-foo val \e \& \-\-bar\-baz val \-\-bar\-qux val \e \& \-\-quux\-corge 11 \e \& \-\-quux\-corge 21 \-\-quux\-grault 22 \e \& \-\-quux\-C 31 .Ve .PP The resulting argument will be: .PP .Vb 12 \& { \& foo => \*(Aqval\*(Aq, \& bar => { \& baz => \*(Aqval\*(Aq, \& qux => \*(Aqval\*(Aq, \& }, \& quux => [ \& {corge=>11}, \& {corge=>21, grault=>22}, \& {corge=>31}, \& ], \& } .Ve .PP For more examples on argument submetadata, see Perinci::Examples::SubMeta. .SH "SHELL COMPLETION" .IX Header "SHELL COMPLETION" The framework can detect when \f(CW\*(C`COMP_LINE\*(C'\fR and \f(CW\*(C`COMP_POINT\*(C'\fR environment variables (set by bash when completing using external command) are set and then answer the completion. In bash, activating tab completion for your script is as easy as (assuming your script is already in \s-1PATH\s0): .PP .Vb 1 \& % complete \-C yourscript yourscript .Ve .PP That is, your script can complete itself (but scripts generated with Perinci::CmdLine::Inline are equipped with companion scripts for completion). The above command can be put in \fI~/.bashrc\fR. But it is recommended that you use shcompgen instead (see below). .PP Tcsh uses \f(CW\*(C`COMMAND_LINE\*(C'\fR instead. The framework can also detect that. .PP For other shells: some shells can emulate bash (like zsh) and for some other (like fish) you need to generate a set of \f(CW\*(C`complete\*(C'\fR commands for each command-line option. .PP \&\f(CW\*(C`shcompgen\*(C'\fR is a \s-1CLI\s0 tool that can detect all scripts in \s-1PATH\s0 if they are using Perinci::CmdLine (as well as a few other frameworks) and generate shell completion scripts for them. It supports several shells. Combined with cpanm-shcompgen, you can install modules and have the shell completion of scripts activated immediately. .SH "PROGRESS INDICATOR" .IX Header "PROGRESS INDICATOR" For functions that express that they do progress updating (by setting their \&\f(CW\*(C`progress\*(C'\fR feature to true), Perinci::CmdLine will setup an output, currently either Progress::Any::Output::TermProgressBar if program runs interactively. .SH "CONFIGURATION FILE" .IX Header "CONFIGURATION FILE" Configuration files are read to preset the value of arguments, before potentially overridden/merged with command-line options. Configuration files are in \s-1IOD\s0 format, which is basically \f(CW\*(C`INI\*(C'\fR with some extra features. .PP By default, configuration files are searched in home directory then \f(CW\*(C`/etc\*(C'\fR, with the name of \fIprogram_name\fR + \f(CW\*(C`.conf\*(C'\fR. If multiple files are found, the contents are merged together. .PP If user wants to use a custom configuration file, she can issue \f(CW\*(C`\-\-config\-path\*(C'\fR command-line option. .PP If user does not want to read configuration file, she can issue \f(CW\*(C`\-\-no\-config\*(C'\fR command-line option. .PP \&\s-1INI\s0 files have the concept of \*(L"sections\*(R". In Perinci::CmdLine, you can use sections to put settings that will only be applied to a certain subcommand, or a certain \*(L"profile\*(R", or other conditions. \*(L"Config profiles\*(R" is a way to specify multiple sets/cases/scenarios in a single configuration file. .PP Example 1 (without any profile or subcommand): .PP .Vb 1 \& ; prog.conf \& \& foo=1 \& bar=2 .Ve .PP When executing program (the comments will show what arguments are set): .PP .Vb 2 \& % prog; # {foo=>1, bar=>2} \& % prog \-\-foo 10; # {foo=>10, bar=>2} .Ve .PP Example 2 (with profiles): .PP .Vb 1 \& ; prog.conf \& \& [profile=profile1] \& foo=1 \& bar=2 \& \& [profile=profile2] \& foo=10 \& bar=20 .Ve .PP When executing program: .PP .Vb 3 \& % prog; # {} \& % prog \-\-config\-profile profile1; # {foo=>1, bar=>2} \& % prog \-\-config\-profile profile2; # {foo=>10, bar=>20} .Ve .PP Example 3 (with subcommands): .PP .Vb 1 \& ; prog.conf \& \& [subcommand=sc1] \& foo=1 \& bar=2 \& \& [subcommand=sc2] \& baz=3 \& qux=4 .Ve .PP When executing program: .PP .Vb 2 \& % prog sc1; # {foo=>1, bar=>2} \& % prog sc2; # {baz=>3, qux=>4} .Ve .PP Example 4 (with subcommands and profiles): .PP .Vb 4 \& ; prog.conf \& [subcommand=sc1 profile=profile1] \& foo=1 \& bar=2 \& \& [profile=profile2 subcommand=sc1] \& foo=10 \& bar=20 .Ve .PP When executing program: .PP .Vb 2 \& % prog sc1 \-\-config\-profile profile1; # {foo=>1, bar=>2} \& % prog sc1 \-\-config\-profile profile2; # {foo=>10, bar=>20} .Ve .SH "HOMEPAGE" .IX Header "HOMEPAGE" Please visit the project's homepage at . .SH "SOURCE" .IX Header "SOURCE" Source repository is at . .SH "SEE ALSO" .IX Header "SEE ALSO" Perinci::CmdLine::Manual::Examples .PP Perinci::CmdLine::Manual::FAQ .PP A list of tutorial posts on my blog, will eventually be moved to \s-1POD:\s0 .SH "AUTHOR" .IX Header "AUTHOR" perlancar .SH "CONTRIBUTING" .IX Header "CONTRIBUTING" To contribute, you can send patches by email/via \s-1RT,\s0 or send pull requests on GitHub. .PP Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via: .PP .Vb 1 \& % prove \-l .Ve .PP 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, and sometimes one or two other Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required beyond that are considered a bug and can be reported to me. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2021, 2018, 2017, 2016, 2015 by perlancar . .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests on the bugtracker website .PP 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.