refactor: simplify the main func

This commit is contained in:
Dan Anglin 2024-01-08 10:37:09 +00:00
parent 36a716eefe
commit 57782b0903
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 9 additions and 15 deletions

View file

@ -50,7 +50,7 @@ D - Delta
Reveal the code words for all letters of the alphabet: Reveal the code words for all letters of the alphabet:
[source,console] [source,console]
---- ----
$ ./phonetic-letters --all $ phonetic-letters --all
A - Alpha A - Alpha
B - Bravo B - Bravo
C - Charlie C - Charlie

View file

@ -3,7 +3,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
"strings" "strings"
) )
@ -13,27 +12,21 @@ func main() {
flag.BoolVar(&all, "all", false, "reveal the code words of all the letters in the English alphabet.") flag.BoolVar(&all, "all", false, "reveal the code words of all the letters in the English alphabet.")
flag.Parse() flag.Parse()
var input string
if all { if all {
fmt.Println(phoneticLetters(fullAlphabet())) input = fullAlphabet()
return
}
args := flag.Args()
if len(args) < 1 {
fmt.Println("ERROR: expected an argument")
os.Exit(1)
} else if len(args) == 1 {
fmt.Println(phoneticLetters(args[0]))
} else { } else {
input := "" args := flag.Args()
for i := range args { for i := range args {
input = input + args[i] input = input + args[i]
} }
fmt.Println(phoneticLetters(input))
}
} }
fmt.Print(phoneticLetters(input))
}
// phoneticLetters returns a string of all the code words for the given set of letters.
func phoneticLetters(letters string) string { func phoneticLetters(letters string) string {
letters = strings.ToLower(letters) letters = strings.ToLower(letters)
@ -99,6 +92,7 @@ func phoneticLetters(letters string) string {
return b.String() return b.String()
} }
// fullAlphabet returns a string of all the letters in the alphabet.
func fullAlphabet() string { func fullAlphabet() string {
return "a b c d e f g h i j k l m n o p q r s t u v w x y z" return "a b c d e f g h i j k l m n o p q r s t u v w x y z"
} }