-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (41 loc) · 1.02 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
package main
import (
"flag"
"fmt"
"os"
"github.com/alefeans/huffman/internal/huffman"
)
type Args struct {
inputFile string
outputFile string
decompress bool
}
func parseCliArgs() *Args {
var args Args
flag.StringVar(&args.inputFile, "f", "", "The name of the input file to be compressed")
flag.StringVar(&args.outputFile, "o", "", "The name of the output compressed file")
flag.BoolVar(&args.decompress, "d", false, "If set, input file will be decompressed to the output file")
flag.Parse()
return &args
}
func main() {
args := parseCliArgs()
if args.inputFile == "" || args.outputFile == "" {
fmt.Println("argument is missing")
flag.Usage()
os.Exit(1)
}
if args.decompress {
err := huffman.NewDecompressor(args.inputFile, args.outputFile).Decompress()
if err != nil {
fmt.Printf("can't decompress file: %v\n", err)
os.Exit(1)
}
} else {
err := huffman.NewCompressor(args.inputFile, args.outputFile).Compress()
if err != nil {
fmt.Printf("can't compress file: %v\n", err)
os.Exit(1)
}
}
}