Getopt-Long-Subcommand

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


    Return hash structure, with these keys: "success" (bool, false if
    parsing options failed e.g. unknown option/subcommand, illegal option
    value, etc), "subcommand" (array of str, subcommand name, if there is
    any; nested subcommands will be listed in order, e.g. "["sub1",
    "subsub1"]").

    Arguments:

    *   summary => str

        Used by autohelp (not yet implemented).

    *   options => hash

        A hash of option names and its specification. The specification is
        the same as what you would feed to Getopt::Long's "GetOptions".

    *   subcommands => hash

        A hash of subcommand name and its specification. The specification
        looks like "GetOptions" argument, with keys like "summary",
        "options", "subcommands" (for nested subcommands).

    *   default_subcommand => str

        Default subcommand to use if no subcommand name is set. Subcommand
        can be set using the first argument, or your option handler can also
        set the subcommand using:

         $_[2]{subcommand_name} = 'something';

    *   configure => arrayref

        Custom Getopt::Long configuration. The default is:

         ['no_ignore_case', 'no_getopt_compat', 'gnu_compat', 'bundling']

        Note that even though you use custom configuration here, the tab
        completion (performed by Complete::Getopt::Long only supports
        "no_ignore_case", "gnu_compat", and "no_getopt_compat".

    Differences with "Getopt::Long"'s "GetOptions":

    *   Accept a command/subcommand specification (%cmdspec) instead of just
        options specification (%ospec) like in "Getopt::Long").

    *   This module's function returns hash instead of bool.

    *   Coderefs in "options" will receive an extra argument $res which is
        the result hash (being built). So the arguments that the coderefs
        get is:

         ($callback, $value, $res)

FAQ
  How to avoid modifying @ARGV? How to process from another array, like Getopt::Long's GetOptionsFromArray?
    Instead of adding another function, you can use "local".

     {
         local @ARGV = ['--some', 'value'];
         GetOptions(...);
     }
     # the original @ARGV is restored

HOMEPAGE
    Please visit the project's homepage at
    <https://metacpan.org/release/Getopt-Long-Subcommand>.

SOURCE
    Source repository is at
    <https://github.com/perlancar/perl-Getopt-Long-Subcommand>.

SEE ALSO
    Getopt::Long

    Getopt::Long::Complete

    Perinci::CmdLine - a more full featured command-line application
    framework, also with subcommands and completion.

    Pod::Weaver::Section::Completion::GetoptLongSubcommand

AUTHOR
    perlancar

CONTRIBUTOR
    perlancar <perlancar@cpan.org>

CONTRIBUTING
    To contribute, you can send patches by email/via RT, or send pull
    requests on GitHub.

    Most of the time, you don't need to build the distribution yourself. You
    can simply modify the code, then test via:

     % prove -l

    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,
    Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two
    other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps
    required beyond that are considered a bug and can be reported to me.

COPYRIGHT AND LICENSE
    This software is copyright (c) 2025 by perlancar.

    This is free software; you can redistribute it and/or modify it under
    the same terms as the Perl 5 programming language system itself.

BUGS
    Please report any bugs or feature requests on the bugtracker website
    <https://rt.cpan.org/Public/Dist/Display.html?Name=Getopt-Long-Subcomman
    d>

    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.

CAVEATS
  Common options take precedence over subcommand options



( run in 0.793 second using v1.01-cache-2.11-cpan-a9496e3eb41 )