#!/usr/bin/perl -w

BEGIN {
	our $VERSION = "0.1";
	$VERSION = eval $VERSION;
}

use warnings;
use strict;

use Getopt::Compact::WithCmd;
use Module::Load;
use Term::ANSIColor;

=head1 NAME

mcbain - Command line utility for reading McBain-based API documentation

=head1 VERSION

version 0.1

=head1 SYNOPSIS

	# in the command line
	$ mcbain help
	$ mcbain describe <TOPIC_CLASS>

=head1 DESCRIPTION

This command line utility provides a simple interface for reading documentation
of APIs created with L<API::McBain>.

Currently, the script handles one command, C<describe>, for reading C<McBain> topic
documentation. For example, echoing C<mcbain describe MyAPI::Topic> will attempt
to parse the C<MyAPI::Topic> class (if exists), and will result in a complete
topic description to be printed. This description will define all methods provided
by the topic, with their parameter specification, descriptions, etc.

More commands to be added in the future.

=cut

my $go = Getopt::Compact::WithCmd->new(
	name          => 'mcbain',
	version       => $main::VERSION,
	command_struct => {
		describe => {
			desc        => 'describe a McBain-based topic',
			args        => 'topic',
		}
	},
);

my $opts = $go->opts;
my $cmd  = $go->command;

if ($cmd && $cmd eq 'describe') {
	my $topic = shift @ARGV
		|| $go->show_usage($cmd);

	load $topic;
	unless ($topic->can('new')) {
		print "$topic does not seem to be a Perl class.\n";
		exit 1;
	}
	my $obj = $topic->new;
	unless ($obj->can('provided_methods')) {
		print "$topic does not seem to be a McBain API topic.\n";
		exit 1;
	}

	print color 'bold underline white';
	print "\n", $topic, "\n\n";
	print color 'reset';

	print color 'bold underline green';
	print "Provided methods:\n";
	print color 'reset';
	foreach ($obj->provided_methods->Keys) {
		my $method = $obj->provided_methods->FETCH($_);
		print color 'bold';
		print "  * ";
		print color 'bold underline cyan';
		print $_, "\n";
		print color 'reset';
		if ($method->description) {
			print color 'bold yellow';
			print "    Description: ";
			print color 'reset';
			print $method->description, "\n";
		}
		if ($method->params) {
			print color 'bold yellow';
			print "    Parameters:\n";
			print color 'reset';
			foreach my $param (keys %{$method->params}) {
				print color 'bold red';
				print "       $param: ";
				print color 'reset';
				print join(', ', keys(%{$method->params->{$param}})), "\n";
			}
		}
		if ($method->returns) {
			print color 'bold yellow';
			print "    Return type: ";
			print color 'reset';
			print $method->returns, "\n";
		}
		print "\n";
	}
} else {
	$go->show_usage;
}

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to
C<bug-API-McBain@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=API-McBain>.

=head1 SUPPORT

You can find documentation for this module by invoking it with
the C<help> command:

	mcbain help

=head1 AUTHOR

Ido Perlmuter <ido@ido50.net>

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2013, Ido Perlmuter C<< ido@ido50.net >>.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself, either version
5.8.1 or any later version. See L<perlartistic|perlartistic> 
and L<perlgpl|perlgpl>.

The full text of the license can be found in the
LICENSE file included with this module.

=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.

=cut

1;
__END__
