perlancar

NAME

Perinci::Sub::To::CLIDocData - From Rinci function metadata, generate structure convenient for producing CLI documentation (help/usage/POD)

VERSION

This document describes version 0.28 of Perinci::Sub::To::CLIDocData (from Perl distribution Perinci-Sub-To-CLIDocData), released on 2016-10-27.

SYNOPSIS

 use Perinci::Sub::To::CLIDocData qw(gen_cli_doc_data_from_meta);
 my $clidocdata = gen_cli_doc_data_from_meta(meta => $meta);

Sample function metadata ($meta):

 {
   args => {
     bool1 => {
                cmdline_aliases => { z => { summary => "This is summary for option `-z`" } },
                schema => "bool",
                summary => "Another bool option",
                tags => ["category:cat1"],
              },
     flag1 => {
                cmdline_aliases => { f => {} },
                schema => ["bool", "is", 1],
                tags => ["category:cat1"],
              },
     str1  => {
                pos => 0,
                req => 1,
                schema => "str*",
                summary => "A required option as well as positional argument",
              },
   },
   examples => [
     {
       argv    => ["a value", "--bool1"],
       summary => "Summary for an example",
       test    => 0,
     },
   ],
   summary => "Function summary",
   v => 1.1,
 }

Sample result:

 do {
   my $a = [
     200,
     "OK",
     {
       example_categories => { Examples => { order => 99 } },
       examples => [
         {
           categories   => ["Examples"],
           category     => "Examples",
           cmdline      => "[[prog]] 'a value' --bool1",
           description  => undef,
           example_spec => {
                             argv    => ["a value", "--bool1"],
                             summary => "Summary for an example",
                             test    => 0,
                           },
           summary      => "Summary for an example",
         },
       ],
       option_categories => { "Cat1 options" => { order => 50 }, "Main options" => { order => 0 } },
       opts => {
         "--bool1" => {
           arg         => "bool1",
           arg_spec    => {
                            cmdline_aliases => { z => { summary => "This is summary for option `-z`" } },
                            schema => ["bool", {}, {}],
                            summary => "Another bool option",
                            tags => ["category:cat1"],
                          },
           categories  => ["Cat1 options"],
           category    => "Cat1 options",
           description => undef,
           fqarg       => "bool1",
           opt_parsed  => { opts => ["bool1"] },
           orig_opt    => "bool1",
           summary     => "Another bool option",
           tags        => 'fix',
         },
         "--flag1, -f" => {
           arg         => "flag1",
           arg_spec    => {
                            cmdline_aliases => { f => {} },
                            schema => ["bool", { is => 1 }, {}],
                            tags => ["category:cat1"],
                          },
           categories  => ["Cat1 options"],
           category    => "Cat1 options",
           description => undef,
           fqarg       => "flag1",
           opt_parsed  => { opts => ["flag1"] },
           orig_opt    => "flag1",
           summary     => undef,
           tags        => 'fix',
         },
         "--str1=s*" => {
           arg => "str1",
           arg_spec => {
             pos => 0,
             req => 1,
             schema => ["str", { req => 1 }, {}],
             summary => "A required option as well as positional argument",
           },
           categories => ["Main options"],
           category => "Main options",
           description => undef,
           fqarg => "str1",
           opt_parsed => { desttype => "", opts => ["str1"], type => "s" },
           orig_opt => "str1=s",
           pos => 0,
           req => 1,
           summary => "A required option as well as positional argument",
         },
         "-z" => {
           alias_for   => "bool1",
           alias_spec  => 'fix',
           arg         => "bool1",
           arg_spec    => 'fix',
           categories  => ["Cat1 options"],
           category    => "Cat1 options",
           description => undef,
           fqarg       => "bool1",
           is_alias    => 1,
           main_opt    => "--bool1",
           opt_parsed  => { opts => ["z"] },
           orig_opt    => "z",
           summary     => "This is summary for option `-z`",
           tags        => 'fix',
         },
       },
       usage_line => "[[prog]] [options] <str1>",
     },
   ];
   $a->[2]{opts}{"--bool1"}{tags} = $a->[2]{opts}{"--bool1"}{arg_spec}{tags};
   $a->[2]{opts}{"--flag1, -f"}{tags} = $a->[2]{opts}{"--flag1, -f"}{arg_spec}{tags};
   $a->[2]{opts}{"-z"}{alias_spec} = $a->[2]{opts}{"--bool1"}{arg_spec}{cmdline_aliases}{z};
   $a->[2]{opts}{"-z"}{arg_spec} = $a->[2]{opts}{"--bool1"}{arg_spec};
   $a->[2]{opts}{"-z"}{tags} = $a->[2]{opts}{"--bool1"}{arg_spec}{tags};
   $a;
 }

For a more complete sample, see function metadata for demo_cli_opts in Perinci::Examples::CLI.

FUNCTIONS

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

From Rinci function metadata, generate structure convenient for producing CLI documentation (help/usage/POD).

This function calls Perinci::Sub::GetArgs::Argv's gen_getopt_long_spec_from_meta() (or receive its result as an argument, if passed, to avoid calling the function twice) and post-processes it: produce command usage line, format the options, include information from metadata, group the options by category. It also selects examples in the examples property which are applicable to CLI environment and format them.

The resulting data structure is convenient to use when one wants to produce a documentation for CLI program (including help/usage message and POD).

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • common_opts => hash

    Will be passed to gen_getopt_long_spec_from_meta().

  • ggls_res => array

    Full result from gen_getopt_long_spec_from_meta().

    If you already call Perinci::Sub::GetArgs::Argv's gen_getopt_long_spec_from_meta(), you can pass the full enveloped result here, to avoid calculating twice. What will be useful for the function is the extra result in result metadata (func.* keys in $res->[3] hash).

  • lang => str

  • meta* => hash

  • meta_is_normalized => bool

  • per_arg_json => bool

    Pass per_arg_json=1 to Perinci::Sub::GetArgs::Argv.

  • per_arg_yaml => bool

    Pass per_arg_json=1 to Perinci::Sub::GetArgs::Argv.

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.

Return value: (hash)

HOMEPAGE

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

SOURCE

Source repository is at https://github.com/perlancar/perl-Perinci-Sub-To-CLIOptSpec.

BUGS

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

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.

SEE ALSO

Perinci::CmdLine, Perinci::CmdLine::Lite

Pod::Weaver::Plugin::Rinci

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

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