NAME
Perinci::CmdLine::Manual - Perinci::CmdLine manual
VERSION
This document describes version 0.13 of Perinci::CmdLine::Manual (from Perl distribution Perinci-CmdLine-Lite), released on 2014-08-16.
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:
Command-line options parsing
Non-scalar arguments (array, hash, other nested) can also be passed as JSON or YAML. For example, if the
tags
argument is defined as 'array', then all of below are equivalent:% mycmd --tags-yaml '[foo, bar, baz]' % mycmd --tags-yaml '["foo","bar","baz"]' % mycmd --tags foo --tags bar --tags baz
Help message (utilizing information from metadata, supports translation)
% mycmd --help % mycmd -h % mycmd -?
Tab completion for bash (including completion from remote code)
% complete -C mycmd mycmd % mycmd --he<tab> ; # --help % mycmd s<tab> ; # sub1, sub2, sub3 (if those are the specified subcommands) % mycmd sub1 -<tab> ; # list the options available for sub1 subcommand
Support for other shell might be added in the future upon request.
Undo/redo/history
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).
Version (--version, -v)
List available subcommands (--subcommands)
Configurable output format (--format, --format-options)
By default
yaml
,json
,text
,text-simple
,text-pretty
are recognized.
DISPATCHING
Below is the description of how the framework determines what action and which function to call. (Currently lots of internal attributes are accessed directly, this might be rectified in the future.)
Actions. The _actions
attribute is an array which contains the list of potential actions to choose, in order. It will then be filled out according to the command-line options specified. For example, if --help
is specified, help
action is shifted to the beginning of _actions
. Likewise for --subcommands
, etc. Finally, the call
action (which means an action to call our function) is added to this list. After we are finished filling out the _actions
array, the first action is chosen by running a method called run_<ACTION>
. For example if the chosen action is help
, run_help()
is called. These run_*
methods must execute the action, display the output, and return an exit code. Program will end with this exit code. A run_*
method can choose to decline handling the action by returning undef, in which case the next action will be tried, and so on until a defined exit code is returned.
The 'call' action and determining which subcommand (function) to call. The call
action (implemented by run_call()
) is the one that actually does the real job, calling the function and displaying its result. The _subcommand
attribute stores information on the subcommand to run, including its Riap URL. If there are subcommands, e.g.:
my $cmd = Perinci::CmdLine->new(
subcommands => {
sub1 => {
url => '/MyApp/func1',
},
sub2 => {
url => '/MyApp/func2',
},
},
);
then which subcommand to run is determined by the command-line argument, e.g.:
% myapp sub1 ...
then _subcommand
attribute will contain {url=>'/MyApp/func1'}
. When no subcommand is specified on the command line, run_call()
will decline handling the action and returning undef, and the next action e.g. help
will be executed. But if default_subcommand
attribute is set, run_call()
will run the default subcommand instead.
When there are no subcommands, e.g.:
my $cmd = Perinci::CmdLine->new(url => '/MyApp/func');
_subcommand
will simply contain {url=>'/MyApp/func'}
.
run_call()
will call the function specified in the url
in the _subcommand
using Perinci::Access
. (Actually, run_help()
or run_completion()
can be called instead, depending on which action to run.)
LOGGING [P::C]
Logging is done with Log::Any (for producing) and Log::Any::App (for displaying to outputs). Loading Log::Any::App will add to startup overhead time, so this module tries to be smart when determining whether or not to do logging output (i.e. whether or not to load Log::Any::App). Here are the order of rules being used:
If running shell completion (
COMP_LINE
is defined), output is offNormally, shell completion does not need to show log output.
If LOG environment is defined, use that
You can make a command-line program start a bit faster if you use LOG=0.
If subcommand's log_any_app setting is defined, use that
This allows you, e.g. to turn off logging by default for subcommands that need faster startup time. You can still turn on logging for those subcommands by LOG=1.
If action metadata's default_log setting is defined, use that
For example, actions like
help
,list
, andversion
hasdefault_log
set to 0, for faster startup time. You can still turn on logging for those actions by LOG=1.Use log_any_app attribute setting
UTF8 OUTPUT [P::C]
By default, binmode(STDOUT, ":utf8")
is issued if utf8 output is desired. This is determined by, in order:
Use setting from environment UTF8, if defined.
This allows you to force-disable or force-enable utf8 output.
Use setting from action metadata, if defined.
Some actions like help, list, and version output translated text, so they have their
use_utf8
metadata set to 1.Use setting from subcommand, if defined.
Use setting from
use_utf8
attribute.This attribute comes from SHARYANTO::Role::TermAttrs, its default is determined from UTF8 environment as well as terminal's capabilities.
COLOR THEMES [P::C]
By default colors are used, but if terminal is detected as not having color support, they are turned off. You can also turn off colors by setting COLOR=0 or using PERINCI_CMDLINE_COLOR_THEME=Default::no_color.
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.
For boolean function arguments, use --arg
to set arg
to true (1), and --noarg
to set arg
to false (0). A flag argument ([bool => {is=>1}]
) only recognizes --arg
and not --noarg
. For single letter arguments, only -X
is recognized, not --X
nor --noX
.
For string and number function arguments, use --arg VALUE
or --arg=VALUE
(or -X VALUE
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.)
For arguments with type array of scalar, a series of --arg VALUE
is accepted, a la Getopt::Long:
--tags tag1 --tags tag2 ; # will result in tags => ['tag1', 'tag2']
For other non-scalar arguments, also use --arg VALUE
or --arg=VALUE
, but VALUE will be attempted to be parsed using JSON, and then YAML. This is convenient for common cases:
--aoa '[[1],[2],[3]]' # parsed as JSON
--hash '{a: 1, b: 2}' # parsed as YAML
For explicit JSON 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 --arg VALUE
:
--str-json 'null' # set undef value
--ary-json '[1,2,3]' # set array value without doing --ary 1 --ary 2 --ary 3
--ary-json '[]' # set empty array value
Likewise for explicit YAML parsing:
--str-yaml '~' # set undef value
--ary-yaml '[a, b]' # set array value without doing --ary a --ary b
--ary-yaml '[]' # set empty array value
BASH COMPLETION
To do bash completion, first create your script, e.g. myscript
, that uses Perinci::CmdLine:
#!/usr/bin/perl
use Perinci::CmdLine;
Perinci::CmdLine->new(...)->run;
then execute this in bash
(or put it in bash startup files like /etc/bash.bashrc
or ~/.bashrc
for future sessions):
% complete -C myscript myscript; # myscript must be in PATH
PROGRESS INDICATOR [P::C]
For functions that express that they do progress updating (by setting their progress
feature to true), Perinci::CmdLine will setup an output, currently either Progress::Any::Output::TermProgressBar if program runs interactively, or Progress::Any::Output::LogAny if program doesn't run interactively.
SEE ALSO
Perinci::CmdLine::Manual::Examples
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Perinci-CmdLine-Lite.
SOURCE
Source repository is at https://github.com/sharyanto/perl-Perinci-CmdLine-Lite.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-CmdLine-Lite
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.
AUTHOR
Steven Haryanto <stevenharyanto@gmail.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Steven Haryanto.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.