use Data::Dumper;
+use Getopt::Mixed "nextOption";
use Digest::MD5 qw(md5_hex);
use File::Touch;
use File::Glob ':glob';
use CIL::Comment;
use CIL::Attachment;
+## ----------------------------------------------------------------------------
+# constants
+
+use constant VERSION => '0.2';
+
my $COMMANDS = {
init => 1,
list => 1,
extract => 1,
};
+my @IN_OPTS = (
+ 'p=s', # p = path
+ 'path>p', # for 'add'
+ 'f=s', # f = filename
+ 'filename=f', # for 'extract'
+
+ 'help',
+ 'version',
+);
+
+my %BOOLEAN_ARGS = (
+ help => 1,
+ version => 1,
+);
+
my $gan = $ENV{GIT_AUTHOR_NAME} || 'Your Name';
my $gae = $ENV{GIT_AUTHOR_EMAIL} || 'you@example.org';
EOF
## ----------------------------------------------------------------------------
+# main program
{
- my ($command) = shift;
- unless ( defined $command and exists $COMMANDS->{$command} ) {
+ my $args = get_options(\@IN_OPTS, \%BOOLEAN_ARGS);
+
+ # do the version and help
+ if ( exists $args->{version} ) {
+ print "cil version ".VERSION."\n";
+ exit;
+ }
+
+ if ( exists $args->{help} ) {
usage();
- exit 2;
+ exit;
}
+ # make sure that the command given is valid
+ Getopt::Mixed::abortMsg('specify a command')
+ if @ARGV == 0;
+
+ my $command = shift @ARGV;
+ Getopt::Mixed::abortMsg("'$command' is not a valid cil command.")
+ unless defined $command and exists $COMMANDS->{$command};
+
my $cil = CIL->new();
if ( $command eq 'init' ) {
- my ($path) = @ARGV;
- $path ||= '.';
- init($cil, $path);
+ init($cil, $args);
}
elsif ( $command eq 'list' ) {
}
elsif ( $command eq 'extract' ) {
- my ($issue_name, $attachment_name, $filename) = @ARGV;
- extract($cil, $issue_name, $attachment_name, $filename);
+ my ($attachment_name) = @ARGV;
+ extract($cil, $attachment_name, $args);
}
# commands
sub init {
- my ($cil, $path) = @_;
+ my ($cil, $args) = @_;
+
+ my $path = $args->{p} || '.'; # default path is right here
# error if $path doesn't exist
unless ( -d $path ) {
}
sub extract {
- my ($cil, $attachment_name, $filename) = @_;
+ my ($cil, $attachment_name, $args) = @_;
my $attachment = CIL::Attachment->new_from_name($cil, $attachment_name);
unless ( defined $attachment ) {
fatal("Couldn't load attachment '$attachment_name'");
}
- $filename ||= $attachment->Filename();
+ my $filename = $args->{f} || $attachment->Filename();
write_file( $filename, $attachment->as_binary );
}
}
my $attachments = $cil->get_attachments_for( $issue );
- print Dumper();
foreach my $attachment ( @$attachments ) {
title( 'Attachment ' . $attachment->name() );
field( 'Filename', $attachment->Filename() );
separator();
}
+## ----------------------------------------------------------------------------
+# helper functions for this command line tool
+
+sub get_options {
+ my ($in_opts, $booleans) = @_;
+
+ my $args = {};
+ Getopt::Mixed::init( @$in_opts );
+ while( my($opt, $val) = nextOption() ) {
+ # if boolean, keep a count of how many there is only
+ if ( exists $booleans->{$opt} ) {
+ $args->{$opt}++;
+ next;
+ }
+ # normal 'string' value
+ if ( defined $args->{$opt} ) {
+ unless ( ref $args->{$opt} eq 'ARRAY' ) {
+ $args->{$opt} = [ $args->{$opt} ];
+ }
+ push @{$args->{$opt}}, $val;
+ }
+ else {
+ $args->{$opt} = $val;
+ }
+ }
+ Getopt::Mixed::cleanup();
+ return $args;
+}
+
sub msg {
print ( defined $_[0] ? $_[0] : '' );
print "\n";
Usage: $0 COMMAND [options]
Commands:
- init PATH
+ init [--path=PATH]
add
summary
list
edit ISSUE
comment ISSUE
attach ISSUE FILENAME
- extract ATTACHMENT [TO_FILENAME]
+ extract ATTACHMENT [--filename=FILENAME]
See <http://kapiti.geek.nz/software/cil.html> for further information.
Report bugs to <andychilton -at- gmail -dot- com>.
}
## ----------------------------------------------------------------------------
+
=head1 NAME
cil - the command-line issue list
$ cil attach cafebabe filename.txt
... added attachment 'decaf7ea' ...
- $ cil extract decaf7ea other_filename.txt
+
+ $ cil extract decaf7ea
+ $ cil extract decaf7ea --filename=other_filename.txt
=head1 DESCRIPTION
=over
-=item init
+=item init [--path=PATH]
-Creates a local '.cil' file and an 'issues' directory.
+Creates a local '.cil' file and an 'issues' directory. If PATH is specified,
+the config file and directory will be created in the destination directory.
=item summary
Adds that particular filename to an existing issue.
-=item extract ATTACHMENT [FILENAME]
+=item extract ATTACHMENT [--filename=FILENAME]
Extracts the file from the attachment number. If filename if given uses that,
otherwise it will use the original one saved along with the attachment.
Copyright (C) 2008 by Andrew Chilton
-cil is free software: you can redistribute it and/or modify it under the terms
+Cil is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.