# GNU General Public License for more details.
#
+# Global variables
+set Targets ""
+set Output ""
+
# Procedures
# ---
proc do_strip_main {in_file out_file} {
close $fw
return $lines
}
+proc parse_args {} {
+ global argc argv Targets Output
+
+ # unset this if we find a "--" argument
+ set argv_safe $argv
+ set parse_args 1
+ while {[llength $argv_safe] > 0} {
+ set this_arg [lvarpop argv_safe]
+ switch -exact -- $this_arg {
+ "-o" {
+ if {!$parse_args} {lappend Targets $this_arg; continue}
+ set next_arg [lvarpop argv_safe]
+ if {$next_arg == ""} {
+ puts stderr "Error: option requires an argument -- o"
+ puts stderr ""
+ exit 1
+ }
+ set Output $next_arg
+ }
+ "--" {
+ if {!$parse_args} {lappend Targets $this_arg; continue}
+ set parse_args 0
+ }
+ default {
+ lappend Targets $this_arg
+ }
+ }
+ }
+
+ return
+}
# Main
# ---
-if {$argc < 1} {
- puts stderr "Usage: `./stripspaces.tcl <file> \[output\]'"
- puts stderr ""
- exit 1
-}
-set in_file [lindex $argv 0]
+# check what they gave us
+parse_args
-if {![file readable $in_file]} {
- puts stderr "Error: Cannot open file \"$in_file\"."
+if {[llength $Targets] < 1} {
+ puts stderr "Usage: ./stripspaces.tcl \[-o output\] <file1> \[file2\] ..."
puts stderr ""
exit 1
}
-if {$argc > 1} {
- set out_file [lindex $argv 1]
-} else {
- set out_file "$in_file.strip"
+if {([llength $Targets] > 1) && ($Output != "")} {
+ if {[file exists $Output]} {
+ if {![file isdirectory $Output]} {
+ puts stderr "Error: Specified multiple files but output target exists and is not a directory!"
+ puts stderr ""
+ exit 1
+ }
+ } else {
+ if {[catch {mkdir $Output} errtmp]} {
+ puts stderr "Error: Couldn't create directory \"$Output\""
+ puts stderr ""
+ exit 1
+ }
+ }
}
-puts stderr "Stripping trailing spaces from \"$in_file\" (output: \"$out_file\")"
+set done 0
-set ret [do_strip_main $in_file $out_file]
+foreach xfile $Targets {
+ if {![file readable $xfile]} {
+ puts stderr "Error: Cannot open file \"$xfile\" (skipped)."
+ continue
+ }
-if {$ret < 0} {
- puts stderr "Failed. Couldn't open the input/output filename."
- puts stderr ""
-} else {
- puts stderr "Done. Parsed $ret lines."
+ if {$Output == ""} {
+ set xoutput "$xfile.strip"
+ } elseif {[file isdirectory $Output]} {
+ set xoutput "$Output/$xfile.strip"
+ } else {
+ set xoutput $Output
+ }
+
+ puts stderr "Stripping trailing spaces from \"$xfile\" (output: \"$xoutput\")"
+ set ret [do_strip_main $xfile $xoutput]
+ if {$ret < 0} {
+ puts stderr "$xfile: Failed. Couldn't open the input/output filename."
+ puts stderr ""
+ } else {
+ puts stderr "File $xfile done. Parsed $ret lines."
+ }
+
+ incr done
}