Moved flag-checking after flag.Parse()

master
Aadhavan Srinivasan 1 month ago
parent e882f41400
commit 1a1a8f4f9c

@ -15,6 +15,8 @@ import (
const CONCAT rune = '~' const CONCAT rune = '~'
var notDotChars []rune
func isOperator(c rune) bool { func isOperator(c rune) bool {
if c == '+' || c == '?' || c == '*' || c == '|' || c == CONCAT { if c == '+' || c == '?' || c == '*' || c == '|' || c == CONCAT {
return true return true
@ -388,11 +390,17 @@ func main() {
// 1. Without '-v': Prints only matches. Prints a newline after every match. // 1. Without '-v': Prints only matches. Prints a newline after every match.
// 2. With '-v': Substitutes all matches with empty string. // 2. With '-v': Substitutes all matches with empty string.
onlyFlag := flag.Bool("o", false, "Print only colored content. Overrides -l.") onlyFlag := flag.Bool("o", false, "Print only colored content. Overrides -l.")
lineFlag := flag.Bool("l", false, "Only print lines with a match (or with no matches, if -v is enabled (similar to grep's default") lineFlag := flag.Bool("l", false, "Only print lines with a match (or with no matches, if -v is enabled). Similar to grep's default.")
multiLineFLag := flag.Bool("t", false, "Multi-line mode. Treats newline just like any character.") multiLineFlag := flag.Bool("t", false, "Multi-line mode. Treats newline just like any character.")
printMatchesFlag := flag.Bool("p", false, "Prints start and end index of each match. Can only be used with '-t' for multi-line mode.") printMatchesFlag := flag.Bool("p", false, "Prints start and end index of each match. Can only be used with '-t' for multi-line mode.")
flag.Parse() flag.Parse()
// In multi-line mode, 'dot' metacharacter also matches newline
if !(*multiLineFlag) {
notDotChars = []rune{'\n'}
} else {
notDotChars = []rune{}
}
// -l and -o are mutually exclusive: -o overrides -l // -l and -o are mutually exclusive: -o overrides -l
if *onlyFlag { if *onlyFlag {
*lineFlag = false *lineFlag = false
@ -424,7 +432,7 @@ func main() {
if linesRead { if linesRead {
break break
} }
if !(*multiLineFLag) { if !(*multiLineFlag) {
// Read every string from stdin until we encounter an error. If the error isn't EOF, panic. // Read every string from stdin until we encounter an error. If the error isn't EOF, panic.
test_str, err = reader.ReadString('\n') test_str, err = reader.ReadString('\n')
if err != nil { if err != nil {

Loading…
Cancel
Save