Skip to content

Commit e44078e

Browse files
SaraTahir28cyf
authored andcommitted
Adding implementation for -a flag
1 parent 1bcd513 commit e44078e

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@ program
66
.name("ls")
77
.description("list directory contents")
88
.option("-1, --one", "Outputs are printed one entry per line")
9+
.option("-a, --all","Show all files including hidden files that start with a .")
910
.argument("[directory]", "Directory to list", "."); // "." means current directory
1011

1112
//interpret the program
1213
program.parse();
14+
const options = program.opts();
1315
const directory = program.args[0] || "."; //get dir arg- 1st arg in program.args array || if no arg default to current dir
1416

1517

16-
const files = await fs.readdir(directory); //read the dir to get array of filenames
18+
let files = await fs.readdir(directory); //read the dir to get array of filenames
1719

1820

21+
if (!options.all) { //Handle -a (include hidden files)
22+
files = files.filter(name => !name.startsWith("."));
23+
}
1924

20-
//print each file on its own line
21-
for (const file of files) { // Loop prints each individually on separate lines
22-
console.log(file)
25+
if (options.one) { // Print each file on its own line
26+
for (const file of files) {
27+
console.log(file);
28+
}
29+
}
30+
else {
31+
console.log(files.join(" "));// Default: join with spaces (like ls without -1)
2332
}

0 commit comments

Comments
 (0)