Skip to content

Commit 1a27139

Browse files
committed
mode switching arg parser
1 parent 5e36e1f commit 1a27139

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@ const args = process.argv.slice(2);
66
const flags = new Set();
77
let paths = [];
88

9-
for (const arg of args) {
10-
if (arg.startsWith("-") && arg !== "-") {
9+
let isFlag = true;
10+
for (let i = 0; i < args.length; i++) {
11+
if (isFlag && args[i] === "--") {
12+
isFlag = false;
13+
} else if (isFlag && args[i].startsWith("-") && args[i] !== "-") {
1114
// capture the flags without the -
1215
// supports combined flags like -1a
13-
for (const ch of arg.slice(1)) {
16+
for (const ch of args[i].slice(1)) {
1417
flags.add(ch);
1518
}
1619
} else {
17-
paths.push(arg);
20+
paths.push(args[i]);
1821
}
1922
}
2023

24+
console.log(paths);
25+
2126
if (paths.length === 0) {
2227
paths.push(".");
2328
}

0 commit comments

Comments
 (0)