perlancar > Perinci-Sub-GetArgs-Argv > Perinci::Sub::GetArgs::Argv

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

Dependencies

Annotate this POD

Website

View/Report Bugs
Module Version: 0.57   Source  

NAME ^

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

VERSION ^

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

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.

This routine will produce a Getopt::Long specification from Rinci function metadata, as well as some more data structure in the result metadata to help producing a command-line help/usage message.

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 this 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 automatically recognize --noNAME or --no-NAME in addition to --name. So this function will also check those names for clashes.

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, func.opts, func.common_opts, func.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).

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.

 (any)

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: gen_getopt_long_spec_from_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.

 (any)

Error codes:

* 400 - Error in Getopt::Long option specification, e.g. in common_opts.

* 500 - failure in GetOptions, meaning argv is not valid according to metadata specification (only if 'strict' mode is enabled).

* 501 - coderef in cmdline_aliases got converted into a string, probably because the metadata was transported (e.g. through Riap::HTTP/Riap::Simple).

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/perlancar/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 ^

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2014 by 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.

syntax highlighting: