#!perl

our $DATE = '2014-12-13'; # DATE
our $VERSION = '0.03'; # VERSION
# FRAGMENT id=shcompgen-nohint

use 5.010;
use strict;
use warnings;
use experimental 'smartmatch';

use App::shcompgen;
use Perinci::CmdLine::Any -prefer_lite=>1;

my $urlprefix = '/App/shcompgen/';
Perinci::CmdLine::Any->new(
    url => $urlprefix,
    log => 1,
    subcommands => {
        init     => {url=>"${urlprefix}init"},
        generate => {url=>"${urlprefix}generate"},
        list     => {url=>"${urlprefix}list"},
        remove   => {url=>"${urlprefix}remove"},
    },
)->run;

# ABSTRACT: Generate shell completion scripts
# PODNAME: shcompgen

__END__

=pod

=encoding UTF-8

=head1 NAME

shcompgen - Generate shell completion scripts

=head1 VERSION

This document describes version 0.03 of shcompgen (from Perl distribution App-shcompgen), released on 2014-12-13.

=head1 SYNOPSIS

Initialize:

 % shcompgen init

Generate shell completion scripts for all detectable programs in PATH:

 % shcompgen generate
 % shcompgen generate --verbose

Generate some programs only, replace if previously already exists:

 % shcompgen generate --replace prog1 prog2 ./bin/prog3

List all shell completion scripts generated by us:

 % shcompgen list
 % shcompgen list --detail

Remove some shell completion scripts:

 % shcompgen remove prog1 prog2

Remove all generated shell completion scripts:

 % shcompgen remove

=head1 DESCRIPTION

B<NOTE: EARLY RELEASE. ONLY BASH SUPPORT HAVE BEEN ADDED. SUPPORT FOR THE OTHER
SHELLS WILL FOLLOW.>

Some shells, like bash/fish/zsh, supports tab completion for programs. They are
usually activated by issuing one or more C<complete> (zsh uses C<compctl>)
internal shell commands. The completion scripts which contain these commands are
usually put in (e.g., for fish) C</etc/fish/completion/PROGNAME.fish> (if one
wants to install globally) or C<~/.config/fish/completions/PROGNAME.fish> (if
one wants to install per-user).

This utility, B<shcompgen>, can detect how to generate shell completion scripts
for some programs and then install the completion scripts into the
abovementioned location (the default is to per-user directory, but if running as
root or with C<--global> switch will install to the global directory).

It can also list all completion scripts generated by it, and be instructed to
uninstall them again.

It supports several shells, currently: bash, fish, tcsh, and zsh. Shell-specific
information can be found below.

=head2 bash-specific information

This script can work with the C<bash-completion> package (and uses the same
global completion directory: C</etc/bash_completion.d>). At the time of this
writing, bash-completion (at version 2.1) does not yet look at per-user
completion scripts directory. This script picks
C<~/.config/bash/completions/PROGNAME> as location for per-user completion
scripts. If later on C<bash-completion> package decides on a different per-user
location, this script will probably be adjusted too.

=head2 fish-specific information

=head2 tcsh-specific information

=head2 zsh-specific information

=head2 Program detection

Below are the types/kinds of programs that can be detected. Expect the list to
expand as more methods are added.

=over

=item * Scripts which are tagged with hints of what completion program to use

You can put this line in a script, e.g. in a script called C<foo>:

 # FRAGMENT id=shcompgen-hint command=bar

The above line tells C<shcompgen> that the script should be completed using an
external program called C<bar>. This will construct this completion script, e.g.
for bash:

 complete -C bar foo

=item * Getopt::Long::Complete-based CLI scripts

If a script like C<foo> is detected as a Perl script using
L<Getopt::Long::Complete>, we know that it can complete itself. Thus,
C<shcompgen> will generate this completion script (e.g. for bash):

 complete -C foo foo

=item * Completion programs which are tagged with hints of what programs they complete

You can create a completion script in Perl (or other language, actually), e.g.
C<_foo> and tag it with hints of what programs they complete, e.g.

 # FRAGMENT id=shcompgen-hint completer=1 for=foo,foo-this-host

This will add completion script for C<foo>:

 complete -C _foo foo

as well as for C<foo-this-host>:

 complete -C _foo foo-this-host

=item * Perinci::CmdLine-based CLI scripts

If a script like C<foo> is detected as a Perl script using L<Perinci::CmdLine>
(or its variant like L<Perinci::CmdLine::Lite> or L<Perinci::CmdLine::Any>) we
know that it can complete itself. Thus, C<shcompgen> will add this completion
script e.g. for bash:

 complete -C foo foo

=item * Other methods

Other methods will be added in the future, e.g. by parsing manpage or POD, and
so on.

=back

=head1 SUBCOMMANDS

=head2 B<generate>

Generate shell completion scripts for detectable programs.

=head2 B<init>

Initialize shcompgen.

This subcommand creates the completion directories and initialization shell
script.


=head2 B<list>

List all shell completion scripts generated by this script.

=head2 B<remove>

Remove shell completion scripts generated by this script.

=head1 OPTIONS

C<*> marks required options.

=head2 Common options

=over

=item B<--config-path>=I<s>

Set path to configuration file.

Can be specified multiple times.

=item B<--config-profile>=I<s>

Set configuration profile to use.

=item B<--debug>

Set log level to 'debug'.

=item B<--format>=I<s>

Set output format.

=item B<--help>, B<-h>, B<-?>

Show help message.

=item B<--json>

Set output format to json.

=item B<--log-level>=I<s>

Set log level.

=item B<--naked-res>

When outputing as JSON, strip result envelope.

By default, when outputing as JSON, the full enveloped result is returned, e.g.:

    [200,"OK",[1,2,3],{"func.extra"=>4}]

The reason is so you can get the status (1st element), status message (2nd
element) as well as result metadata/extra result (4th element) instead of just
the result (3rd element). However, sometimes you want just the result, e.g. when
you want to pipe the result for more post-processing:

    [1,2,3]

In this case, you can use `--naked-res`.


=item B<--no-config>

Do not use any configuration file.

=item B<--quiet>

Set log level to 'error'.

=item B<--subcommands>

List available subcommands.

=item B<--trace>

Set log level to 'trace'.

=item B<--verbose>

Set log level to 'info'.

=item B<--version>, B<-v>

Show program version.

=back

=head2 Options for subcommand generate

=over

=item B<--bash-global-dir>=I<s>

Directory to put completions scripts.

Default value:

 "/etc/bash_completion.d"

=item B<--bash-per-user-dir>=I<s>

Directory to put completions scripts.

=item B<--global>

Use global completions directory.

Shell has global (system-wide) completions directory as well as per-user. For
example, in fish the global directory is by default `/etc/fish/completions` and
the per-user directory is `~/.config/fish/completions`.

By default, if running as root, the global is chosen. And if running as normal
user, per-user directory is chosen. Using `--global` or `--per-user` overrides
that and manually select which.


=item B<--per-user>

Alias for --no-global.

See C<--global>.

=item B<--prog-json>=I<s>

Program(s) to generate completion for (JSON-encoded).

See C<--prog>.

=item B<--prog>=I<s@>

Program(s) to generate completion for.

Can contain path (e.g. `../foo`) or a plain word (`foo`) in which case will be
searched from PATH.


Can be specified multiple times.

=item B<--replace>

Replace existing script.

The default behavior is to skip if an existing completion script exists.


=item B<--shell>=I<s>

Override autodetection and select shell manually.

Valid values:

 ["bash"]

The default is to look at your SHELL environment variable value. If it is
undefined, the default is `bash`.


=back

=head2 Options for subcommand init

=over

=item B<--bash-global-dir>=I<s>

Directory to put completions scripts.

Default value:

 "/etc/bash_completion.d"

=item B<--bash-per-user-dir>=I<s>

Directory to put completions scripts.

=item B<--global>

Use global completions directory.

Shell has global (system-wide) completions directory as well as per-user. For
example, in fish the global directory is by default `/etc/fish/completions` and
the per-user directory is `~/.config/fish/completions`.

By default, if running as root, the global is chosen. And if running as normal
user, per-user directory is chosen. Using `--global` or `--per-user` overrides
that and manually select which.


=item B<--per-user>

Alias for --no-global.

See C<--global>.

=item B<--shell>=I<s>

Override autodetection and select shell manually.

Valid values:

 ["bash"]

The default is to look at your SHELL environment variable value. If it is
undefined, the default is `bash`.


=back

=head2 Options for subcommand list

=over

=item B<--bash-global-dir>=I<s>

Directory to put completions scripts.

Default value:

 "/etc/bash_completion.d"

=item B<--bash-per-user-dir>=I<s>

Directory to put completions scripts.

=item B<--detail>

=item B<--global>

Use global completions directory.

Shell has global (system-wide) completions directory as well as per-user. For
example, in fish the global directory is by default `/etc/fish/completions` and
the per-user directory is `~/.config/fish/completions`.

By default, if running as root, the global is chosen. And if running as normal
user, per-user directory is chosen. Using `--global` or `--per-user` overrides
that and manually select which.


=item B<--per-user>

Alias for --no-global.

See C<--global>.

=item B<--shell>=I<s>

Override autodetection and select shell manually.

Valid values:

 ["bash"]

The default is to look at your SHELL environment variable value. If it is
undefined, the default is `bash`.


=back

=head2 Options for subcommand remove

=over

=item B<--bash-global-dir>=I<s>

Directory to put completions scripts.

Default value:

 "/etc/bash_completion.d"

=item B<--bash-per-user-dir>=I<s>

Directory to put completions scripts.

=item B<--global>

Use global completions directory.

Shell has global (system-wide) completions directory as well as per-user. For
example, in fish the global directory is by default `/etc/fish/completions` and
the per-user directory is `~/.config/fish/completions`.

By default, if running as root, the global is chosen. And if running as normal
user, per-user directory is chosen. Using `--global` or `--per-user` overrides
that and manually select which.


=item B<--per-user>

Alias for --no-global.

See C<--global>.

=item B<--prog-json>=I<s>

Program(s) to remove completion script of (JSON-encoded).

See C<--prog>.

=item B<--prog>=I<s@>

Program(s) to remove completion script of.

Can contain path (e.g. `../foo`) or a plain word (`foo`) in which case will be
searched from PATH.


Can be specified multiple times.

=item B<--shell>=I<s>

Override autodetection and select shell manually.

Valid values:

 ["bash"]

The default is to look at your SHELL environment variable value. If it is
undefined, the default is `bash`.


=back

=head1 FILES

B</etc/shcompgen.conf>

B<~/shcompgen.conf>

=head1 TODO

=head1 SEE ALSO

L<Dist::Zilla::Plugin::Rinci::InstallCompletion>

=head1 COMPLETION

This script has shell tab completion capability with support for several shells.

=head2 bash

To activate bash completion for this script, put:

 complete -C shcompgen shcompgen

in your bash startup (e.g. C<~/.bashrc>). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

You can also install L<App::BashCompletionProg> which makes it easy to add completion for Getopt::Long::Complete-based scripts. After you install the module and put C<. ~/.bash-complete-prog> (or C<. /etc/bash-complete-prog>), you can just run C<bash-completion-prog> and the C<complete> command will be added to your C<~/.bash-completion-prog>. Your next shell session will then recognize tab completion for the command.

=head2 fish

To activate fish completion for this script, execute:

 begin; set -lx COMP_SHELL fish; set -lx COMP_MODE gen_command; shcompgen; end > $HOME/.config/fish/completions/shcompgen.fish

Or if you want to install globally, you can instead write the generated script to C</etc/fish/completions/shcompgen.fish> or C</usr/share/fish/completions/shcompgen.fish>. The exact path might be different on your system. Please check your C<fish_complete_path> variable.

=head2 tcsh

To activate tcsh completion for this script, put:

 complete shcompgen 'p/*/`shcompgen`/'

in your tcsh startup (e.g. C<~/.tcshrc>). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

=head2 zsh

To activate zsh completion for this script, put:

 _shcompgen() { read -l; local cl="$REPLY"; read -ln; local cp="$REPLY"; reply=(`COMP_SHELL=zsh COMP_LINE="$cl" COMP_POINT="$cp" shcompgen`) }

 compctl -K _shcompgen shcompgen

in your zsh startup (e.g. C<~/.zshrc>). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-shcompgen>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-shcompgen>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-shcompgen>

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.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 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.

=cut
