Module: Elect::CLI
- Defined in:
- lib/elect/cli.rb
Overview
命令行接口
用法:
elect [选项] <文件或目录...>
选项:
-d, --dry-run 仅检测,不修改文件
-v, --verbose 详细输出
-e, --exts x,y,z 自定义扩展名(逗号分隔)
-x, --exclude a,b 排除目录名(逗号分隔)
-h, --help 显示帮助
-V, --version 显示版本
Class Method Summary collapse
Class Method Details
.build_parser(opts) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/elect/cli.rb', line 64 def self.build_parser(opts) OptionParser.new do |o| o. = <<~BANNER elect — AIGC 水印干掉器 用法: elect [选项] <文件或目录...> 示例: elect ./src # 清理 src 目录下所有水印 elect -d ./docs # 仅检测不修改(dry-run) elect -v main.py # 清理单个文件,详细输出 elect -e md,txt ./docs # 只扫描 md 和 txt elect -x vendor,tmp ./src # 排除 vendor 和 tmp 目录 选项: BANNER o.on("-d", "--dry-run", "仅检测水印,不修改文件") do opts[:dry_run] = true end o.on("-v", "--verbose", "详细输出") do opts[:verbose] = true end o.on("-e", "--exts x,y,z", Array, "自定义扫描扩展名(逗号分隔)") do |v| opts[:exts] = v end o.on("-x", "--exclude a,b", Array, "排除目录名(逗号分隔)") do |v| opts[:exclude] = v end o.on("-h", "--help", "显示帮助") do puts o exit end o.on("-V", "--version", "显示版本") do puts "elect #{Elect::VERSION}" exit end end end |
.run(argv = ARGV) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/elect/cli.rb', line 22 def self.run(argv = ARGV) opts = { dry_run: false, verbose: false, exts: nil, exclude: nil, } parser = build_parser(opts) paths = parser.parse(argv) if paths.empty? warn parser exit 1 end all_results = [] paths.each do |p| path = Pathname.new(p) if path.directory? results = Processor.process_dir( path, dry_run: opts[:dry_run], verbose: opts[:verbose], exts: opts[:exts], exclude: opts[:exclude], ) all_results.concat(results) elsif path.file? r = Processor.process_file(path, dry_run: opts[:dry_run], verbose: opts[:verbose]) all_results << r else warn "[elect] 路径不存在: #{p}" end end Processor.report(all_results) # 有错误时退出码非零 exit 1 if all_results.any? { |r| r.status == :error } end |