Steven Haryanto > Perinci-Sub-GetArgs-Argv > Perinci::Sub::GetArgs::Argv

Download:
Perinci-Sub-GetArgs-Argv-0.37.tar.gz

Dependencies

Annotate this POD

Website

View/Report Bugs
Module Version: 0.37   Source  

NAME ^

Perinci::Sub::GetArgs::Argv - Get subroutine arguments from command line arguments (@ARGV)

VERSION ^

This document describes version 0.37 of Perinci::Sub::GetArgs::Argv (from Perl distribution Perinci-Sub-GetArgs-Argv), released on 2014-07-10.

SYNOPSIS ^

 use Perinci::Sub::GetArgs::Argv;

 my $res = get_args_from_argv(argv=>\@ARGV, meta=>$meta, ...);

DESCRIPTION ^

This module provides get_args_from_argv(), which parses command line arguments (@ARGV) into subroutine arguments (%args). This module is used by Perinci::CmdLine. For explanation on how command-line options are processed, see Perinci::CmdLine's documentation.

This module uses Log::Any for logging framework.

This module has Rinci metadata.

FUNCTIONS ^

gen_getopt_long_spec_from_meta(%args) -> [status, msg, result, meta]

Generate Getopt::Long spec from Rinci function metadata.

Function arguments will be mapped to command-line options with the same name, with non-alphanumeric characters changed to - (- is preferred over _ because it lets user avoid pressing Shift on popular keyboards). For example: file_size becomes file-size, file_size.max becomes file-size-max. If function argument option name clashes with command-line option or another existing option, it will be renamed to NAME-arg (or NAME-arg2 and so on). For example: help will become help-arg (if common_opts contains help, that is).

Each command-line alias (cmdline_aliases property) in the argument specification will also be added as command-line option, except if it clashes with an existing option, in which case the function will warn and skip adding the alias. For more information about cmdline_aliases, see Rinci::function.

For arguments with type of bool, Getopt::Long will by default also add --noNAME in addition to --name.

For arguments with type array of simple scalar, --NAME can be specified more than once to append to the array.

If per_arg_json setting is active, and argument's schema is not a "required simple scalar" (e.g. an array, or a nullable string), then --NAME-json will also be added to let users input undef (through --NAME-json null) or a non-scalar value (e.g. --NAME-json '[1,2,3]'). If this name conflicts with another existing option, a warning will be displayed and the option will not be added.

If per_arg_yaml setting is active, and argument's schema is not a "required simple scalar" (e.g. an array, or a nullable string), then --NAME-yaml will also be added to let users input undef (through --NAME-yaml '~') or a non-scalar value (e.g. --NAME-yaml '[foo, bar]'). If this name conflicts with another existing option, a warning will be displayed and the option will not be added. YAML can express a larger set of values, e.g. binary data, circular references, etc.

Will produce a hash (Getopt::Long spec), with func.specmeta and func.opts that contain extra information (func.specmeta is a hash of getopt spec name and a hash of extra information while func.opts lists all used option names). For example this is a complete response:

    [200, "OK",
     # Getopt::Long spec
     {"help|h"     => sub { ... }, # this is simply taken from 'common_opts'
      "version"    => sub { ... }, # ditto
      "str-arg=s"  => sub { ... }, # from arg 'str_arg'
      "ary-arg=s"  => sub { ... }, # from arg 'ary_arg'
      "ary-arg-json=s" => sub { ... },
      "ary-arg-yaml=s" => sub { ... }},
     # result metadata
     {
       # extra information
       "func.specmeta" => {
           "help|h"    => {arg=>undef},
           "version"   => {arg=>undef},
           "str-arg=s" => {arg=>'str_arg'},
           "ary-arg=s" => {arg=>'ary_arg'},
           "ary-arg-json=s" => {arg=>'ary_arg', is_json=>1},
           "ary-arg-yaml=s" => {arg=>'ary_arg', is_yaml=>1},
       },
       "func.opts" => ['help','h','version','str-arg','ary-arg','ary-arg-json','ary-arg-yaml'],
     }]

Arguments ('*' denotes required arguments):

Return value:

Returns an enveloped result (an array).

First element (status) is an integer containing HTTP status code (200 means OK, 4xx caller error, 5xx function error). Second element (msg) is a string containing error message, or 'OK' if status is 200. Third element (result) is optional, the actual result. Fourth element (meta) is called result metadata and is optional, a hash that contains extra information.

get_args_from_argv(%args) -> [status, msg, result, meta]

Get subroutine arguments (%args) from command-line arguments (@ARGV).

Using information in Rinci function metadata's args property, parse command line arguments @argv into hash %args, suitable for passing into subroutines.

Currently uses Getopt::Long's GetOptions to do the parsing.

As with GetOptions, this function modifies its argv argument, so you might want to copy the original argv first (or pass a copy instead) if you want to preserve the original.

See also: gengetoptlongspecfrom_meta() which is the routine that generates the specification.

Arguments ('*' denotes required arguments):

Return value:

Returns an enveloped result (an array).

First element (status) is an integer containing HTTP status code (200 means OK, 4xx caller error, 5xx function error). Second element (msg) is a string containing error message, or 'OK' if status is 200. Third element (result) is optional, the actual result. Fourth element (meta) is called result metadata and is optional, a hash that contains extra information.

FAQ ^

TODO ^

Option to enable json/yaml for nullable simple scalar (to enable --str-json '~').

SEE ALSO ^

Perinci

HOMEPAGE ^

Please visit the project's homepage at https://metacpan.org/release/Perinci-Sub-GetArgs-Argv.

SOURCE ^

Source repository is at https://github.com/sharyanto/perl-Perinci-Sub-GetArgs-Argv.

BUGS ^

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Sub-GetArgs-Argv

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.

syntax highlighting: