Last active
March 11, 2022 06:05
-
-
Save YasuhiroABE/767583f87d52524a942756c8a1babded to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
=begin | |
This script generates ID files into the current directory for the "sdamps stamp -f" command. | |
Usage: $0 [-n <number of unit>] [-c <prefix number>] [-p <output file prefix>] [-s <output file suffix>] <total number of set> | |
-n <number of unit> (default: 100) | |
If the "-n 10" is specified, then the each ID file contains 10 lines. | |
-c <prefix number> (default: "YYYYMMDD") | |
If the "-s 2022" is specified, then the each ID started with the "20220001". | |
-p <output file prefix> (default: "id") | |
-s <output file suffix> (default: ".txt") | |
The output file name will be "id01.txt" and the digit of number part will be calculated by the "<total number of set> / <number of unit>".length + 1 | |
=end | |
require 'optparse' | |
opt = OptionParser.new | |
params = { | |
:n => 100, | |
:c => Time.now.strftime("%Y%m%d").to_i, | |
:p => "id", | |
:s => ".txt", | |
:total => 0, ## ARGV[0] | |
:total_length => 0, ## digits of ID numbers | |
:numdigits => 0 ## "id#{:numdigits}.txt" | |
} | |
opt.on('-n NUM') {|v| params[:n] = v.to_i } | |
opt.on('-c NUM') {|v| params[:c] = v.to_i } | |
opt.on('-p VAL') {|v| params[:p] = v } | |
opt.on('-s VAL') {|v| params[:s] = v } | |
opt.parse!(ARGV) | |
## check params | |
if ARGV.length != 1 and ARGV[0].to_i <= 0 | |
puts "[error] You must be specified the total number of sheets." | |
exit(0) | |
end | |
if params[:n] <= 0 or params[:c] <= 0 | |
puts "[error] The option must be a >0 number." | |
exit(0) | |
end | |
## calculate other option | |
params[:total] = ARGV[0].to_i | |
params[:total_length] = ARGV[0].length | |
params[:n] = (params[:total] > params[:n]) ? params[:n] : params[:total] | |
params[:numdigits] = (ARGV[0].to_i / params[:n]).to_s.length + 1 | |
params[:unit] = (params[:total].to_i * 1.0 / params[:n].to_i * 1.0).ceil | |
p params | |
## Main | |
counter = 0 | |
params[:unit].times {|i| | |
digits = format "%0#{params[:numdigits]}i", (i+1) | |
filename = "#{params[:p]}#{digits}#{params[:s]}" | |
open(filename, "w") {|f| | |
params[:n].times {|n| | |
break if (params[:total] == counter) | |
f.write(format "#{params[:c]}%0#{params[:total_length]}i\n", (params[:n] * i) + n) | |
counter += 1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment