Image::Magick::CommandParser - Parse any command line acceptable to convert or mogrify
Image::Magick::CommandParser
This is scripts/synopsis.pl:
#!/usr/bin/env perl use strict; use warnings; use warnings qw(FATAL utf8); use Image::Magick::CommandParser; # ---------------------------------------------- my($command) = 'convert colors/*s*.png -append output.png'; my($processor) = Image::Magick::CommandParser -> new ( command => $command, maxlevel => 'notice', ); $processor -> run; print 'Input: ', $command, "\n"; print 'Result: ', $processor -> result, "\n";
With its output (after running in the distro dir, with access to colors/*.png):
Input: convert colors/*s*.png -append output.png Result: convert colors/fuchsia.png colors/silver.png -append output.png
Image::Magick::CommandParser is a stand-alone parser for command lines acceptable to the Imagemagick programs convert and mogrify.
convert
mogrify
It aims to handle all constructs supported by Imagemagick itself, but it's vital to understand that this module does not use any component of Imagemagick. Hence the stand-alone just above.
In particular the output is a stack, accessible via the $object -> stack method, which returns an array of hashrefs.
$object -> stack
The stack is managed by an object of type Set::Array. See the "FAQ" for details.
The result - as a space-separated string of tokens detected in the command - is returned by "result()".
The actual parsing is done with Set::FA::Element.
Consult the "FAQ" and t/test.t for specific examples of command line options supported. A few of them are included here:
Samples:
Imagemagick has a web page, http://imagemagick.org/script/command-line-processing.php, dedicated to the features available in its command line processing code. Please report any cases where this module does not support one of those features. But see "Trouble-shooting" before reporting an issue, since there I list a few special cases.
Install Image::Magick::CommandParser as you would for any Perl module:
Perl
Run:
cpanm Image::Magick::CommandParser
or run:
sudo cpan Image::Magick::CommandParser
or unpack the distro, and then:
perl Makefile.PL make (or dmake or nmake) make test make install
Call new() as my($parser) = Image::Magick::CommandParser -> new(k1 => v1, k2 => v2, ...).
new()
my($parser) = Image::Magick::CommandParser -> new(k1 => v1, k2 => v2, ...)
It returns a new object of type Image::Magick::CommandParser.
Key-value pairs accepted in the parameter list (see also the corresponding methods [e.g. "command([$string])"]):
The command string to process.
Default: ''.
Specify a logger object.
The default value triggers creation of an object of type Log::Handler which outputs to the screen.
To disable logging, just set logger to the empty string.
Default: undef.
This option is only used if an object of type Log::Handler is created. See logger above.
See also Log::Handler::Levels.
Nothing is printed by default.
Default: 'notice'. Typical value is 'debug' and 'info'.
Default: 'error'.
No lower levels are used.
Here, the [] indicate an optional parameter.
Get or set the command line string to be processed.
Calls $self -> logger -> log($level => $s) if ($self -> logger).
Get or set the logger object.
This logger is passed to GraphViz2.
Note: logger is a parameter to new().
logger
Get or set the value used by the logger object.
This option is only used if an object of type Log::Handler is created. See Log::Handler::Levels.
Note: maxlevel is a parameter to new().
maxlevel
Note: minlevel is a parameter to new().
minlevel
The constructor. See "Constructor and Initialization".
Returns a string of space-separated tokens, as output by the parser.
There is result() in its entirety:
result()
sub result { my($self) = @_; return join(' ', map{$$_{token} } $self -> stack -> print); } # End of result.
Returns 0 for success and 1 for failure.
Run the parser on the command provided by new(command => '...') or provided by calling "command([$string])" before calling run().
new(command => '...')
run()
If the return value is 0, call "result()" to get a string corresponding to the input, or process the stack directly by calling "stack()".
Globs etc in the input will be represented by multiple items in the stack.
This returns an object of type Set::Array, which you can use to iterate over the items output by the parser.
See "result()" just above for how to use this object.
They are hashrefs, with these keys:
This is the token extracted from the command line.
Note: In the cases of file globbing and redirection of input from a file, these tokens are after expansion of such items.
This is my classification of the type of token detected. The values taken by type are:
type
In this case, the token will be the empty string.
token
This is used for both explicit file names and for each file name produced by expanding globs.
The way Set::FA::Element was designed, it will not move from a state to the same state when the input matches. So, to trigger the entry or exit subs, I have to rock back-and-forth between 2 states which are more-or-less identical.
I had a failure when installing the module on my laptop for the 1st time. The problem was that, somehow, during the installation of Image::Magick, root had become the owner of a directory under the control of perlbrew. To fix this, I had to do:
sudo chown ron:ron /home/ron/perl5/perlbrew/perls/perl-5.20.2/lib/site_perl/5.20.2/x86_64-linux/auto/Image/Magick
So, you must put the '@' at the end of the region:
convert magick:logo -resize '10000@' wiz10000.png
So, this won't work:
convert 'images.gif[0]' image.png
Imager
Image::Magick::Chart
Image::Magick::PolyText
Image::Magick::Tiler
Set::Array
Set::FA::Element
The file Changes was converted into Changelog.ini by Module::Metadata::Changes.
Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
https://github.com/ronsavage/Image-Magick-CommandParser
Email the author, or log a bug on RT:
https://rt.cpan.org/Public/Dist/Display.html?Name=Image::Magick::CommandParser.
Image::Magick::CommandParser was written by Ron Savage <ron@savage.net.au> in 2016.
My homepage: http://savage.net.au/
Australian copyright (c) 2016, Ron Savage.
All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of The Perl License, a copy of which is available at: http://dev.perl.org/licenses/
To install Image::Magick::CommandParser, copy and paste the appropriate command in to your terminal.
cpanm
CPAN shell
perl -MCPAN -e shell install Image::Magick::CommandParser
For more information on module installation, please visit the detailed CPAN module installation guide.