[TxMt] Run Scripts with Arguments and pipe STDIN to script - A simple Approach?

Hans-Joerg Bibiko bibiko at eva.mpg.de
Tue Nov 28 08:53:20 UTC 2006


Hi,

this thread follows the discussion in

http://thread.gmane.org/gmane.editors.textmate.general/13806/focus=13806

'run script with args'

In order to be able to control the arguments and STDIN here my  
suggestion:

Example for Perl:

#!/usr/bin/env perl -w

#!TMC: STDIN=cat ~/Desktop/test.txt
#!TMC: ARG=1 "2 and 3" /etc/file
#!TMC:end

print join ('-', at ARGV);
print "\n";

@a = <STDIN>;
chomp(@a);
$j=1;
foreach $line (@a) {
	print $j++.$line."\n";
}

The actual command line would be: 'cat ~/Desktop/test.txt | /usr/bin/ 
perl -w  1 "2 and 3" /etc/file

The idea is to specify the values for ARG and STDIN hidden as  
comments within the script.
If you want to test several ARGs and STDINs you can write

#!/usr/bin/env perl -w

# !TMC: STDIN=cat ~/Desktop/test.txt
# !TMC: ARG=1 "2 and 3" /etc/file
#!TMC: STDIN = echo -e "A\nB"
#!TMC: ARG = DATA
#!TMC:end
...

By doing so line 5 and 6 are active. You see to change ARG and STDIN  
is easy.
Of course, you can think about to use tm_dialog with an history list  
or whatever but I believe this is fast and very simple. If you forget  
to delete the TMC tags while running this script from Terminal it  
doesn't matter. These tags are comments.

For that purpose I add some stuff to scriptmate.rb.

Here some hints:

     #parse script for arguments and STDIN pipe data given as comment(s)
     #!TMC: ARG=           //everything after = is interpreted as  
arguments like ARG= one two "three and four" five
     #!TMC: STDIN=         //everything after = will be executed on  
the system and piped to the script like STDIN=cat /PATH/TO/FILE
     #!TMC:end             //marker to cancel the parsing; the  
parsing also ends if ARG _and_ STDIN are set
     #
     #!TMC: ...           //active
     # !TMC: ...          //not active to have the chance to chooce
     #
     #known problems:
     # for Perl
     #   piping STDIN: e.g. @a = <>; doesn't work, use instead @a =  
<STDIN>;
     #

Any comments?

Bye,

Hans

PLEASE forgive my Ruby syntax ;)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: scriptmate.rb
Type: text/x-ruby-script
Size: 4187 bytes
Desc: not available
URL: <http://lists.macromates.com/textmate/attachments/20061128/9f8d124e/attachment.rb>
-------------- next part --------------






More information about the textmate mailing list