Skip to content

Commit ab32fbe

Browse files
monyarmPetarKirov
authored andcommitted
feat(mcl/main): Integrate argparse for command-line argument parsing and enhance command handling
1 parent 1629158 commit ab32fbe

File tree

3 files changed

+58
-49
lines changed

3 files changed

+58
-49
lines changed

packages/mcl/dub.sdl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ dflags "-mcpu=baseline" platform="dmd"
2121

2222
dependency "mir-cpuid" version="~>1.2.11"
2323
dependency "silly" version="~>1.1.1"
24+
dependency "argparse" version="~>1.4.1"

packages/mcl/dub.selections.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"fileVersion": 1,
33
"versions": {
4+
"argparse": "1.4.1",
45
"mir-core": "1.7.1",
56
"mir-cpuid": "1.2.11",
67
"silly": "1.1.1"

packages/mcl/src/main.d

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,80 @@
1+
module mcl.main;
2+
13
import std.stdio : writefln, writeln, stderr;
24
import std.array : replace;
35
import std.getopt : getopt;
46
import std.logger : infof, errorf, LogLevel;
5-
7+
import std.sumtype : SumType, match;
8+
import std.string : stripRight, stripLeft;
9+
import std.algorithm : endsWith;
10+
import std.format : format;
611
import mcl.utils.path : rootDir;
712
import mcl.utils.tui : bold;
813

9-
import cmds = mcl.commands;
14+
import mcl.commands;
1015

11-
alias supportedCommands = imported!`std.traits`.AliasSeq!(
12-
cmds.get_fstab,
13-
cmds.deploy_spec,
14-
cmds.ci_matrix,
15-
cmds.print_table,
16-
cmds.shard_matrix,
17-
cmds.host_info,
18-
cmds.ci,
19-
cmds.machine,
20-
cmds.config,
21-
);
16+
import argparse;
2217

23-
int main(string[] args)
18+
19+
@(Command(" ").Description(" "))
20+
struct unknown_command_args {}
21+
int unknown_command(unknown_command_args unused)
2422
{
25-
if (args.length < 2)
26-
return wrongUsage("no command selected");
23+
stderr.writeln("Unknown command. Use --help for a list of available commands.");
24+
return 1;
25+
}
2726

28-
string cmd = args[1];
29-
LogLevel logLevel = LogLevel.info;
30-
args.getopt("log-level", &logLevel);
27+
template genSubCommandArgs()
28+
{
29+
const char[] genSubCommandArgs =
30+
"@SubCommands\n"~
31+
"SumType!("~
32+
"Default!unknown_command_args"~
33+
") cmd;";
34+
}
3135

32-
setLogLevel(logLevel);
36+
template genSubCommandMatch()
37+
{
38+
const char[] generateMatchString = () {
39+
alias CmdTypes = typeof(MCLArgs.cmd).Types;
40+
string match = "int result = args.cmd.match!(";
3341

34-
infof("Git root: '%s'", rootDir.bold);
42+
static foreach (CmdType; CmdTypes)
43+
{{
44+
string name = CmdType.stringof.replace("Default!(", "").stripRight(")");
45+
match ~= format("\n\t(%s a) => %s(a)", name, name.replace("_args", "")) ~ ", ";
46+
}}
47+
match = match.stripRight(", ");
48+
match ~= "\n);";
3549

36-
try switch (cmd)
37-
{
38-
default:
39-
return wrongUsage("unknown command: `" ~ cmd ~ "`");
50+
return match;
51+
}();
52+
}
4053

41-
static foreach (command; supportedCommands)
42-
case __traits(identifier, command):
43-
{
54+
struct MCLArgs
55+
{
56+
@NamedArgument(["log-level"])
57+
LogLevel logLevel = cast(LogLevel)-1;
58+
mixin(genSubCommandArgs!());
59+
}
60+
61+
mixin CLI!MCLArgs.main!((args)
62+
{
63+
static assert(is(typeof(args) == MCLArgs));
64+
65+
LogLevel logLevel = LogLevel.info;
66+
if (args.logLevel != cast(LogLevel)-1)
67+
logLevel = args.logLevel;
68+
setLogLevel(logLevel);
69+
70+
mixin genSubCommandMatch;
4471

45-
infof("Running %s task", cmd.bold);
46-
command(args[2..$]);
47-
infof("Execution Succesfull");
4872
return 0;
49-
}
50-
}
51-
catch (Exception e)
52-
{
53-
errorf("Task %s failed. Error:\n%s", cmd.bold, e);
54-
return 1;
55-
}
56-
}
73+
});
5774

5875
void setLogLevel(LogLevel l)
5976
{
6077
import std.logger : globalLogLevel, sharedLog;
6178
globalLogLevel = l;
6279
(cast()sharedLog()).logLevel = l;
6380
}
64-
65-
int wrongUsage(string error)
66-
{
67-
writefln("Error: %s.", error);
68-
writeln("Usage:\n");
69-
static foreach (cmd; supportedCommands)
70-
writefln(" mcl %s", __traits(identifier, cmd));
71-
72-
return 1;
73-
}

0 commit comments

Comments
 (0)