1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| pflag.IntVarP(&(args.startPage), "startPage", "s", -1, "Define startPage") pflag.IntVarP(&(args.endPage), "endPage", "e", -1, "Define endPage") pflag.IntVarP(&(args.pageLen), "pageLength", "l", 72, "Define pageLength") pflag.StringVarP(&(args.printDest), "printDest", "d", "", "Define printDest") pflag.BoolVarP(&(args.pageType), "pageType", "f", false, "Define pageType") ``` ### 检查变量和参数名 ```go func checkArgs(args *selpgArgs) { if len(os.Args) < 3 { fmt.Fprintf(os.Stderr, "\n[Error]The arguments are not enough!\n") pflag.Usage() os.Exit(1) } else if (args.startPage == -1) || (args.endPage == -1) { fmt.Fprintf(os.Stderr, "\n[Error]The startPage and endPage can't be empty! Please check your command!\n") pflag.Usage() os.Exit(2) } else if (args.startPage <= 0) || (args.endPage <= 0) { fmt.Fprintf(os.Stderr, "\n[Error]The startPage and endPage can't be negative! Please check your command!\n") pflag.Usage() os.Exit(3) } else if args.startPage > args.endPage { fmt.Fprintf(os.Stderr, "\n[Error]The startPage can't be bigger than the endPage! Please check your command!\n") pflag.Usage() os.Exit(4) } else if (args.pageType == true) && (args.pageLen != 72) { fmt.Fprintf(os.Stderr, "\n[Error]The command -l and -f are exclusive, you can't use them together!\n") pflag.Usage() os.Exit(5) } else if args.pageLen <= 0 { fmt.Fprintf(os.Stderr, "\n[Error]The pageLen can't be less than 1 ! Please check your command!\n") pflag.Usage() os.Exit(6) } else { pageType := "page length." if args.pageType == true { pageType = "The end sign /f." } fmt.Printf("\n[ArgsStart]\n") fmt.Printf("startPage: %d\nendPage: %d\ninputFile: %s\npageLength: %d\npageType: %s\nprintDestation: %s\n[ArgsEnd]", args.startPage, args.endPage, args.inFileName, args.pageLen, pageType, args.printDest) }
}
|