ls: "-a" option added.

This commit is contained in:
k1574 2020-11-25 04:00:35 +05:00
parent 647d875555
commit f7fb263f8b

View file

@ -5,6 +5,11 @@ import(
"flag" "flag"
"strings" "strings"
"regexp" "regexp"
"path"
)
var(
listHidden bool
) )
func IsDir(p string) (bool, error) { func IsDir(p string) (bool, error) {
@ -39,7 +44,17 @@ func Stat(p string) (os.FileInfo, error) {
return s, nil return s, nil
} }
func shouldList(p string) bool {
if !listHidden && path.Base(p)[0]=='.' {
return false
}
return true
}
func ls(p string, fold int) error { func ls(p string, fold int) error {
if !shouldList(p) {
return nil
}
isDir, e := IsDir(p) isDir, e := IsDir(p)
if e != nil { if e != nil {
return e return e
@ -86,6 +101,7 @@ func Run(args []string) int {
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
var foldLvl int var foldLvl int
flagSet.IntVar(&foldLvl, "r", 1, "List recursively with choosing deepness, can't be negative or zero.") flagSet.IntVar(&foldLvl, "r", 1, "List recursively with choosing deepness, can't be negative or zero.")
flagSet.BoolVar(&listHidden, "a", false, "List hidden files.")
flagSet.Usage = func() { flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: %s [files]\n", arg0, arg0) fmt.Fprintf(os.Stderr, "Usage of %s: %s [files]\n", arg0, arg0)
flagSet.PrintDefaults() flagSet.PrintDefaults()
@ -110,6 +126,9 @@ func Run(args []string) int {
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e) fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
} else { } else {
for _, f := range l { for _, f := range l {
if !shouldList(f.Name()) {
continue
}
isDir, _ := IsDir(f.Name()) isDir, _ := IsDir(f.Name())
fmt.Println(f.Name()) fmt.Println(f.Name())