File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+
3+ import argparse
4+ import os
5+ import sys
6+
7+ parser = argparse .ArgumentParser (
8+ prog = 'ls' ,
9+ description = 'CLI tool to list contents of a directory'
10+ )
11+
12+ parser .add_argument ('-1' , '--one' , action = 'store_true' , help = 'Force output to be entry per line' )
13+ parser .add_argument ('-a' ,'--all' , action = 'store_true' , help = 'Include hidden files' )
14+ parser .add_argument ('path' , nargs = '?' , default = '.' , help = 'Directories to list' )
15+
16+ args = parser .parse_args ()
17+
18+ try :
19+ entries = os .listdir (args .path )
20+ # print(entries)
21+ if not args .all :
22+ entries = [entry for entry in entries if not entry .startswith ('.' )]
23+ entries .sort (key = str .lower )
24+ # print(entries)
25+
26+ if args .one :
27+ for entry in entries :
28+ print (entry )
29+
30+ else :
31+ print (f" " .join (entries ))
32+
33+ except FileNotFoundError :
34+ print (f"ls: No such file or directory " )
You can’t perform that action at this time.
0 commit comments