gauche.parseoptを試す

30分プログラム、その195。gauche.parseoptコマンドライン引数を試してみる。

お題は、headコマンド。

使い方

# 短いやつ
$ ./head.scm -v
head 1.0.0

# 長いやつ
$ ./head.scm --version
head 1.0.0

# 引数付き
$ ./head.scm -n 1 hello.js
print('Hello,World!!!');

# 引数付き その2
$ ./head.scm --number=1 hello.js
print('Hello,World!!!');

# デフォルトの動作
$ ./head.scm hello.js
print('Hello,World!!!');
java.lang.System.out.println('Hello,Java!!');

// swing
importPackage(Packages.javax.swing);

ソースコード

#! /opt/local/bin/gosh
;; -*- mode:scheme; coding:utf-8 -*-
;;
;; head.scm -
;;
;; Copyright(C) 2007 by mzp
;; Author: MIZUNO Hiroki / mzpppp at gmail dot com
;; http://howdyworld.org
;;
;; Timestamp: 2007/12/01 22:29:38
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the same terms as Scheme itself.
;;

(use srfi-1)
(use gauche.parseopt)

(define (run str)
  (main (string-split str " ")))

(define (main args)
  (let-args (cdr args)
      ((version "v|version" #f)
       (count   "n|number=n" 5) .
       filenames)
    (cond 
     (version (print "head 1.0.0"))
     (else (for-each (lambda (x)
		       (for-each print
				 (take (port->string-list 
					(open-input-file x))
				       count)))
		     filenames)))))

とこで、この

(for-each (lambda (x)
		       (for-each print
				 (take (port->string-list 
					(open-input-file x))
				       count)))
		     filenames)

(for-each (cut
	   for-each print
	   (take (port->string-list 
		  (open-input-file <>))
		 count))
	  filenames)

にするとうまく動かないのはなぜだろう?