From c01ae28b71bc8cf68395815b4f805a76548c15af Mon Sep 17 00:00:00 2001 From: Yash-Handa Date: Mon, 24 Aug 2020 07:28:35 +0530 Subject: [PATCH] Added flag constant bits and version info --- .vscode/settings.json | 5 ++++ HELP.md | 2 +- main.go | 53 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..551fc56 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "go.lintFlags": [ + "\"-min_confidence=.9\"" + ] +} \ No newline at end of file diff --git a/HELP.md b/HELP.md index e7c2f8c..a9ac7c0 100644 --- a/HELP.md +++ b/HELP.md @@ -1,5 +1,5 @@ ```txt -Usage: logo-ls [-1?aAdgGhlorSstUvVX] [parameters ...] +Usage: logo-ls [-1?aAdgGhlorSstUvVX] [files ...] -1 list one file per line. -? display this help and exit -a, --all do not ignore entries starting with . diff --git a/main.go b/main.go index 8b38c5a..02ace1d 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,36 @@ package main import ( + "fmt" + "log" "os" "github.com/pborman/getopt/v2" ) +// flags with corresponding bit values +// frequently used flags should be higher in the list +// help (-?) and version (-V) not included +const ( + flag_a uint = 1 << iota + flag_l + flag_alpha // sort in alphabetic order (default) + flag_A + flag_h + flag_1 + flag_d + flag_s + flag_r + flag_S + flag_t + flag_X + flag_v + flag_U + flag_o + flag_g + flag_G +) + func main() { // content flags _ = getopt.BoolLong("all", 'a', "do not ignore entries starting with .") @@ -30,12 +55,32 @@ func main() { _ = getopt.BoolLong("reverse", 'r', "reverse order while sorting") - helpFlag := getopt.Bool('?', "display this help and exit") - _ = getopt.BoolLong("version", 'V', "output version information and exit") + f_help := getopt.Bool('?', "display this help and exit") + f_V := getopt.BoolLong("version", 'V', "output version information and exit") - getopt.ParseV2() - if *helpFlag { + // using getopt.Getopt instead of parse to provide custom err + err := getopt.Getopt(nil) + if err != nil { + // code to handle error + log.Printf("%v\nTry 'logo-ls -?' for more information.", err) + os.Exit(2) + } + + // if f_help is provided print help and exit(0) + if *f_help { getopt.PrintUsage(os.Stdout) + os.Exit(0) + } + // if f_V is provided version will be printed and exit(0) + if *f_V { + fmt.Printf("logo-ls %s\nCopyright (c) 2020 Yash Handa\nLicense MIT .\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\n", "v1.0.0") + os.Exit(0) } } + +func init() { + getopt.SetParameters("[files ...]") + log.SetPrefix("logo-ls: ") + log.SetFlags(0) +}