github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/arg_usage.go (about) 1 /* 2 Golang To Github Markdown Utility: gotomd 3 Copyright (C) 2023, 2024 Leslie Dancsecs 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <https://www.gnu.org/licenses/>. 17 */ 18 19 package main 20 21 import ( 22 "flag" 23 "fmt" 24 "os" 25 "strings" 26 ) 27 28 const defaultPermissions = 0o0644 29 30 //nolint:goCheckNoGlobals // Ok. 31 var ( 32 cleanOnly = false 33 forceOverwrite = false 34 replace = false 35 verbose = false 36 szColorize = false 37 outputDir = "." 38 defaultPerm = defaultPermissions 39 showLicense = false 40 ) 41 42 func usage() { 43 cmdName := os.Args[0] 44 fmt.Fprint(flag.CommandLine.Output(), 45 "Usage of ", cmdName, 46 " [-c | -r]"+ 47 " [-f]"+ 48 " [-v]"+ 49 " [-z]"+ 50 " [-p perm]"+ 51 " [-o outDir]"+ 52 " file|dir"+ 53 " [file|dir...]"+ 54 "\n", 55 ) 56 flag.PrintDefaults() 57 } 58 59 func captureFlagDefaults() string { 60 buf := strings.Builder{} 61 origOut := flag.CommandLine.Output() 62 63 defer func() { 64 flag.CommandLine.SetOutput(origOut) 65 }() 66 flag.CommandLine.SetOutput(&buf) 67 68 flag.CommandLine.Usage() 69 70 return buf.String() 71 } 72 73 func processArgs() { 74 flag.BoolVar(&verbose, "v", false, 75 "Provide more information when processing.", 76 ) 77 flag.BoolVar(&cleanOnly, "c", false, 78 "Reverse operation and remove generated markdown "+ 79 "(Cannot be used with the -r option).", 80 ) 81 flag.BoolVar(&replace, "r", false, 82 "Replace the *.MD in place (Cannot be used with the -c flag).", 83 ) 84 flag.BoolVar(&showLicense, "l", false, 85 "Display license before program exits.", 86 ) 87 flag.BoolVar(&forceOverwrite, "f", false, 88 "Do not confirm overwrite of destination.", 89 ) 90 flag.BoolVar(&szColorize, "z", false, 91 "Colorize go test output.", 92 ) 93 flag.StringVar(&outputDir, "o", ".", 94 "Direct all output to the specified directory.", 95 ) 96 flag.IntVar(&defaultPerm, "p", defaultPermissions, 97 "Permissions to use when creating new file"+ 98 " (can only set RW bits).", 99 ) 100 101 flag.CommandLine.Usage = usage 102 _ = flag.CommandLine.Parse(os.Args[1:]) 103 104 if flag.CommandLine.NArg() < 1 { 105 panic("at least one file or directory must be specified\n" + 106 captureFlagDefaults(), 107 ) 108 } 109 110 if defaultPerm&(^0o0666) != 0 { 111 panic("invalid default permissions specified\n" + 112 captureFlagDefaults(), 113 ) 114 } 115 116 if replace && cleanOnly { 117 panic("only one of -c and -r may be specified\n" + 118 captureFlagDefaults(), 119 ) 120 } 121 122 if outputDir != "." { 123 s, err := os.Stat(outputDir) 124 if err != nil || !s.IsDir() { 125 panic( 126 "invalid output directory specified: " + 127 outputDir + 128 "\n" + 129 captureFlagDefaults(), 130 ) 131 } 132 } 133 }