golang flag的使用

package main

import (
	"backend-service-api-scripts/utils"
	"database/sql"
	"flag"
	"fmt"
	"log"
	"os"
)

var (
	db         *sql.DB = utils.ConnectData() // 这里的连接数据库需要根据数据库本身的情况进行更改
	courseId   int
	companyId  int
	outputFile string
	inputFile  string
	h          bool
)

func init() {
	flag.BoolVar(&h, "h", false, "帮助")
	flag.IntVar(&courseId, "c", 0, "(必传值)课程ID:\n\tA课:111\n\tB课:222\n\tC课:333")
	flag.IntVar(&companyId, "C", 0, "(必传值)企业ID")
	flag.StringVar(&inputFile, "i", "inputFile.txt", "需要查询的电话列表文件")
	flag.StringVar(&outputFile, "o", "outputFile.txt", "查询电话对应的班级号文件")

	flag.Usage = usage
}

func usage() {
	fmt.Fprintf(os.Stderr, `View class number by phone
Usage: viewClassNumber [-c courseId] [-C companyId] [-i inputFile] [-o outputFile]
Options:
`)
	flag.PrintDefaults()
}

type UserClassesInfo struct {
	ClasseNo sql.NullString
}

func getUserGroupId(phoneList <-chan string, courseId, companyId int) {
	for {
		phone, ok := <-phoneList
		if !ok {
			break
		}
		row := db.QueryRow("SELECT classes_no FROM classes.classes_info WHERE phone=? AND course_id=? AND company_id=?", phone, courseId, companyId)
		u := UserClassesInfo{}
		if err := row.Scan(&u.ClasseNo); err != nil {
			log.Println(err)
		}
		info := fmt.Sprintf("%v\t%v\n", phone, u.ClasseNo.String)
		utils.WriteFile(outputFile, &info)
	}
}

func main() {
	flag.Parse()
	if h || companyId == 0 || courseId == 0 {
		flag.Usage()
		return
	}
	phoneList := make(chan string, 500)
	utils.ReadFile(inputFile, phoneList)
	getUserGroupId(phoneList, courseId, companyId)
}

# 处理EXCEL脚本示例
package main

import (
	"flag"
	"fmt"
	"io"
	"net/http"
	"os"
	"regexp"
	"strings"

	logs "github.com/gofiber/fiber/v2/log"
	"github.com/xuri/excelize/v2"
)

var (
	processingType string
	filename       string
)

// 存储图片
func storageImage(imageUrl, path string) {
	if imageUrl == "" || imageUrl == "\r\n" || imageUrl == "\n" || imageUrl == "\t" {
		return
	}
	re := regexp.MustCompile(`[% ]+`)
	imageStatus := re.Split(imageUrl, -1)
	resp, err := http.Get(imageStatus[0])
	if err != nil {
		logs.Error("获取图片地址错误: ", err)
		return
	}
	imageInfo := strings.Split(resp.Request.URL.String(), "/")
	file, err := os.Create(path + "/" + imageInfo[len(imageInfo)-1])
	if err != nil {
		logs.Error("创建图片地址错误: ", path, err)
		return
	}
	_, err = io.Copy(file, resp.Body)
	if err != nil {
		logs.Error("存储图片地址错误: ", path, err)
		return
	}
	resp.Body.Close()
	file.Close()
}

// 处理照片
func processingPhoto(path, row string) {
	os.Mkdir(path, 0755)
	rows := strings.Split(row, "\r\n")
	if rows[0] == "" || rows[0] == "\r\n" {
		return
	}
	switch len(rows) {
	case 0:
		return
	case 1:
		storageImage(rows[0], path)
	default:
		for _, v := range rows {
			storageImage(v, path)
		}
	}
}

// 读取文档
func readFile() {
	f, err := excelize.OpenFile(filename)
	if err != nil {
		logs.Error("打开文件错误", err)
		return
	}
	defer func() {
		if err := f.Close(); err != nil {
			logs.Error("关闭文件错误", err)
			return
		}
	}()
	rows, err := f.GetRows("Sheet1")
	if err != nil {
		logs.Error("打开Sheet页错误", err)
		return
	}
	if processingType == "com" || processingType == "family" {
		for _, row := range rows {
			if row[0] == "序号" {
				continue
			}
			familyDir := "./家庭照片/" + row[1]
			companyDir := "./企业照片/" + row[1]
			processingPhoto(familyDir, row[2])
			processingPhoto(companyDir, row[3])
		}
	}
	if processingType == "logo" {
		for _, row := range rows {
			if row[0] == "序号" {
				continue
			}
			logoDir := "./企业LOGO/" + row[1]
			processingPhoto(logoDir, row[2])
		}
	}
}

func usage() {
	fmt.Fprintf(os.Stderr, `Processing photo
Usage: processingPhoto -t [processingType] -f [filename]
Options:
`)
	// flag.PrintDefaults()
	fmt.Println("    -t", flag.Lookup("t").Usage)
	fmt.Println("    -f", flag.Lookup("f").Usage)
}

func init() {
	flag.StringVar(&processingType, "t", "", "\t需要处理图片类型(processingType):\n\tcom | family\t处理家庭照片和企业照片\n\tlogo\t处理企业logo照片\n")
	flag.StringVar(&filename, "f", "", "\t(filename)需要处理的文件名称")
	flag.Usage = usage
	// 创建家庭照片目录
	os.Mkdir("./家庭照片", 0755)
	// 创建企业照片目录
	os.Mkdir("./企业照片", 0755)
	// 创建企业LOGO目录
	os.Mkdir("./企业LOGO", 0755)
}
func main() {
	flag.Parse()
	if processingType == "" || filename == "" {
		flag.Usage()
		return
	}
	readFile()
}
posted @ 2023-10-31 09:46  昌慶  阅读(26)  评论(0编辑  收藏  举报