Skip to content

Commit 0e931ba

Browse files
committed
wip
1 parent c09f57f commit 0e931ba

File tree

7 files changed

+561
-6
lines changed

7 files changed

+561
-6
lines changed

src/clis/ls/main.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
#include <karm-cli/args.h>
12
#include <karm-sys/dir.h>
23
#include <karm-sys/entry.h>
34

4-
Async::Task<> entryPointAsync(Sys::Context &) {
5+
Async::Task<> entryPointAsync(Sys::Context &ctx) {
6+
auto allFlag = Cli::flag('a', "all"s, "Do not ignore entries starting with ."s);
7+
8+
Cli::Command cmd{
9+
"ls"s,
10+
NONE,
11+
"List directory contents."s,
12+
{allFlag}
13+
};
14+
15+
co_trya$(cmd.execAsync(ctx));
16+
17+
if (not cmd)
18+
co_return Ok();
19+
520
auto url = co_try$(Mime::parseUrlOrPath("."));
621
auto dir = co_try$(Sys::Dir::open(url));
7-
for (auto const &entry : dir.entries())
22+
23+
for (auto const &entry : dir.entries()) {
24+
if (!allFlag && entry.name[0] == '.')
25+
continue;
826
Sys::println(entry.name);
27+
}
28+
929
co_return Ok();
1030
}

src/clis/ls/manifest.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"type": "exe",
55
"description": "The ls coreutility",
66
"requires": [
7+
"karm-cli",
78
"karm-sys"
89
]
910
}

src/libs/karm-cli/args.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "args.h"
2+
3+
namespace Karm::Cli {
4+
5+
// MARK: Tokenizer -------------------------------------------------------------
6+
7+
void tokenize(Str arg, Vec<Token> &out) {
8+
if (arg == "--"s) {
9+
out.pushBack(Token::EXTRA);
10+
} else if (arg == "-"s) {
11+
out.pushBack("-"s);
12+
} else if (startWith(arg, "--"s) == Match::PARTIAL) {
13+
out.emplaceBack(Token::OPTION, next(arg, 2));
14+
} else if (startWith(arg, "-"s) == Match::PARTIAL) {
15+
Str flags = next(arg, 1);
16+
for (auto r : iterRunes(flags))
17+
out.emplaceBack(r);
18+
} else {
19+
out.pushBack(arg);
20+
}
21+
}
22+
23+
void tokenize(Slice<Str> args, Vec<Token> &out) {
24+
for (auto &arg : args)
25+
tokenize(arg, out);
26+
}
27+
28+
void tokenize(int argc, char **argv, Vec<Token> &out) {
29+
for (int i = 0; i < argc; ++i)
30+
tokenize(Str::fromNullterminated(argv[i]), out);
31+
}
32+
33+
// MARK: Values ----------------------------------------------------------------
34+
35+
void ValueParser<bool>::usage(Io::Emit &e) {
36+
e("true|false");
37+
}
38+
39+
Res<bool> ValueParser<bool>::parse(Cursor<Token> &) {
40+
return Ok(true);
41+
}
42+
43+
void ValueParser<i32>::usage(Io::Emit &e) {
44+
e("integer");
45+
}
46+
47+
Res<i32> ValueParser<i32>::parse(Cursor<Token> &c) {
48+
if (c.ended() or c->kind != Token::OPERAND)
49+
return Error::other("missing value");
50+
51+
auto value = c.next().value;
52+
53+
auto result = Io::atoi(value);
54+
if (not result)
55+
return Error::other("expected integer");
56+
57+
return Ok(result.unwrap());
58+
}
59+
60+
void ValueParser<Str>::usage(Io::Emit &e) {
61+
e("string");
62+
}
63+
64+
Res<Str> ValueParser<Str>::parse(Cursor<Token> &c) {
65+
if (c.ended() or c->kind != Token::OPERAND)
66+
return Error::other("missing value");
67+
68+
return Ok(c.next().value);
69+
}
70+
71+
} // namespace Karm::Cli

0 commit comments

Comments
 (0)