Skip to content

Commit 808756c

Browse files
committed
Added --help and creating already existing file
1 parent 493f4e9 commit 808756c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,36 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"os"
67
)
78

89
func main() {
10+
11+
flag.Usage = func() {
12+
fmt.Fprintf(os.Stderr, "Usage: filecommander <command> <args>\n")
13+
fmt.Fprintf(os.Stderr, "\nCommands:\n")
14+
fmt.Fprintf(os.Stderr, " create <filename> Create a new file\n")
15+
fmt.Fprintf(os.Stderr, " read <filename> Read the contents of a file\n")
16+
fmt.Fprintf(os.Stderr, " write <filename> <content> Write content to a file\n")
17+
fmt.Fprintf(os.Stderr, " delete <filename> Delete a file\n")
18+
fmt.Fprintf(os.Stderr, " list <directory> List files in a directory\n")
19+
fmt.Fprintf(os.Stderr, " copy <srcfile> <destfile> Copy a file to a new location\n")
20+
fmt.Fprintf(os.Stderr, " move <srcfile> <destfile> Move a file to a new location\n")
21+
fmt.Fprintf(os.Stderr, " search <directory> <filename> Search for a file in a directory\n")
22+
fmt.Fprintf(os.Stderr, "\nOptions:\n")
23+
flag.PrintDefaults()
24+
}
25+
26+
// Define flags
27+
help := flag.Bool("help", false, "Show help")
28+
929
flag.Parse()
1030

31+
if *help {
32+
flag.Usage()
33+
return
34+
}
35+
1136
args := flag.Args()
1237
if len(args) < 1 {
1338
fmt.Println("Usage: filecommander <command> <args>")

utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ import (
88
)
99

1010
func createFile(filename string) {
11+
if _, err := os.Stat(filename); err == nil {
12+
// File exists, prompt user to overwrite
13+
answer := ""
14+
for answer != "y" && answer != "n" {
15+
fmt.Print("File already exists. Do you want to overwrite it? (y/n): ")
16+
_, err := fmt.Scanln(&answer)
17+
if err != nil {
18+
fmt.Println(err)
19+
return
20+
}
21+
}
22+
23+
if answer == "n" {
24+
return
25+
}
26+
}
27+
28+
// Create the file
1129
file, err := os.Create(filename)
1230
if err != nil {
1331
fmt.Println("Error creating file:", err)

0 commit comments

Comments
 (0)