1
1
package main
2
2
3
3
import (
4
+ "flag"
4
5
"fmt"
5
6
"os"
6
7
)
7
8
8
9
func main () {
9
- if len (os .Args ) < 2 {
10
- fmt .Println ("Please provide a filename" )
10
+ var output = flag .String ("output" , "" , "Output to a specific file (default: add .dfu suffix)" )
11
+ var debug = flag .Bool ("debug" , false , "Enable debugging mode" )
12
+ var linked = flag .Bool ("prelinked" , false , "Provided file has already been linked to Zephyr" )
13
+
14
+ flag .Parse ()
15
+ if flag .NArg () != 1 {
16
+ fmt .Printf ("Usage: %s [flags] <filename>\n " , os .Args [0 ])
17
+ flag .PrintDefaults ()
11
18
return
12
19
}
13
-
14
- filename := os .Args [1 ]
15
- debug := 0
16
- linked := 0
17
-
18
- if len (os .Args ) >= 3 {
19
- if os .Args [2 ] == "debug" {
20
- debug = 1
21
- }
22
- if os .Args [2 ] == "linked" {
23
- linked = 1
24
- }
25
- }
20
+ filename := flag .Arg (0 )
26
21
27
22
// Read the file content
28
23
content , err := os .ReadFile (filename )
@@ -34,17 +29,20 @@ func main() {
34
29
// Get the length of the file content
35
30
length := len (content )
36
31
37
- // Create a new filename for the copy
38
- newFilename := filename + ".dfu"
39
-
40
32
// Create the new content with the length in front
41
33
len_str := fmt .Sprintf ("%d" , length )
42
- newContent := append ([]byte (len_str ), 0 , byte (debug ), byte (linked ))
34
+ newContent := append ([]byte (len_str ), 0 , byte (* debug ), byte (* linked ))
43
35
// make newContent 16 bytes
44
36
tmp := make ([]byte , 16 - len (newContent ))
45
37
newContent = append (newContent , tmp ... )
46
38
newContent = append (newContent , content ... )
47
39
40
+ // Create a new filename for the copy
41
+ newFilename := * output
42
+ if newFilename == "" {
43
+ newFilename = filename + ".dfu"
44
+ }
45
+
48
46
// Write the new content to the new file
49
47
err = os .WriteFile (newFilename , []byte (newContent ), 0644 )
50
48
if err != nil {
@@ -58,5 +56,5 @@ func main() {
58
56
return
59
57
}
60
58
61
- fmt .Printf ("File copied and saved as %s\n " , newFilename )
59
+ fmt .Printf ("File %s saved as %s\n " , filename , newFilename )
62
60
}
0 commit comments