diff --git a/README.md b/README.md index e6eec84..a2173bd 100644 --- a/README.md +++ b/README.md @@ -46,25 +46,25 @@ $ which aar Creating an archive: ```bash -$ aar -c -f archive.aarch file1.txt file2.txt file3.txt +$ aar create -f archive.aarch file1.txt file2.txt file3.txt ``` Extracting all files an archive: ```bash -$ aar -x -f archive.aarch +$ aar extract -f archive.aarch ``` Extracting a single file by name from an archive: ```bash -$ aar -x -f archive.aarch -n file2.txt +$ aar extract -f archive.aarch -n file2.txt ``` Listing the contents of an archive: ```bash -$ aar -l -f archive.aarch +$ aar list -f archive.aarch ``` ## File Format diff --git a/cmd/aar/main.go b/cmd/aar/main.go index 47dafed..c27bd26 100644 --- a/cmd/aar/main.go +++ b/cmd/aar/main.go @@ -8,75 +8,64 @@ import ( "github.com/angelsolaorbaiceta/aar/cmd" ) -var ( - createFlag = flag.Bool("c", false, "Create a new archive") - extractFlag = flag.Bool("x", false, "Extract an archive") - extractNameFlag = flag.String("n", "", "Extract a specific file by name from the archive") - listFlag = flag.Bool("l", false, "List the contents of an archive") - fileName = flag.String("f", "", "Output filename of the archive") -) - func main() { - flag.Parse() - validateFlags() + var ( + createCmd = flag.NewFlagSet("create", flag.ExitOnError) + createFileNameFlag = createCmd.String("f", "", "Output filename of the archive") + + extractCmd = flag.NewFlagSet("extract", flag.ExitOnError) + extractFileNameFlag = extractCmd.String("f", "", "Filename of the archive to extract") + extractNameFlag = extractCmd.String("n", "", "Extract a specific file by name from the archive") - if *createFlag { - createArchive() - } else if *extractFlag { - if *extractNameFlag != "" { - extractArchiveFile(*extractNameFlag) + listCmd = flag.NewFlagSet("list", flag.ExitOnError) + listFileNameFlag = listCmd.String("f", "", "Filename of the archive to list") + ) + + if len(os.Args) < 2 { + fmt.Fprintf(os.Stderr, "Usage: aar [options]\n") + os.Exit(1) + } + + switch os.Args[1] { + case "create": + createCmd.Parse(os.Args[2:]) + validateFileName(*createFileNameFlag) + fileNames := createCmd.Args() + createArchive(*createFileNameFlag, fileNames) + + case "extract": + extractCmd.Parse(os.Args[2:]) + validateFileName(*extractFileNameFlag) + + if *extractNameFlag == "" { + cmd.ExtractArchive(*extractFileNameFlag) } else { - extractArchive() + cmd.ExtractArchiveFile(*extractFileNameFlag, *extractNameFlag) } - } else if *listFlag { - listArchive() - } else { - fmt.Println("Usage: aar [options] -f ") - flag.PrintDefaults() - } -} -func validateFlags() { - activeFlags := 0 - if *createFlag { - activeFlags++ - } - if *extractFlag { - activeFlags++ - } - if *listFlag { - activeFlags++ - } + case "list": + listCmd.Parse(os.Args[2:]) + validateFileName(*listFileNameFlag) + cmd.ListArchive(*listFileNameFlag) - if activeFlags != 1 { - fmt.Fprintf(os.Stderr, "You must specify one of the -c (create), -x (extract), or -l (list) flags.\n") + default: + fmt.Fprintf(os.Stderr, "Usage: aar [options]\n") os.Exit(1) } +} - if *fileName == "" { +func validateFileName(name string) { + if name == "" { fmt.Fprintf(os.Stderr, "You must specify a filename with the -f flag.\n") os.Exit(1) } } -func createArchive() { - fileNames := flag.Args() +func createArchive(fileName string, fileNames []string) { if len(fileNames) == 0 { fmt.Fprintf(os.Stderr, "You must specify at least one file to add to the archive.\n") os.Exit(1) } - cmd.CreateArchive(*fileName, fileNames) -} - -func extractArchive() { - cmd.ExtractArchive(*fileName) -} - -func extractArchiveFile(fileToExtract string) { - cmd.ExtractArchiveFile(*fileName, fileToExtract) -} - -func listArchive() { - cmd.ListArchive(*fileName) + cmd.CreateArchive(fileName, fileNames) }