App-optex
view release on metacpan or search on metacpan
script/optex view on Meta::CPAN
unsupported options.
% date -Iseconds
Common configuration is stored in F<~/.optex.d/default.rc> file, and
those rules are applied to all commands executed through B<optex>.
Actually, C<--iso-8601> option can be defined simpler as this:
option --iso-8601 -I$<shift>
This works fine almost always, but fails with sole C<--iso-8601>
option preceding other option like this:
% date --iso-8601 -u
=head2 COMMAND ALIASES
Command aliases can be set in the configuration file like this:
[alias]
pgrep = [ "greple", "-Mperl", "--code" ]
Alias name is used to find rc file and module directory. In the above
example, F<~/.optex.d/pgrep.rc> and F<~/.optex.d/pgrep/> will be
referred.
Read L<CONFIGURATION FILE> section.
=head2 MACROS
Complex string can be composed using macro C<define>. Next example is
an awk script to count vowels in the text, to be declared in file
F<~/.optex.d/awk.rc>.
define __delete__ /[bcdfgkmnpsrtvwyz]e( |$)/
define __match__ /ey|y[aeiou]*|[aeiou]+/
define __count_vowels__ <<EOS
{
s = tolower($0);
gsub(__delete__, " ", s);
for (count=0; match(s, __match__); count++) {
s=substr(s, RSTART + RLENGTH);
}
print count " " $0;
}
EOS
option --vowels __count_vowels__
This can be used like this:
% awk --vowels /usr/share/dict/words
When setting complex option, C<expand> directive is useful. C<expand>
works almost same as C<option>, but effective only within the file
scope, and not available for command line option.
expand repository ( -name .git -o -name .svn -o -name RCS )
expand no_dots ! -name .*
expand no_version ! -name *,v
expand no_backup ! -name *~
expand no_image ! -iname *.jpg ! -iname *.jpeg \
! -iname *.gif ! -iname *.png
expand no_archive ! -iname *.tar ! -iname *.tbz ! -iname *.tgz
expand no_pdf ! -iname *.pdf
option --clean \
repository -prune -o \
-type f \
no_dots \
no_version no_backup \
no_image \
no_archive \
no_pdf
% find . --clean -print
=head2 MODULES
B<optex> also supports module extension. In the example of C<date>,
module file is found at F<~/.optex.d/date/> directory. If default
module, F<~/.optex.d/date/default.pm> exists, it is loaded
automatically on every execution.
This is a normal Perl module, so package declaration and the final
true value is necessary. Between them, you can put any kind of Perl
code. For example, next program set environment variable C<LANG> to
C<C> before executing C<date> command.
package default;
$ENV{LANG} = 'C';
1;
% /bin/date
2017å¹´ 10æ22æ¥ æ¥ææ¥ 18æ00å00ç§ JST
% date
Sun Oct 22 18:00:00 JST 2017
Other modules are loaded using C<-M> option. Unlike other options,
C<-M> have to be placed at the beginning of argument list. Module
files in F<~/.optex.d/date/> directory are used only for C<date>
command. If the module is placed on F<~/.optex.d/> directory, it can
be used from all commands.
If you want use C<-Mes> module, make a file F<~/.optex.d/es.pm> with
following content.
package es;
$ENV{LANG} = 'es_ES';
1;
% date -Mes
domingo, 22 de octubre de 2017, 18:00:00 JST
When the specified module was not found in library path, B<optex>
ignores the option and stops argument processing immediately. Ignored
options are passed through to the target command.
Module is also used with subroutine call. Suppose
F<~/.optex.d/env.pm> module look like:
package env;
sub setenv {
while (($a, $b) = splice @_, 0, 2) {
$ENV{$a} = $b;
}
}
1;
( run in 0.601 second using v1.01-cache-2.11-cpan-99c4e6809bf )