From 57782b09039cdc06c15e790da4b499a40adda9ce Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 8 Jan 2024 10:37:09 +0000 Subject: [PATCH] refactor: simplify the main func --- README.asciidoc | 2 +- phonetic_letters.go | 22 ++++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index 083c590..fc6d077 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -50,7 +50,7 @@ D - Delta Reveal the code words for all letters of the alphabet: [source,console] ---- -$ ./phonetic-letters --all +$ phonetic-letters --all A - Alpha B - Bravo C - Charlie diff --git a/phonetic_letters.go b/phonetic_letters.go index d5d3cd3..818e4e6 100644 --- a/phonetic_letters.go +++ b/phonetic_letters.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "os" "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.Parse() + var input string + if all { - fmt.Println(phoneticLetters(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])) + input = fullAlphabet() } else { - input := "" + args := flag.Args() for i := range args { 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 { letters = strings.ToLower(letters) @@ -99,6 +92,7 @@ func phoneticLetters(letters string) string { return b.String() } +// fullAlphabet returns a string of all the letters in the alphabet. 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" }