CohortExplorer
view release on metacpan or search on metacpan
lib/CohortExplorer/Application.pm view on Meta::CPAN
OPTIONS
-d --datasource : provide datasource
-u --username : provide username
-p --password : provide password
-v --verbose : show with verbosity
-h --help : show usage message and exit
COMMANDS
help - show application or command-specific help
menu - show menu of available commands
console - start a command console for the application
describe - show datasource description including the entity count
find - find variables using keywords
search - search entities with/without conditions on variables
compare - compare entities across visits (valid to longitudinal datasources with data on at least 2 visits)
history - show saved commands
};
}
sub option_spec {
# Username, password and datasource name are mandatory options
# Password may or may not be provided at start
[],
[ 'datasource|d:s' => 'provide datasource' ],
[ 'username|u:s' => 'provide username' ],
[ 'password|p:s' => 'provide password' ],
[],
[ 'verbose|v' => 'show with verbosity' ],
[ 'help|h' => 'show usage message and exit' ],
[];
}
sub validate_options {
my ( $app, $opts ) = @_;
# Show help and exit
if ( $opts->{help} || keys %$opts == 0 ) {
$app->render( $app->get_default_usage );
exit;
} else {
# Throw exception if mandatory options are missing
if ( !$opts->{datasource}
|| !$opts->{username}
|| !exists $opts->{password} )
{
throw_app_opts_validation_exception(
error => "All mandatory parameters must be provided" );
}
}
}
sub command_map {
console => 'CLI::Framework::Command::Console',
help => 'CohortExplorer::Command::Help',
menu => 'CohortExplorer::Command::Menu',
describe => 'CohortExplorer::Command::Describe',
history => 'CohortExplorer::Command::History',
find => 'CohortExplorer::Command::Find',
search => 'CohortExplorer::Command::Query::Search',
compare => 'CohortExplorer::Command::Query::Compare';
}
sub command_alias {
h => 'help',
m => 'menu',
s => 'search',
c => 'compare',
d => 'describe',
hist => 'history',
f => 'find',
sh => 'console';
}
sub noninteractive_commands {
my ($app) = @_;
my $ds = $app->cache->get('cache')->{datasource};
# May or may not be preloaded
eval 'require ' . ref $ds;
# Menu and console commands are invalid under interactive mode
push my @noninteractive_command, qw/menu console/;
# search, compare and history commands are invalid if the user
# does not have access to any variable
if ( keys %{ $ds->variable_info } == 0 ) {
push @noninteractive_command, qw/search history compare/;
}
# Compare command is invalid only if
# visit variables are defined
if ( !$ds->visit_variables ) {
push @noninteractive_command, 'compare';
}
return @noninteractive_command;
}
sub render {
my ( $app, $output ) = @_;
# All commands except help return hash where key is headingText (table heading)
# and value is ref to array of arrays containing column values
if ( ref $output eq 'HASH' ) {
require Text::ASCIITable;
my $t = Text::ASCIITable->new(
{
hide_Lastline => 1,
reportErrors => 0,
drawRowLine => 1,
headingText => $output->{headingText}
}
);
my @col = @{ shift @{ $output->{rows} } };
my $colWidth = $output->{headingText} eq 'command history' ? 1000 : 30;
$t->setCols(@col);
for (@col) {
$t->setColWidth( $_, $colWidth );
}
# Prevent truncation by inserting space at every $colWidth character
for my $r ( @{ $output->{rows} } ) {
my @row = map {
substr( $_, ( $colWidth - 1 ), 0 ) = ' '
if ( $_ && $_ =~ /^[^\n]+$/ && length $_ >= $colWidth );
$_
} @$r;
$t->addRow(@row);
}
(
my $table = $t->draw(
[ '', '', '', '' ],
[ '', '', '' ],
[ '', '', '', '' ],
[ '', '', '' ],
[ '', '', '', '' ],
[ '', '', '', '' ]
)
) =~ s/\|//g;
if ( $^O eq 'linux' ) {
delete @ENV{qw(PATH)};
$ENV{PATH} = "/usr/bin:/bin";
lib/CohortExplorer/Application.pm view on Meta::CPAN
'escape_char' => '"',
'sep_char' => ',',
'binary' => 1,
'auto_diag' => 1,
'eol' => $/
}
)
}
);
if ( $app->get_current_command eq 'console' ) {
$app->render(
"Welcome to the CohortExplorer version $VERSION console." . "\n\n"
. "Type 'help <COMMAND>' for command specific help." . "\n"
. "Use tab for command-line completion and ctrl + L to clear the screen."
. "\n"
. "Type q or exit to quit." );
}
return;
}
#-------
1;
__END__
=pod
=head1 NAME
CohortExplorer::Application - CohortExplorer superclass
=head1 SYNOPSIS
The class is inherited from L<CLI::Framework::Application> and overrides the following methods:
=head2 usage_text()
This method returns the application usage.
=head2 option_spec()
This method returns the application option specifications as expected by L<Getopt::Long::Descriptive>.
(
[ 'datasource|d:s' => 'provide datasource' ],
[ 'username|u:s' => 'provide username' ],
[ 'password|p:s' => 'provide password' ],
[ 'verbose|v' => 'show with verbosity' ],
[ 'help|h' => 'show usage message and exit' ]
)
=head2 validate_options( $opts )
This method ensures the user has supplied all mandatory options such as datasource, username and password.
=head2 command_map()
This method returns the mapping between command names and command classes
console => 'CLI::Framework::Command::Console',
help => 'CohortExplorer::Command::Help',
menu => 'CohortExplorer::Command::Menu',
describe => 'CohortExplorer::Command::Describe',
history => 'CohortExplorer::Command::History',
find => 'CohortExplorer::Command::Find',
search => 'CohortExplorer::Command::Query::Search',
compare => 'CohortExplorer::Command::Query::Compare'
=head2 command_alias()
This method returns mapping between command aliases and command names
h => 'help',
m => 'menu',
s => 'search',
c => 'compare',
d => 'describe',
hist => 'history',
f => 'find',
sh => 'console'
=head2 pre_dispatch( $command )
This method ensures the invalid commands do not dispatch and logs all commands successfully dispatched.
=head2 noninteractive_commands()
The method returns a list of valid commands under interactive mode. The commands search, compare and history can be invalid because they are application dependent. These commands require the user to have access to at least one variable from the datas...
=head2 render( $output )
This method is responsible for the presentation of the command output. The output from all commands except help is organised into a table.
=head2 read_cmd( )
This method attempts to provide the autocompletion of options and arguments wherever applicable.
=head2 handle_exception( $e )
This method prints and logs exceptions.
=head2 init( $opts )
This method is responsible for the application initialization which includes prompting the user to enter password if not already supplied and initializing the logger and the datasource.
=head2 OPERATIONS
This class attempts to perform the following operations upon successful initialization of the datasource:
=over
=item 1
Prints a menu of available commands based on the datasource type.
=item 2
Provides autocompletion of command arguments/options (if applicable) for the user entered command.
=item 3
Dispatches the command object for command specific processing.
( run in 0.460 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )