-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (133 loc) · 4.49 KB
/
main.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Package main is the entry point for the timezone application.
// It retrieves the timezone for a given city name
// and displays the current local time for that timezone.
package main
import (
"flag"
"fmt"
"github.com/kritibb/ktz/cmd"
"os"
"strings"
)
// main is the entry point of the application.
// It expects command-line arguments in the form of ktz lookup [options] [city],
// retrieves the corresponding timezone, and displays the current local time in that timezone.
func main() {
//define subcommand `lookup` and its flags
lookupCmd := flag.NewFlagSet("lookup", flag.ExitOnError)
lookupC := lookupCmd.String("c", "", "country name/code like `Nepal` or `NP`")
lookupZ := lookupCmd.String("z", "", "`timezones` like `Asia/Kathmandu` or `PST`")
//define subcommand `lookup` and its flags
helpCmd := flag.NewFlagSet("help", flag.ExitOnError)
// Check if subcommands like "lookup" is provided
if len(os.Args) < 2 {
printError("", "\n Error: Incomplete command")
return
}
switch os.Args[1] {
case "lookup":
lookupCmd.Parse(os.Args[2:])
//no flags or positional argument provided
if *lookupZ == "" && *lookupC == "" && len(lookupCmd.Args()) == 0 {
printError("lookup", "\n Error: Incomplete command")
return
}
// Invalid combination: more than one flag or both flags and positional argument
if (*lookupZ != "" && *lookupC != "") ||
(*lookupZ != "" && len(lookupCmd.Args()) != 0) ||
(*lookupC != "" && len(lookupCmd.Args()) != 0) {
printError("lookup", "\nError: Use only a flag [-z] or [-c] or <city>")
return
}
//handle -z flag
if *lookupZ != "" {
cmd.ResolveTimezone("", "", *lookupZ)
} else if *lookupC != "" { //handle -c flag
cmd.ResolveTimezone("", *lookupC, "")
} else if len(lookupCmd.Args()) != 0 {
// combine all non-flag arguments to create a city name
city := strings.Join(lookupCmd.Args(), " ")
// Display the current local time based on the given city
cmd.ResolveTimezone(city, "", "")
}
case "help":
helpCmd.Parse(os.Args[2:])
printLookupHelp()
default:
printError("", "\n Error: Unknown command")
}
// switch os.Args[1] {
// case "add", "lookup":
// if len(os.Args) < 3 {
// if os.Args[1] == "lookup" {
// printLookupHelp()
// return
// } else if os.Args[1] == "add" {
// printError("Error: Incomplete command")
// return
// }
// }
// city := strings.Join(os.Args[2:], " ")
// if os.Args[1] == "lookup" {
// // Display the current local time based on the given city
// cmd.DisplayTime(city)
// } else if os.Args[1] == "add" {
// // Add tz to local storage ~/.ktz_cli
// fmt.Println("Added tz", city)
// }
// case "help":
// fmt.Println("Show help")
// return
// case "view":
// fmt.Println("Show all saved timezones")
// return
// default:
// printError("Error: Unknown command")
// }
}
func printError(errCmd, errMsg string) {
fmt.Println(errMsg)
switch errCmd {
case "lookup":
fmt.Println(" Usage: ktz lookup [options] <city>")
default:
}
fmt.Println(" For more information, try 'ktz help'")
fmt.Println("")
}
func printLookupHelp() {
fmt.Println("Usage: ktz lookup [options] <city>")
fmt.Println()
fmt.Println("Look up the current time for a city,zone or country")
fmt.Println()
fmt.Println("Options:")
fmt.Println(" -z string Specify a timezone")
fmt.Println(" -c string Specify a country name or alpha2/alpha3 code")
fmt.Println()
fmt.Println("Examples:")
fmt.Println(" ktz lookup \"New York\"")
fmt.Println(" ktz lookup -z=America/New_York or -z=PST")
fmt.Println(" ktz lookup -c=NP or -c=Nepal")
}
/////////////////////////////////////Completed///////////////////////////
// ktz lookup -z="America/New_York"/ "pst"
// ktz lookup -c="usa"/
// ktz lookup nepal
// ktz lookup kathmandu
//edit country names to autonomous regions, and change the country in the cityTOIANA as well.
//search country by 2-3 country code
//add more command like ktz help
//////////////////////////////////// TODO ////////////////////////////
//write tests
//////////////////////////////////// Future ////////////////////////////
//ktz add -tz="America/New_York" --- only add the tz you know and store in local storage
//ktz remove -tz="America/New_York" --- only remove the tz you know
//ktz remove -interactive mode
//ktz view-all
// Usage: myapp.py [OPTIONS] [THINGS]...
// Combines things into a single element
// Options:
// -t TEXT Data type [default: int]
// -o TEXT Output format
// --help Show this message and exit.
//replace fuzzy with prefix