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+ import argparse
2+ import sys
3+ import os
4+
5+ parser = argparse .ArgumentParser (
6+ prog = "ls" ,
7+ description = "List directory contents"
8+ )
9+
10+ parser .add_argument (
11+ "-1" ,
12+ "--format" ,
13+ action = "store_true" ,
14+ help = "List one file per line"
15+ )
16+
17+ parser .add_argument (
18+ "-a" ,
19+ "--all" ,
20+ action = "store_true" ,
21+ help = "Do not ignore entries starting with ."
22+ )
23+
24+ parser .add_argument (
25+ "paths" ,
26+ nargs = "*" ,
27+ help = "The file to print"
28+ )
29+
30+ args = parser .parse_args ()
31+
32+ if args .format :
33+ separator = "\n "
34+ else :
35+ separator = " "
36+
37+ multiple_paths = len (args .paths ) > 1
38+
39+ file_paths = []
40+ directory_paths = []
41+
42+ if len (args .paths ) == 0 :
43+ args .paths .append ("." )
44+
45+ for path in args .paths :
46+ if os .path .isfile (path ):
47+ file_paths .append (path )
48+
49+ elif os .path .isdir (path ):
50+ directory_paths .append (path )
51+
52+ if len (file_paths ) > 0 :
53+ sys .stdout .write (separator .join (file_paths ) + "\n " )
54+
55+ for path in directory_paths :
56+ if multiple_paths :
57+ sys .stdout .write ("\n " + path + ":\n " )
58+
59+ entries = os .listdir (path )
60+ entries .sort ()
61+
62+ if args .all :
63+ entries = ["." , ".." ] + entries
64+
65+ else :
66+ visible_entries = []
67+
68+ for entry in entries :
69+ if not entry .startswith ("." ):
70+ visible_entries .append (entry )
71+
72+ entries = visible_entries
73+
74+ sys .stdout .write (separator .join (entries ) + "\n " )
You can’t perform that action at this time.
0 commit comments