File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -3,11 +3,36 @@ package main
3
3
import (
4
4
"flag"
5
5
"fmt"
6
+ "os"
6
7
)
7
8
8
9
func main () {
10
+
11
+ flag .Usage = func () {
12
+ fmt .Fprintf (os .Stderr , "Usage: filecommander <command> <args>\n " )
13
+ fmt .Fprintf (os .Stderr , "\n Commands:\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 , "\n Options:\n " )
23
+ flag .PrintDefaults ()
24
+ }
25
+
26
+ // Define flags
27
+ help := flag .Bool ("help" , false , "Show help" )
28
+
9
29
flag .Parse ()
10
30
31
+ if * help {
32
+ flag .Usage ()
33
+ return
34
+ }
35
+
11
36
args := flag .Args ()
12
37
if len (args ) < 1 {
13
38
fmt .Println ("Usage: filecommander <command> <args>" )
Original file line number Diff line number Diff line change @@ -8,6 +8,24 @@ import (
8
8
)
9
9
10
10
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
11
29
file , err := os .Create (filename )
12
30
if err != nil {
13
31
fmt .Println ("Error creating file:" , err )
You can’t perform that action at this time.
0 commit comments