LoginSignup
4
2

More than 5 years have passed since last update.

Nimでファイル内文字列置換ツールを作る

Posted at

概要

任意のファイルを開いて、コマンドラインで渡したパラメータ(キー・値)で置換し、コンソールに出力するツールです。
bashとかなら、ヒアドキュメントで済むんですけど。

ソース

rep.nim

# ファイル内の文字列置換ツール

import os
import parseopt2
import tables
import queues
import strutils

proc rep_files(files:Queue[string],params:Table[string,string]) : bool =
  result = true
  # ファイルを取り出す
  let f:string = files[0]
  # ファイルの存在チェック
  if f.existsFile :
    # ファイルを1行ずつ読み込む
    for line in f.lines() :
      var work = line
      # パラメータで置換する
      for k,v in params :
        work = work.replace(k,v)
      echo work
  else: 
    result = false

# メインモジュールとして起動しているかチェック
if isMainModule :
  var files = initQueue[string]()
  var params = initTable[string,string]()
  # イテレータで取得    
  for kind, key, val in getopt() :
    # (余談)kindは、enum型なので、case文ではすべてのenum値
    # を網羅していないとコンパイルエラーとなります
    case kind
    of cmdArgument:
      # ファイル名として保存
      if files.len == 0 :
        files.add(key)
      else :
        echo "指定できるファイルは1つだけです"
        quit(1)
    of cmdLongOption, cmdShortOption:
      # 置換パラメータを保存(大文字版も追加)
      params[ [ "${" , key , "}"].join("") ] = val
      params[ [ "${" , key.toUpperAscii , "}"].join("") ] = val.toUpperAscii
    of cmdEnd:
      discard

  discard rep_files(files,params)

実行結果

$ cat hello.txt
hello ${hoge} ${HOGE}
こんにちは ${hoge} ${HOGE}
さようなら ${hoge} ${foo}-${bar}

$ rep hello.txt --foo=a --bar=b -hoge:6in
hello 6in 6IN
こんにちは 6in 6IN
さようなら 6in a-b

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2